import InputText from '@/components/InputText'; import InputPhone from '@/components/InputPhone'; import React, { useEffect } from 'react'; import { useTranslations } from 'next-intl'; import { Trash2, Plus, Users } from 'lucide-react'; import SectionHeader from '@/components/SectionHeader'; export default function ResponsableInputFields({ guardians, setGuardians, errors, setIsPageValid, }) { const t = useTranslations('ResponsableInputFields'); useEffect(() => { const isValid = guardians.length > 0 && guardians.every((guardian, index) => { return !Object.keys(guardian).some( (field) => getLocalError(index, field) !== '' ); }); setIsPageValid(isValid); }, [guardians, setIsPageValid]); const getError = (index, field) => { return errors[index]?.[field]?.[0]; }; const getLocalError = (index, field) => { if ( // Student Form (field === 'last_name' && (!guardians[index].last_name || guardians[index].last_name.trim() === '')) || (field === 'first_name' && (!guardians[index].first_name || guardians[index].first_name.trim() === '')) || (field === 'email' && (!guardians[index].associated_profile_email || guardians[index].associated_profile_email.trim() === '')) || (field === 'birth_date' && (!guardians[index].birth_date || guardians[index].birth_date.trim() === '')) || (field === 'profession' && (!guardians[index].profession || guardians[index].profession.trim() === '')) || (field === 'address' && (!guardians[index].address || guardians[index].address.trim() === '')) ) { return 'Champs requis'; } return ''; }; const onGuardiansChange = (id, field, value) => { const updatedGuardians = guardians.map((guardian) => guardian.id === id ? { ...guardian, [field]: value } : guardian ); setGuardians(updatedGuardians); }; const addGuardian = () => { setGuardians([...guardians, { id: Date.now(), name: '', email: '' }]); }; const deleteGuardian = (index) => { const updatedGuardians = guardians.filter((_, i) => i !== index); setGuardians(updatedGuardians); }; return (
{guardians.map((item, index) => (

{t('responsable')} {index + 1}

{guardians.length > 1 && ( deleteGuardian(index)} /> )}
{ onGuardiansChange(item.id, 'last_name', event.target.value); }} errorMsg={ getError(index, 'last_name') || getLocalError(index, 'last_name') } required /> { onGuardiansChange(item.id, 'first_name', event.target.value); }} errorMsg={ getError(index, 'first_name') || getLocalError(index, 'first_name') } required />
{ onGuardiansChange( item.id, 'associated_profile_email', event.target.value ); }} required errorMsg={ getError(index, 'email') || getLocalError(index, 'email') } /> { onGuardiansChange(item.id, 'phone', event); }} required errorMsg={getError(index, 'phone')} />
{ onGuardiansChange(item.id, 'birth_date', event.target.value); }} required errorMsg={ getError(index, 'birth_date') || getLocalError(index, 'birth_date') } /> { onGuardiansChange(item.id, 'profession', event.target.value); }} required errorMsg={ getError(index, 'profession') || getLocalError(index, 'profession') } />
{ onGuardiansChange(item.id, 'address', event.target.value); }} required errorMsg={ getError(index, 'address') || getLocalError(index, 'address') } />
))}
addGuardian(e)} />
); }