import { useState } from "react"; import { Card, CardContent } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { useCourses, useChapters } from "@/hooks/queries"; import { lmsService } from "@/services"; import { useQueryClient } from "@tanstack/react-query"; import { Link, useNavigate } from "react-router-dom"; import { Plus, Users, BookOpen, Pencil, FolderOpen, Wand2, BarChart3 } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import type { Course } from "@/types"; function CourseCard({ course }: { course: Course }) { const [editOpen, setEditOpen] = useState(false); const [form, setForm] = useState({ title: course.title, code: course.code, description: course.description, max_capacity: course.max_capacity }); const { data: chapters = [] } = useChapters(course.id); const qc = useQueryClient(); const { toast } = useToast(); const navigate = useNavigate(); const [saving, setSaving] = useState(false); async function handleSave() { setSaving(true); try { await lmsService.updateCourse(course.id, form); qc.invalidateQueries({ queryKey: ["lms", "courses"] }); toast({ title: "Course updated" }); setEditOpen(false); } catch (e: unknown) { toast({ title: "Error", description: e instanceof Error ? e.message : String(e), variant: "destructive" }); } finally { setSaving(false); } } const totalMaterials = chapters.reduce((s, ch) => s + (ch.material_count || 0), 0); return ( <>
{course.status} {course.code}

{course.title}

{course.description &&

{course.description}

}
{course.enrolled} students {chapters.length} chapters ยท {totalMaterials} materials
Edit Course
setForm(f => ({ ...f, title: e.target.value }))} />
setForm(f => ({ ...f, code: e.target.value }))} />