"use client"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; import { toast } from "sonner"; import { api } from "@/lib/api"; import type { Party } from "@/lib/types"; import { Plus, Trash2, X, Users } from "lucide-react"; interface PartyListProps { caseId: string; parties: Party[]; } interface PartyFormData { name: string; role: string; representative: string; } const ROLE_OPTIONS = [ "Kläger", "Beklagter", "Nebenintervenient", "Patentinhaber", "Streithelfer", ]; const inputClass = "w-full rounded-md border border-neutral-200 bg-white px-3 py-1.5 text-sm outline-none transition-colors focus:border-neutral-400 focus:ring-1 focus:ring-neutral-400"; export function PartyList({ caseId, parties }: PartyListProps) { const queryClient = useQueryClient(); const [showForm, setShowForm] = useState(false); const [form, setForm] = useState({ name: "", role: "", representative: "", }); const addMutation = useMutation({ mutationFn: (data: PartyFormData) => api.post(`/cases/${caseId}/parties`, { name: data.name, role: data.role || undefined, representative: data.representative || undefined, }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["case", caseId] }); toast.success("Partei hinzugefügt"); setShowForm(false); setForm({ name: "", role: "", representative: "" }); }, onError: () => toast.error("Fehler beim Hinzufügen"), }); const deleteMutation = useMutation({ mutationFn: (partyId: string) => api.delete(`/parties/${partyId}`), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["case", caseId] }); toast.success("Partei entfernt"); }, onError: () => toast.error("Fehler beim Entfernen"), }); return (

Parteien ({parties.length})

{!showForm && ( )}
{parties.length === 0 && !showForm && (

Keine Parteien vorhanden.

)}
{parties.map((party) => (

{party.name}

{party.role && {party.role}} {party.representative && ( Vertreter: {party.representative} )}
))}
{showForm && (
Neue Partei
{ e.preventDefault(); if (!form.name.trim()) { toast.error("Bitte Namen eingeben"); return; } addMutation.mutate(form); }} className="mt-3 space-y-3" > setForm({ ...form, name: e.target.value })} className={inputClass} />
setForm({ ...form, representative: e.target.value }) } className={inputClass} />
)}
); }