"use client"; import { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { Plus, Search, Pencil } from "lucide-react"; import { Button, buttonVariants } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { Switch } from "@/components/ui/switch"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import DeleteDialog from "@/components/admin/delete-dialog"; import { toast } from "sonner"; import { cn, formatDateShort } from "@/lib/utils"; type Alert = { id: string; title: string; type: string; region: string; active: boolean; activeFrom: Date; activeTo: Date | null; createdAt: Date; }; const TYPE_STYLES: Record = { WARNING: "bg-gold/10 text-gold border-gold/20", INFO: "bg-[#60A5FA]/10 text-[#60A5FA] border-[#60A5FA]/20", DANGER: "bg-wine/10 text-[#FB7185] border-wine/20", }; export default function AlertsClient({ alerts }: { alerts: Alert[] }) { const router = useRouter(); const [search, setSearch] = useState(""); const filtered = alerts.filter( (a) => a.title.toLowerCase().includes(search.toLowerCase()) || a.region.toLowerCase().includes(search.toLowerCase()) ); async function handleToggleActive(id: string, active: boolean) { try { const alert = alerts.find((a) => a.id === id); if (!alert) return; const res = await fetch(`/api/alerts/${id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...alert, active }), }); if (!res.ok) throw new Error(); toast.success(active ? "Alerte activee" : "Alerte desactivee"); router.refresh(); } catch { toast.error("Erreur"); } } async function handleDelete(id: string) { const res = await fetch(`/api/alerts/${id}`, { method: "DELETE" }); if (!res.ok) { toast.error("Erreur lors de la suppression"); return; } toast.success("Alerte supprimee"); router.refresh(); } return (

Alertes saisonnieres

{alerts.length} alertes

Ajouter
setSearch(e.target.value)} className="pl-9 rounded-xl bg-card border-[oklch(0.22_0.005_60)] text-cream placeholder:text-stone-700 focus:border-vine/40" />
Titre Type Region Periode Active Actions {filtered.map((alert) => ( {alert.title} {alert.type} {alert.region} {formatDateShort(alert.activeFrom)} {alert.activeTo && ` — ${formatDateShort(alert.activeTo)}`} handleToggleActive(alert.id, checked)} />
handleDelete(alert.id)} />
))}
{filtered.length === 0 && (

Aucune alerte trouvee

)}
{/* Mobile */}
{filtered.map((alert) => (

{alert.title}

{alert.type} {alert.region} handleToggleActive(alert.id, checked)} className="ml-auto" />
))}
); }