"use client" import { useState } from "react" import { MoreHorizontal, Pencil, Plus, Trash2 } from "lucide-react" import { toast } from "sonner" import { useRouter } from "next/navigation" import type { SchoolListItem } from "../types" import { createSchoolAction, deleteSchoolAction, updateSchoolAction } from "../actions" import { Button } from "@/shared/components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "@/shared/components/ui/card" import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/shared/components/ui/dialog" import { Input } from "@/shared/components/ui/input" import { Label } from "@/shared/components/ui/label" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/shared/components/ui/table" import { EmptyState } from "@/shared/components/ui/empty-state" import { Badge } from "@/shared/components/ui/badge" 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 { formatDate } from "@/shared/lib/utils" export function SchoolsClient({ schools }: { schools: SchoolListItem[] }) { const router = useRouter() const [isWorking, setIsWorking] = useState(false) const [createOpen, setCreateOpen] = useState(false) const [editItem, setEditItem] = useState(null) const [deleteItem, setDeleteItem] = useState(null) const handleCreate = async (formData: FormData) => { setIsWorking(true) try { const res = await createSchoolAction(undefined, formData) if (res.success) { toast.success(res.message) setCreateOpen(false) router.refresh() } else { toast.error(res.message || "Failed to create school") } } catch { toast.error("Failed to create school") } finally { setIsWorking(false) } } const handleUpdate = async (formData: FormData) => { if (!editItem) return setIsWorking(true) try { const res = await updateSchoolAction(editItem.id, undefined, formData) if (res.success) { toast.success(res.message) setEditItem(null) router.refresh() } else { toast.error(res.message || "Failed to update school") } } catch { toast.error("Failed to update school") } finally { setIsWorking(false) } } const handleDelete = async () => { if (!deleteItem) return setIsWorking(true) try { const res = await deleteSchoolAction(deleteItem.id) if (res.success) { toast.success(res.message) setDeleteItem(null) router.refresh() } else { toast.error(res.message || "Failed to delete school") } } catch { toast.error("Failed to delete school") } finally { setIsWorking(false) } } return ( <>
All schools {schools.length} {schools.length === 0 ? ( ) : ( Name Code Updated {schools.map((s) => ( {s.name} {s.code || "-"} {formatDate(s.updatedAt)} setEditItem(s)}> Edit setDeleteItem(s)} > Delete ))}
)}
New school
{ if (!open) setEditItem(null) }} > Edit school {editItem ? (
) : null}
{ if (!open) setDeleteItem(null) }} > Delete school This will permanently delete {deleteItem?.name || "this school"} and its grades. Cancel Delete ) }