'use client' import { useTransition, useState } from 'react' import { useRouter } from 'next/navigation' import { Loader2 } from 'lucide-react' import { toast } from 'sonner' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Select } from '@/components/ui/select' import { Textarea } from '@/components/ui/textarea' import { AUFWAND_MAX_MINUTEN, PRIORITAET_NAME, STATUS_NAME, TYP_NAME, moeglicheUebergaenge, } from '@/lib/aufgabe' import type { AufgabeAnzeige } from '@/lib/aufgabe-anzeige' import { aufgabeAendern, aufgabeAnlegen } from '@/server/aktionen/aufgaben' export type BereichWahl = { id: string; name: string } export type ProjektWahl = { id: string; name: string } type Eigenschaften = { /** Fehlt sie, wird eine neue Aufgabe angelegt. */ aufgabe?: AufgabeAnzeige bereiche: BereichWahl[] projekte: ProjektWahl[] /** Vorbelegung beim Anlegen. */ vorgabe?: { bereichId?: string; projektId?: string; typ?: string } onFertig?: () => void } const TYPEN = ['AUFGABE', 'IDEE', 'ARBEITSPAKET', 'MEILENSTEIN'] as const export function AufgabeFormular({ aufgabe, bereiche, projekte, vorgabe, onFertig, }: Eigenschaften) { const router = useRouter() const [laeuft, starte] = useTransition() const [felder, setFelder] = useState>({}) // Aus einem abgeschlossenen Zustand geht es nur zurück nach OFFEN. Die // Auswahl zeigt deshalb nur, was von hier aus wirklich möglich ist. const statusAuswahl = aufgabe ? [aufgabe.status, ...moeglicheUebergaenge(aufgabe.status)] : (['OFFEN', 'GEPLANT', 'WARTET'] as const) function absenden(ereignis: React.FormEvent) { ereignis.preventDefault() const formular = ereignis.currentTarget const daten = new FormData(formular) starte(async () => { const antwort = aufgabe ? await aufgabeAendern(aufgabe.id, daten) : await aufgabeAnlegen(daten) if (antwort.ok) { setFelder({}) toast.success(antwort.meldung) if (!aufgabe) formular.reset() router.refresh() onFertig?.() } else { setFelder(antwort.felder ?? {}) toast.error(antwort.fehler) } }) } const kennung = aufgabe?.id ?? 'neu' return (
{felder.titel &&

{felder.titel}

}