"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 { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Switch } from "@/components/ui/switch"; import DeleteDialog from "@/components/admin/delete-dialog"; import { cn } from "@/lib/utils"; import { toast } from "sonner"; type Disease = { id: string; name: string; scientificName: string; slug: string; type: string; severity: string; published: boolean; createdAt: Date; _count: { scans: number }; }; const TYPE_LABELS: Record = { FUNGAL: "Fongique", BACTERIAL: "Bacterien", PEST: "Ravageur", ABIOTIC: "Carence", }; const TYPE_STYLES: Record = { FUNGAL: "bg-vine/10 text-vine border-vine/20", BACTERIAL: "bg-[#A78BFA]/10 text-[#A78BFA] border-[#A78BFA]/20", PEST: "bg-gold/10 text-gold border-gold/20", ABIOTIC: "bg-[#60A5FA]/10 text-[#60A5FA] border-[#60A5FA]/20", }; const SEVERITY_STYLES: Record = { HIGH: "bg-wine/10 text-[#FB7185] border-wine/20", MEDIUM: "bg-gold/10 text-gold border-gold/20", LOW: "bg-[#60A5FA]/10 text-[#60A5FA] border-[#60A5FA]/20", }; const SEVERITY_LABELS: Record = { HIGH: "Critique", MEDIUM: "Modere", LOW: "Faible", }; export default function DiseasesClient({ diseases }: { diseases: Disease[] }) { const router = useRouter(); const [search, setSearch] = useState(""); const [typeFilter, setTypeFilter] = useState("ALL"); const filtered = diseases.filter((d) => { const matchSearch = d.name.toLowerCase().includes(search.toLowerCase()) || d.scientificName.toLowerCase().includes(search.toLowerCase()); const matchType = typeFilter === "ALL" || d.type === typeFilter; return matchSearch && matchType; }); async function handleTogglePublish(id: string, published: boolean) { try { const disease = diseases.find((d) => d.id === id); if (!disease) return; const res = await fetch(`/api/diseases/${id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...disease, published, symptoms: [], description: "placeholder", treatment: "placeholder", season: "placeholder", }), }); if (!res.ok) throw new Error(); toast.success(published ? "Maladie publiee" : "Maladie depubliee"); router.refresh(); } catch { toast.error("Erreur lors de la mise a jour"); } } async function handleDelete(id: string) { const res = await fetch(`/api/diseases/${id}`, { method: "DELETE" }); if (!res.ok) { toast.error("Erreur lors de la suppression"); return; } toast.success("Maladie supprimee"); router.refresh(); } return (
{/* Header */}

Maladies de la vigne

{diseases.length} maladies repertoriees

Ajouter
{/* Filters */}
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" />
{["ALL", "FUNGAL", "BACTERIAL", "PEST", "ABIOTIC"].map((type) => ( ))}
{/* Table (desktop) */}
Nom Type Severite Scans Publie Actions {filtered.map((disease) => (

{disease.name}

{disease.scientificName && (

{disease.scientificName}

)}
{TYPE_LABELS[disease.type]} {SEVERITY_LABELS[disease.severity]} {disease._count.scans} handleTogglePublish(disease.id, checked)} />
handleDelete(disease.id)} />
))}
{filtered.length === 0 && (

Aucune maladie trouvee

)}
{/* Cards (mobile) */}
{filtered.map((disease) => (

{disease.name}

{disease.scientificName && (

{disease.scientificName}

)}
{TYPE_LABELS[disease.type]} {SEVERITY_LABELS[disease.severity]} {disease._count.scans} scans
))}
); }