import React, { useEffect, useState } from 'react'; import { getCoursesLinkedToProduct, unlinkCourseFromProduct } from '@services/payments/products'; import { useLHSession } from '@components/Contexts/LHSessionContext'; import { useOrg } from '@components/Contexts/OrgContext'; import { Trash2, Plus, BookOpen } from 'lucide-react'; import { Button } from "@/components/ui/button"; import toast from 'react-hot-toast'; import { mutate } from 'swr'; import Modal from '@components/StyledElements/Modal/Modal'; import LinkCourseModal from './LinkCourseModal'; interface ProductLinkedCoursesProps { productId: string; } export default function ProductLinkedCourses({ productId }: ProductLinkedCoursesProps) { const [linkedCourses, setLinkedCourses] = useState([]); const [isLinkModalOpen, setIsLinkModalOpen] = useState(false); const session = useLHSession() as any; const org = useOrg() as any; const fetchLinkedCourses = async () => { try { const response = await getCoursesLinkedToProduct(org.id, productId, session.data?.tokens?.access_token); setLinkedCourses(response.data || []); } catch (error) { toast.error('Failed to fetch linked courses'); } }; const handleUnlinkCourse = async (courseId: string) => { try { const response = await unlinkCourseFromProduct(org.id, productId, courseId, session.data?.tokens?.access_token); if (response.success) { await fetchLinkedCourses(); mutate([`/payments/${org.id}/products`, session.data?.tokens?.access_token]); toast.success('Course unlinked successfully'); } else { toast.error(response.data?.detail || 'Failed to unlink course'); } } catch (error) { toast.error('Failed to unlink course'); } }; useEffect(() => { if (org && session && productId) { fetchLinkedCourses(); } }, [org, session, productId]); return (

Linked Courses

{ setIsLinkModalOpen(false); fetchLinkedCourses(); }} /> } dialogTrigger={ } />
{linkedCourses.length === 0 ? (
No courses linked yet
) : ( linkedCourses.map((course) => (
{course.name}
)) )}
); }