"use client"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { api } from "@/lib/api"; import type { Note } from "@/lib/types"; import { format, parseISO } from "date-fns"; import { de } from "date-fns/locale"; import { Plus, Pencil, Trash2, X, Check, MessageSquare } from "lucide-react"; import { useState } from "react"; import { toast } from "sonner"; interface NotesListProps { parentType: "case" | "deadline" | "appointment" | "case_event"; parentId: string; } export function NotesList({ parentType, parentId }: NotesListProps) { const queryClient = useQueryClient(); const queryKey = ["notes", parentType, parentId]; const [newContent, setNewContent] = useState(""); const [showNew, setShowNew] = useState(false); const [editingId, setEditingId] = useState(null); const [editContent, setEditContent] = useState(""); const { data: notes, isLoading } = useQuery({ queryKey, queryFn: () => api.get(`/notes?${parentType}_id=${parentId}`), }); const createMutation = useMutation({ mutationFn: (content: string) => { const body: Record = { content, [`${parentType}_id`]: parentId, }; return api.post("/notes", body); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey }); setNewContent(""); setShowNew(false); toast.success("Notiz erstellt"); }, onError: () => toast.error("Fehler beim Erstellen der Notiz"), }); const updateMutation = useMutation({ mutationFn: ({ id, content }: { id: string; content: string }) => api.put(`/notes/${id}`, { content }), onSuccess: () => { queryClient.invalidateQueries({ queryKey }); setEditingId(null); toast.success("Notiz aktualisiert"); }, onError: () => toast.error("Fehler beim Aktualisieren der Notiz"), }); const deleteMutation = useMutation({ mutationFn: (id: string) => api.delete(`/notes/${id}`), onSuccess: () => { queryClient.invalidateQueries({ queryKey }); toast.success("Notiz geloescht"); }, onError: () => toast.error("Fehler beim Loeschen der Notiz"), }); function handleCreate() { if (!newContent.trim()) return; createMutation.mutate(newContent.trim()); } function handleUpdate(id: string) { if (!editContent.trim()) return; updateMutation.mutate({ id, content: editContent.trim() }); } function startEdit(note: Note) { setEditingId(note.id); setEditContent(note.content); } const notesList = Array.isArray(notes) ? notes : []; return (

Notizen

{!showNew && ( )}
{showNew && (