/** * FEAT-6: Gesundheit-Tab — Gesundheitseinträge eines Tieres: * Liste (Datum absteigend) + Inline-Formular zum Anlegen/Bearbeiten, * Löschen mit Bestätigung. Deutsch durchgehend. */ import { useState, type FormEvent } from 'react' import { de } from '../strings/de' import { createHealthRecord, deleteHealthRecord, listHealthRecords, updateHealthRecord, HEALTH_RECORD_TYPES, type CreateHealthRecord, type HealthRecord, type HealthRecordType, } from '../api/healthRecords' import { useApi, useMutation } from '../hooks/useApi' import { formatDate } from '../format/labels' import { todayISO } from '../format/dates' interface FormState { date: string type: HealthRecordType description: string veterinarian: string } const emptyForm = (): FormState => ({ date: todayISO(), type: 'Examination', description: '', veterinarian: '', }) export default function GerbilHealthTab({ gerbilId }: { gerbilId: string }) { const t = de.pages.tierTabs.health const ta = de.pages.tierTabs.actions const records = useApi(() => listHealthRecords(gerbilId), [gerbilId]) // editing: null = kein Formular, 'new' = anlegen, sonst Record-Id (bearbeiten) const [editing, setEditing] = useState(null) const [form, setForm] = useState(emptyForm) const [errors, setErrors] = useState>>({}) const save = useMutation((body: CreateHealthRecord) => editing && editing !== 'new' ? updateHealthRecord(editing, body) : createHealthRecord(body), ) const removal = useMutation((recordId: string) => deleteHealthRecord(recordId)) const set = (key: K, value: FormState[K]) => setForm((f) => ({ ...f, [key]: value })) function openCreate() { setForm(emptyForm()) setErrors({}) setEditing('new') } function openEdit(r: HealthRecord) { setForm({ date: r.date, type: r.type, description: r.description, veterinarian: r.veterinarian ?? '', }) setErrors({}) setEditing(r.id) } async function onSubmit(e: FormEvent) { e.preventDefault() const next: Partial> = {} if (!form.date) next.date = t.validation.dateRequired if (form.description.trim() === '') next.description = t.validation.descriptionRequired setErrors(next) if (Object.keys(next).length > 0) return await save.run({ gerbilId, date: form.date, type: form.type, description: form.description.trim(), veterinarian: form.veterinarian.trim() === '' ? null : form.veterinarian.trim(), }) setEditing(null) records.reload() } async function onDelete(recordId: string) { if (!window.confirm(t.deleteConfirm)) return await removal.run(recordId) records.reload() } return (
{editing === null && ( )} {editing !== null && (

{editing === 'new' ? t.createTitle : t.editTitle}