"use client"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { api } from "@/lib/api"; import type { Appointment, Case } from "@/lib/types"; import { format, parseISO } from "date-fns"; import { X } from "lucide-react"; import { toast } from "sonner"; import { useEffect, useState } from "react"; const APPOINTMENT_TYPES = [ { value: "hearing", label: "Verhandlung" }, { value: "meeting", label: "Besprechung" }, { value: "consultation", label: "Beratung" }, { value: "deadline_hearing", label: "Fristanhorung" }, { value: "other", label: "Sonstiges" }, ]; interface AppointmentModalProps { open: boolean; onClose: () => void; appointment?: Appointment | null; } function toLocalDatetime(iso: string): string { const d = parseISO(iso); return format(d, "yyyy-MM-dd'T'HH:mm"); } export function AppointmentModal({ open, onClose, appointment }: AppointmentModalProps) { const queryClient = useQueryClient(); const isEdit = !!appointment; const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [startAt, setStartAt] = useState(""); const [endAt, setEndAt] = useState(""); const [location, setLocation] = useState(""); const [appointmentType, setAppointmentType] = useState(""); const [caseId, setCaseId] = useState(""); const { data: cases } = useQuery({ queryKey: ["cases"], queryFn: () => api.get<{ cases: Case[]; total: number }>("/cases"), }); useEffect(() => { if (appointment) { setTitle(appointment.title); setDescription(appointment.description ?? ""); setStartAt(toLocalDatetime(appointment.start_at)); setEndAt(appointment.end_at ? toLocalDatetime(appointment.end_at) : ""); setLocation(appointment.location ?? ""); setAppointmentType(appointment.appointment_type ?? ""); setCaseId(appointment.case_id ?? ""); } else { setTitle(""); setDescription(""); setStartAt(""); setEndAt(""); setLocation(""); setAppointmentType(""); setCaseId(""); } }, [appointment]); const createMutation = useMutation({ mutationFn: (body: Record) => api.post("/appointments", body), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["appointments"] }); queryClient.invalidateQueries({ queryKey: ["dashboard"] }); toast.success("Termin erstellt"); onClose(); }, onError: () => toast.error("Fehler beim Erstellen des Termins"), }); const updateMutation = useMutation({ mutationFn: (body: Record) => api.put(`/appointments/${appointment!.id}`, body), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["appointments"] }); queryClient.invalidateQueries({ queryKey: ["dashboard"] }); toast.success("Termin aktualisiert"); onClose(); }, onError: () => toast.error("Fehler beim Aktualisieren des Termins"), }); const deleteMutation = useMutation({ mutationFn: () => api.delete(`/appointments/${appointment!.id}`), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["appointments"] }); queryClient.invalidateQueries({ queryKey: ["dashboard"] }); toast.success("Termin geloscht"); onClose(); }, onError: () => toast.error("Fehler beim Loschen des Termins"), }); function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!title.trim() || !startAt) return; const body: Record = { title: title.trim(), start_at: new Date(startAt).toISOString(), }; if (description.trim()) body.description = description.trim(); if (endAt) body.end_at = new Date(endAt).toISOString(); if (location.trim()) body.location = location.trim(); if (appointmentType) body.appointment_type = appointmentType; if (caseId) body.case_id = caseId; if (isEdit) { updateMutation.mutate(body); } else { createMutation.mutate(body); } } const isPending = createMutation.isPending || updateMutation.isPending; if (!open) return null; return (

{isEdit ? "Termin bearbeiten" : "Neuer Termin"}

setTitle(e.target.value)} required className="w-full rounded-md border border-neutral-200 px-3 py-1.5 text-sm outline-none focus:border-neutral-400 focus:ring-1 focus:ring-neutral-400" placeholder="z.B. Mundliche Verhandlung" />
setStartAt(e.target.value)} required className="w-full rounded-md border border-neutral-200 px-3 py-1.5 text-sm outline-none focus:border-neutral-400 focus:ring-1 focus:ring-neutral-400" />
setEndAt(e.target.value)} className="w-full rounded-md border border-neutral-200 px-3 py-1.5 text-sm outline-none focus:border-neutral-400 focus:ring-1 focus:ring-neutral-400" />
setLocation(e.target.value)} className="w-full rounded-md border border-neutral-200 px-3 py-1.5 text-sm outline-none focus:border-neutral-400 focus:ring-1 focus:ring-neutral-400" placeholder="z.B. UPC Munchen, Saal 3" />