'use client'; /** * Patient Form Component (FHIR-based) * Form for creating/editing patients using FHIR format */ import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { Save, Loader2 } from 'lucide-react'; import { createPatient, updatePatient } from '../actions'; import type { FHIRPatient } from '@/lib/fhir'; interface PatientFormProps { patient?: FHIRPatient; } export function PatientForm({ patient }: PatientFormProps) { const router = useRouter(); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const existingName = patient?.name?.[0]; const existingBsn = patient?.identifier?.find( (id) => id.system === 'http://fhir.nl/fhir/NamingSystem/bsn' )?.value; const existingPhone = patient?.telecom?.find((t) => t.system === 'phone')?.value; const existingEmail = patient?.telecom?.find((t) => t.system === 'email')?.value; async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setIsSubmitting(true); setError(null); try { const formData = new FormData(e.currentTarget); // Build FHIR Patient resource const fhirPatient: FHIRPatient = { resourceType: 'Patient', identifier: [ { system: 'http://fhir.nl/fhir/NamingSystem/bsn', value: formData.get('bsn') as string, use: 'official' as const, }, ], name: [ { use: 'official' as const, family: formData.get('family') as string, given: [formData.get('given') as string].filter(Boolean), prefix: formData.get('prefix') ? [(formData.get('prefix') as string)] : undefined, }, ], gender: formData.get('gender') as 'male' | 'female' | 'other' | 'unknown', birthDate: formData.get('birthDate') as string, telecom: [ formData.get('phone') ? { system: 'phone' as const, value: formData.get('phone') as string, use: 'mobile' as const, } : undefined, formData.get('email') ? { system: 'email' as const, value: formData.get('email') as string, } : undefined, ].filter((t): t is NonNullable => t !== undefined), active: true, }; if (patient?.id) { // Update existing patient await updatePatient(patient.id, fhirPatient); } else { // Create new patient await createPatient(fhirPatient); } router.push('/epd/patients'); router.refresh(); } catch (err) { setError(err instanceof Error ? err.message : 'Er is een fout opgetreden'); setIsSubmitting(false); } } return (
{error && (

{error}

)} {/* Name Fields */}
{/* BSN and Birth Date */}
{/* Gender */}
{/* Contact Information */}
{/* Action Buttons */}
); }