import { useState } from "react"; import { useParams } from "react-router-dom"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; import { Plus, GripVertical, Lock, Unlock, Trash2, Loader2, BookOpen } from "lucide-react"; import { useChapters, useCreateChapter, useDeleteChapter, useUnlockChapter, useLockChapter } from "@/hooks/queries"; import { useToast } from "@/hooks/use-toast"; import type { ChapterUnlockMode } from "@/types/courseware"; export default function CourseChapters() { const { courseId } = useParams<{ courseId: string }>(); const cid = Number(courseId); const { toast } = useToast(); const { data: chapters = [], isLoading } = useChapters(cid); const createChapter = useCreateChapter(); const deleteChapter = useDeleteChapter(); const unlockChapter = useUnlockChapter(); const lockChapter = useLockChapter(); const [showAdd, setShowAdd] = useState(false); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [startDate, setStartDate] = useState(""); const [unlockMode, setUnlockMode] = useState("manual"); const resetForm = () => { setName(""); setDescription(""); setStartDate(""); setUnlockMode("manual"); }; const handleCreate = () => { createChapter.mutate( { courseId: cid, data: { name, course_id: cid, description, start_date: startDate || undefined, unlock_mode: unlockMode } }, { onSuccess: () => { toast({ title: "Chapter Created" }); setShowAdd(false); resetForm(); }, onError: () => toast({ title: "Error", description: "Failed to create chapter", variant: "destructive" }), }, ); }; const handleDelete = (id: number) => { deleteChapter.mutate( { id, courseId: cid }, { onSuccess: () => toast({ title: "Chapter Deleted" }), onError: () => toast({ title: "Error", description: "Failed to delete chapter", variant: "destructive" }), }, ); }; const handleToggleLock = (id: number, isUnlocked: boolean) => { const mutation = isUnlocked ? lockChapter : unlockChapter; mutation.mutate( { id, courseId: cid }, { onSuccess: () => toast({ title: isUnlocked ? "Chapter Locked" : "Chapter Unlocked" }), onError: () => toast({ title: "Error", description: "Failed to update lock status", variant: "destructive" }), }, ); }; if (isLoading) return
; return (

Course Chapters

Manage chapters and their sequence for this course.

Add New Chapter
setName(e.target.value)} />