"use client" import { useState } from "react" import { useRouter } from "next/navigation" import { MoreHorizontal, UserCheck, UserX } from "lucide-react" import { toast } from "sonner" import { Badge } from "@/shared/components/ui/badge" import { Button } from "@/shared/components/ui/button" import { cn } from "@/shared/lib/utils" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/shared/components/ui/dropdown-menu" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/shared/components/ui/alert-dialog" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/shared/components/ui/table" import type { ClassStudent } from "../types" import { setStudentEnrollmentStatusAction } from "../actions" export function StudentsTable({ students }: { students: ClassStudent[] }) { const router = useRouter() const [workingKey, setWorkingKey] = useState(null) const [removeTarget, setRemoveTarget] = useState(null) const setStatus = async (student: ClassStudent, status: "active" | "inactive") => { const key = `${student.classId}:${student.id}:${status}` setWorkingKey(key) try { const res = await setStudentEnrollmentStatusAction(student.classId, student.id, status) if (res.success) { toast.success(res.message) router.refresh() } else { toast.error(res.message || "Failed to update student") } } catch { toast.error("Failed to update student") } finally { setWorkingKey(null) } } return ( <> Student Email Class Status Actions {students.map((s) => ( {s.name} {s.email} {s.className} {s.status === "active" ? "Active" : "Inactive"} {s.status !== "active" ? ( setStatus(s, "active")} disabled={workingKey !== null}> Set active ) : ( setStatus(s, "inactive")} disabled={workingKey !== null}> Set inactive )} setRemoveTarget(s)} className="text-destructive focus:text-destructive" disabled={s.status === "inactive" || workingKey !== null} > Remove from class ))}
{ if (workingKey !== null) return if (!open) setRemoveTarget(null) }} > Remove student from class? {removeTarget ? ( <> This will set {removeTarget.name} to inactive in{" "} {removeTarget.className}. ) : null} Cancel { if (!removeTarget) return setRemoveTarget(null) setStatus(removeTarget, "inactive") }} > Remove ) }