import { useState } from "react"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { useTimetable, useCourses } from "@/hooks/queries"; import { lmsService } from "@/services/lms.service"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useToast } from "@/hooks/use-toast"; import { Plus, Trash2 } from "lucide-react"; const days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] as const; const hours = ["08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00"]; const SESSION_COLOR = "hsl(192, 82%, 32%)"; export default function AdminTimetable() { const { data: rawSessions, isLoading } = useTimetable(); const timetableSessions = Array.isArray(rawSessions) ? rawSessions : []; const [createOpen, setCreateOpen] = useState(false); const [selectedSession, setSelectedSession] = useState<(typeof timetableSessions)[number] | null>(null); const [form, setForm] = useState({ course_id: "", day: "Monday" as string, start_time: "08:00", end_time: "09:00", room: "" }); const { toast } = useToast(); const qc = useQueryClient(); const { data: coursesData } = useCourses({ size: 200 }); const courses = coursesData?.items ?? []; const createMutation = useMutation({ mutationFn: (data: Record) => lmsService.createTimetableSession(data as never), onSuccess: () => { qc.invalidateQueries({ queryKey: ["lms", "timetable"] }); setCreateOpen(false); toast({ title: "Session created" }); }, onError: (err: Error) => toast({ title: "Error", description: err.message, variant: "destructive" }), }); const deleteMutation = useMutation({ mutationFn: (id: number) => lmsService.deleteTimetableSession(id), onSuccess: () => { qc.invalidateQueries({ queryKey: ["lms", "timetable"] }); setSelectedSession(null); toast({ title: "Session deleted" }); }, onError: (err: Error) => toast({ title: "Error", description: err.message, variant: "destructive" }), }); if (isLoading) return
; function isStart(day: string, hour: string) { return timetableSessions.find(s => s.day === day && s.start_time === hour); } function getSessionAt(day: string, hour: string) { return timetableSessions.find(s => s.day === day && s.start_time <= hour && s.end_time > hour); } return (

Master Timetable

View and manage all scheduled sessions.

{days.map(d =>
{d}
)}
{hours.map(hour => (
{hour}
{days.map(day => { const session = isStart(day, hour); const occupied = getSessionAt(day, hour); if (session) { return (
setSelectedSession(session)}>

{session.course_name}

{session.batch_name}

{session.teacher_name}

{session.room}

); } if (occupied) return
; return
; })}
))}
{ if (!v) setSelectedSession(null); }}> Session Details {selectedSession && (

Course: {selectedSession.course_name}

Batch: {selectedSession.batch_name}

Teacher: {selectedSession.teacher_name}

Day: {selectedSession.day}

Time: {selectedSession.start_time} – {selectedSession.end_time}

Room: {selectedSession.room}

)}
Add Timetable Session
setForm(f => ({ ...f, start_time: e.target.value }))} />
setForm(f => ({ ...f, end_time: e.target.value }))} />
setForm(f => ({ ...f, room: e.target.value }))} placeholder="e.g. Room A101" />
); }