import React, { useState } from 'react' import { useOrg } from '@components/Contexts/OrgContext' import { useLHSession } from '@components/Contexts/LHSessionContext' import useSWR from 'swr' import { getProductsByCourse, getStripeProductCheckoutSession } from '@services/payments/products' import { RefreshCcw, SquareCheck, ChevronDown, ChevronUp } from 'lucide-react' import { Badge } from '@components/ui/badge' import { Button } from '@components/ui/button' import toast from 'react-hot-toast' import { useRouter } from 'next/navigation' import { getUriWithOrg } from '@services/config/config' interface CoursePaidOptionsProps { course: { id: string; org_id: number; } } function CoursePaidOptions({ course }: CoursePaidOptionsProps) { const org = useOrg() as any const session = useLHSession() as any const [expandedProducts, setExpandedProducts] = useState<{ [key: string]: boolean }>({}) const [isProcessing, setIsProcessing] = useState<{ [key: string]: boolean }>({}) const router = useRouter() const { data: linkedProducts, error } = useSWR( () => org && session ? [`/payments/${course.org_id}/courses/${course.id}/products`, session.data?.tokens?.access_token] : null, ([url, token]) => getProductsByCourse(course.org_id, course.id, token) ) const handleCheckout = async (productId: number) => { if (!session.data?.user) { // Redirect to login if user is not authenticated router.push(`/signup?orgslug=${org.slug}`) return } try { setIsProcessing(prev => ({ ...prev, [productId]: true })) const redirect_uri = getUriWithOrg(org.slug, '/courses') const response = await getStripeProductCheckoutSession( course.org_id, productId, redirect_uri, session.data?.tokens?.access_token ) if (response.success) { router.push(response.data.checkout_url) } else { toast.error('Failed to initiate checkout process') } } catch (error) { toast.error('An error occurred while processing your request') } finally { setIsProcessing(prev => ({ ...prev, [productId]: false })) } } const toggleProductExpansion = (productId: string) => { setExpandedProducts(prev => ({ ...prev, [productId]: !prev[productId] })) } if (error) return
{product.description}
{product.benefits && ({product.benefits}