import InputText from '@/components/Form/InputText'; import InputPhone from '@/components/Form/InputPhone'; import React, { useEffect } from 'react'; import { useTranslations } from 'next-intl'; import { Trash2, Plus, Users } from 'lucide-react'; import SectionHeader from '@/components/SectionHeader'; import { useEstablishment } from '@/context/EstablishmentContext'; import logger from '@/utils/logger'; export default function ResponsableInputFields({ guardians, setGuardians, profiles, errors, setIsPageValid, enable = true, }) { const t = useTranslations('ResponsableInputFields'); const { selectedEstablishmentId } = useEstablishment(); const MAX_GUARDIANS = 2; 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) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; const dateRegex = /^\d{4}-\d{2}-\d{2}$/; 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 === 'associated_profile_email' && (!guardians[index].associated_profile_email || guardians[index].associated_profile_email.trim() === '' || !emailRegex.test(guardians[index].associated_profile_email))) || (field === 'birth_date' && (!guardians[index].birth_date || guardians[index].birth_date.trim() === '' || !dateRegex.test(guardians[index].birth_date))) || (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) => { if (guardian.id === id) { const updatedGuardian = { ...guardian, [field]: value }; // Synchroniser profile_data.email et profile_data.username avec associated_profile_email if (field === 'associated_profile_email') { const existingProfile = profiles?.find( (profile) => profile.email === value ); if (existingProfile) { updatedGuardian.profile_role_data.profile = existingProfile.id; delete updatedGuardian.profile_role_data.profile_data; } else { updatedGuardian.profile_role_data.profile_data.email = value; updatedGuardian.profile_role_data.profile_data.username = value; delete updatedGuardian.profile_role_data.profile; } } return updatedGuardian; } return guardian; }); setGuardians(updatedGuardians); }; const addGuardian = () => { setGuardians([ ...guardians, { profile_role_data: { establishment: selectedEstablishmentId, role_type: 2, is_active: false, profile_data: { email: '', password: 'Provisoire01!', username: '', }, }, last_name: '', first_name: '', birth_date: '', address: '', phone: '', profession: '', }, ]); }; const deleteGuardian = (index) => { const updatedGuardians = guardians.filter((_, i) => i !== index); setGuardians(updatedGuardians); }; return (