'use client'; import { useState, useTransition } from 'react'; import { createExamination, deleteExamination, type Examination } from '../../actions'; import { Loader2, Trash2 } from 'lucide-react'; import { format } from 'date-fns'; import { nl } from 'date-fns/locale'; const examinationTypes = ['Bloedonderzoek', 'Neuropsychologisch onderzoek', 'Psychodiagnostiek', 'IQ-test', 'Persoonlijkheidsonderzoek', 'Overig']; interface ExaminationManagerProps { patientId: string; intakeId: string; examinations: Examination[]; isRom?: boolean; } export function ExaminationManager({ patientId, intakeId, examinations, isRom }: ExaminationManagerProps) { const filtered = examinations.filter((exam) => isRom ? exam.examination_type === 'ROM' : exam.examination_type !== 'ROM' ); const [form, setForm] = useState({ date: '', type: examinationTypes[0], performer: '', findings: '', reason: '', notes: '', }); const [error, setError] = useState(null); const [isPending, startTransition] = useTransition(); const [deletingId, setDeletingId] = useState(null); const handleSubmit = () => { if (!form.date || !form.findings) { setError('Datum en bevindingen zijn verplicht.'); return; } setError(null); startTransition(async () => { try { await createExamination({ patientId, intakeId, date: form.date, type: form.type, findings: form.findings, performer: form.performer, reason: form.reason, notes: form.notes, isRom, }); setForm({ ...form, findings: '', notes: '' }); } catch (err) { setError(err instanceof Error ? err.message : 'Opslaan mislukt'); } }); }; const handleDelete = (id: string) => { setDeletingId(id); startTransition(async () => { try { await deleteExamination(patientId, intakeId, id); } catch (err) { setError(err instanceof Error ? err.message : 'Verwijderen mislukt'); } finally { setDeletingId(null); } }); }; return (
{filtered.length === 0 && (

{isRom ? 'Nog geen ROM-metingen' : 'Nog geen onderzoeken geregistreerd.'}

)} {filtered.map((exam) => (

{exam.examination_type}

{format(new Date(exam.examination_date), 'd MMM yyyy', { locale: nl })}

{exam.findings}

{exam.notes &&

Notities: {exam.notes}

}
))}

{isRom ? 'Nieuwe ROM-meting' : 'Nieuw onderzoek'}

setForm((prev) => ({ ...prev, date: e.target.value }))} className="h-10 rounded-md border border-slate-300 px-3 text-sm" /> {!isRom && ( )} setForm((prev) => ({ ...prev, performer: e.target.value }))} className="h-10 rounded-md border border-slate-300 px-3 text-sm" />