'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { Save, Loader2 } from 'lucide-react'; import type { ClientFormData } from '@/lib/types/client'; import type { Client } from '@/lib/types/client'; import { createClient, updateClient } from '../actions'; interface ClientFormProps { client?: Client; } export function ClientForm({ client }: ClientFormProps) { const router = useRouter(); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const [formData, setFormData] = useState({ first_name: client?.first_name || '', last_name: client?.last_name || '', birth_date: client?.birth_date || '', }); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setIsSubmitting(true); try { if (client) { await updateClient(client.id, formData); router.push(`/epd/clients/${client.id}`); } else { const newClient = await createClient(formData); router.push(`/epd/clients/${newClient.id}`); } router.refresh(); } catch (err) { console.error('Form submission error:', err); setError('Er is een fout opgetreden. Probeer het opnieuw.'); } finally { setIsSubmitting(false); } }; const handleChange = (field: keyof ClientFormData, value: string) => { setFormData((prev) => ({ ...prev, [field]: value })); }; return (
{/* Error Message */} {error && (

{error}

)} {/* Form Card */}
{/* First Name */}
handleChange('first_name', e.target.value)} className="w-full px-4 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-teal-500 focus:border-transparent" placeholder="bijv. Jan" />
{/* Last Name */}
handleChange('last_name', e.target.value)} className="w-full px-4 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-teal-500 focus:border-transparent" placeholder="bijv. de Vries" />
{/* Birth Date */}
handleChange('birth_date', e.target.value)} className="w-full px-4 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-teal-500 focus:border-transparent" />
{/* Form Actions */}
); }