"use client"; import { useState, useTransition } from "react"; import { Trash2, Plus, Utensils } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { addMitbringsel, deleteMitbringsel } from "../actions"; type MitbringselItemDto = { id: string; userId: string; content: string; user: { firstName: string; lastName: string }; }; export function MitbringselList({ terminId, items, currentUserId, isAdmin, }: { terminId: string; items: MitbringselItemDto[]; currentUserId: string; isAdmin: boolean; }) { const [content, setContent] = useState(""); const [isPending, startTransition] = useTransition(); const handleAdd = () => { if (!content.trim()) return; startTransition(async () => { const res = await addMitbringsel({ terminId, content }); if (res.error) { toast.error(res.error); } else { toast.success("Eintrag hinzugefügt."); setContent(""); } }); }; const handleDelete = (id: string) => { startTransition(async () => { const res = await deleteMitbringsel(id); if (res.error) { toast.error(res.error); } else { toast.success("Eintrag gelöscht."); } }); }; return (
Mitbring-Liste
{items.length === 0 ? (

Noch keine Einträge vorhanden.

) : ( items.map((item) => (
{item.user.firstName} {item.user.lastName} {item.content}
{(isAdmin || item.userId === currentUserId) && ( )}
)) )}
setContent(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); handleAdd(); } }} disabled={isPending} className="h-8 text-sm" />
); }