mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 07:53:23 +00:00
feat: Mise en place des actions pour chaque state du RF, possibilité
d'éditer le formulaire de création de RF (reste à submit un PUT)
This commit is contained in:
@ -9,7 +9,6 @@ import Table from '@/components/Table';
|
||||
import FeesSection from '@/components/Structure/Tarification/FeesSection';
|
||||
import DiscountsSection from '@/components/Structure/Tarification/DiscountsSection';
|
||||
import SectionTitle from '@/components/SectionTitle';
|
||||
import Popup from '@/components/Popup';
|
||||
import InputPhone from '@/components/InputPhone';
|
||||
import CheckBox from '@/components/CheckBox';
|
||||
import RadioList from '@/components/RadioList';
|
||||
@ -18,8 +17,9 @@ import { getCurrentSchoolYear, getNextSchoolYear } from '@/utils/Date';
|
||||
import logger from '@/utils/logger';
|
||||
import { levels, genders } from '@/utils/constants';
|
||||
import { useEstablishment } from '@/context/EstablishmentContext';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useSearchParams, useRouter } from 'next/navigation';
|
||||
import {
|
||||
fetchRegisterForm,
|
||||
fetchStudents,
|
||||
createRegisterForm,
|
||||
} from '@/app/actions/subscriptionAction';
|
||||
@ -37,6 +37,7 @@ import {
|
||||
createRegistrationSchoolFileTemplate,
|
||||
createRegistrationParentFileTemplate,
|
||||
} from '@/app/actions/registerFileGroupAction';
|
||||
import { fetchProfiles } from '@/app/actions/authAction';
|
||||
import { useClasses } from '@/context/ClassesContext';
|
||||
import { useCsrfToken } from '@/context/CsrfContext';
|
||||
import { FE_ADMIN_SUBSCRIPTIONS_URL } from '@/utils/Url';
|
||||
@ -46,6 +47,7 @@ export default function CreateSubscriptionPage() {
|
||||
studentLastName: '',
|
||||
studentFirstName: '',
|
||||
studentLevel: '',
|
||||
studentGender: '',
|
||||
guardianLastName: '',
|
||||
guardianFirstName: '',
|
||||
guardianEmail: '',
|
||||
@ -60,6 +62,11 @@ export default function CreateSubscriptionPage() {
|
||||
schoolYear: getCurrentSchoolYear(),
|
||||
});
|
||||
|
||||
const searchParams = useSearchParams();
|
||||
// Si l'ID est valorisé, alors on est en mode édition
|
||||
const registerFormID = searchParams.get('id');
|
||||
const registerFormMoment = searchParams.get('school_year');
|
||||
|
||||
const [students, setStudents] = useState([]);
|
||||
const [registrationDiscounts, setRegistrationDiscounts] = useState([]);
|
||||
const [tuitionDiscounts, setTuitionDiscounts] = useState([]);
|
||||
@ -67,16 +74,14 @@ export default function CreateSubscriptionPage() {
|
||||
const [tuitionFees, setTuitionFees] = useState([]);
|
||||
const [groups, setGroups] = useState([]);
|
||||
const [schoolFileMasters, setSchoolFileMasters] = useState([]);
|
||||
|
||||
const [parentFileMasters, setParentFileMasters] = useState([]);
|
||||
const [profiles, setProfiles] = useState([]);
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
const [existingGuardians, setExistingGuardians] = useState([]);
|
||||
const [totalRegistrationAmount, setTotalRegistrationAmount] = useState(0);
|
||||
const [totalTuitionAmount, setTotalTuitionAmount] = useState(0);
|
||||
const [popupVisible, setPopupVisible] = useState(false);
|
||||
const [popupMessage, setPopupMessage] = useState('');
|
||||
const [selectedStudent, setSelectedEleve] = useState(null);
|
||||
const [isNewResponsable, setIsNewResponsable] = useState(true);
|
||||
|
||||
@ -122,39 +127,31 @@ export default function CreateSubscriptionPage() {
|
||||
const getLocalError = (field) => {
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
|
||||
// Extraire tous les guardians des students
|
||||
const allGuardians = students.flatMap((student) => student.guardians);
|
||||
if (field === 'guardianEmail') {
|
||||
if (!formData.guardianEmail || formData.guardianEmail.trim() === '') {
|
||||
return 'Champs requis';
|
||||
}
|
||||
|
||||
if (
|
||||
// Student Form
|
||||
(field === 'schoolYear' &&
|
||||
(!formData.schoolYear || String(formData.schoolYear).trim() === '')) ||
|
||||
(field === 'studentLastName' &&
|
||||
(!formData.studentLastName ||
|
||||
formData.studentLastName.trim() === '')) ||
|
||||
(field === 'studentFirstName' &&
|
||||
(!formData.studentFirstName ||
|
||||
formData.studentFirstName.trim() === '')) ||
|
||||
(field === 'guardianEmail' &&
|
||||
(!formData.guardianEmail ||
|
||||
formData.guardianEmail.trim() === '' ||
|
||||
!emailRegex.test(formData.guardianEmail) ||
|
||||
allGuardians.some(
|
||||
(guardian) =>
|
||||
guardian.associated_profile_email === formData.guardianEmail
|
||||
))) // Vérifie si l'email existe déjà dans la liste des guardians
|
||||
) {
|
||||
return field === 'guardianEmail' &&
|
||||
allGuardians.some(
|
||||
if (!emailRegex.test(formData.guardianEmail)) {
|
||||
return 'Email invalide';
|
||||
}
|
||||
|
||||
// Vérifiez si l'email existe déjà, mais ne mettez pas à jour `formData` ici
|
||||
const existingGuardian = students
|
||||
.flatMap((student) => student.guardians)
|
||||
.find(
|
||||
(guardian) =>
|
||||
guardian.associated_profile_email === formData.guardianEmail
|
||||
)
|
||||
? 'Cet email est déjà associé à un responsable existant'
|
||||
: 'Champs requis';
|
||||
);
|
||||
|
||||
if (existingGuardian) {
|
||||
return ''; // Pas d'erreur, mais l'email existe déjà
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
};
|
||||
|
||||
const requestErrorHandler = (err) => {
|
||||
logger.error('Error fetching data:', err);
|
||||
setErrors(err);
|
||||
@ -165,7 +162,69 @@ export default function CreateSubscriptionPage() {
|
||||
}, [formData]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!formData.guardianEmail) {
|
||||
// Si l'email est vide, réinitialiser existingProfileId
|
||||
setFormData((prevData) => ({
|
||||
...prevData,
|
||||
isExistingParentProfile: false,
|
||||
existingProfileId: null,
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérifiez si le profil existe dans la liste des profils
|
||||
const existingProfile = profiles.find(
|
||||
(profile) => profile.email === formData.guardianEmail
|
||||
);
|
||||
|
||||
if (existingProfile) {
|
||||
// Si un profil avec cet email existe, valoriser isExistingParentProfile et existingProfileId
|
||||
setFormData((prevData) => ({
|
||||
...prevData,
|
||||
isExistingParentProfile: true,
|
||||
existingProfileId: existingProfile.id, // Récupérer l'ID du profil associé
|
||||
guardianLastName: existingProfile.last_name || '',
|
||||
guardianFirstName: existingProfile.first_name || '',
|
||||
guardianPhone: existingProfile.phone || '',
|
||||
}));
|
||||
} else {
|
||||
// Si aucun profil avec cet email n'existe, réinitialiser les champs
|
||||
setFormData((prevData) => ({
|
||||
...prevData,
|
||||
isExistingParentProfile: false,
|
||||
existingProfileId: null,
|
||||
}));
|
||||
}
|
||||
}, [formData.guardianEmail, profiles]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchProfiles()
|
||||
.then((data) => {
|
||||
setProfiles(data);
|
||||
})
|
||||
.catch(requestErrorHandler);
|
||||
if (selectedEstablishmentId) {
|
||||
if (registerFormID) {
|
||||
fetchRegisterForm(registerFormID)
|
||||
.then((data) => {
|
||||
setFormData((prevData) => ({
|
||||
...prevData,
|
||||
studentLastName: data?.student?.last_name || '',
|
||||
studentFirstName: data?.student?.first_name || '',
|
||||
studentLevel: data?.student?.level || '',
|
||||
studentGender: data?.student?.gender || '',
|
||||
guardianLastName: data?.student?.guardians[0]?.last_name || '',
|
||||
guardianFirstName: data?.student?.guardians[0]?.first_name || '',
|
||||
guardianEmail:
|
||||
data?.student?.guardians[0]?.associated_profile_email || '',
|
||||
guardianPhone: data?.student?.guardians[0]?.phone || '',
|
||||
selectedFileGroup: data?.fileGroup || '',
|
||||
schoolYear: data?.school_year || '',
|
||||
}));
|
||||
})
|
||||
.catch(requestErrorHandler);
|
||||
}
|
||||
|
||||
fetchStudents(selectedEstablishmentId)
|
||||
.then((studentsData) => {
|
||||
setStudents(studentsData);
|
||||
@ -219,6 +278,17 @@ export default function CreateSubscriptionPage() {
|
||||
}
|
||||
}, [selectedEstablishmentId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (registrationFees.length > 0) {
|
||||
const defaultSelectedFees = registrationFees.map((fee) => fee.id);
|
||||
const totalAmount = calculateFinalRegistrationAmount(
|
||||
defaultSelectedFees,
|
||||
formData.selectedRegistrationDiscounts
|
||||
);
|
||||
setTotalRegistrationAmount(totalAmount);
|
||||
}
|
||||
}, [registrationFees]);
|
||||
|
||||
const handleChange = (e) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData((prevState) => ({
|
||||
@ -250,21 +320,22 @@ export default function CreateSubscriptionPage() {
|
||||
};
|
||||
|
||||
const createRF = () => {
|
||||
logger.debug('createRF formData:', formData);
|
||||
logger.debug('createRF formData:', formDataRef.current);
|
||||
|
||||
// Préparation des données
|
||||
const selectedRegistrationFeesIds = formData.selectedRegistrationFees.map(
|
||||
(feeId) => feeId
|
||||
);
|
||||
const selectedRegistrationFeesIds =
|
||||
formDataRef.current.selectedRegistrationFees.map((feeId) => feeId);
|
||||
const selectedRegistrationDiscountsIds =
|
||||
formData.selectedRegistrationDiscounts.map((discountId) => discountId);
|
||||
const selectedTuitionFeesIds = formData.selectedTuitionFees.map(
|
||||
formDataRef.current.selectedRegistrationDiscounts.map(
|
||||
(discountId) => discountId
|
||||
);
|
||||
const selectedTuitionFeesIds = formDataRef.current.selectedTuitionFees.map(
|
||||
(feeId) => feeId
|
||||
);
|
||||
const selectedTuitionDiscountsIds = formData.selectedTuitionDiscounts.map(
|
||||
(discountId) => discountId
|
||||
);
|
||||
const selectedFileGroup = formData.selectedFileGroup;
|
||||
const selectedTuitionDiscountsIds =
|
||||
formDataRef.current.selectedTuitionDiscounts.map(
|
||||
(discountId) => discountId
|
||||
);
|
||||
const selectedFileGroup = formDataRef.current.selectedFileGroup;
|
||||
|
||||
const allFeesIds = [
|
||||
...selectedRegistrationFeesIds,
|
||||
@ -277,25 +348,31 @@ export default function CreateSubscriptionPage() {
|
||||
|
||||
const data = {
|
||||
student: {
|
||||
last_name: formData.studentLastName,
|
||||
first_name: formData.studentFirstName,
|
||||
level: formData.studentLevel,
|
||||
gender: formData.studentGender,
|
||||
guardians: formData.selectedGuardians.length
|
||||
? formData.selectedGuardians.map((guardianId) => ({ id: guardianId }))
|
||||
: formData.isExistingParentProfile
|
||||
last_name: formDataRef.current.studentLastName,
|
||||
first_name: formDataRef.current.studentFirstName,
|
||||
...(formDataRef.current.studentLevel && {
|
||||
level: formDataRef.current.studentLevel,
|
||||
}),
|
||||
...(formDataRef.current.studentGender && {
|
||||
gender: formDataRef.current.studentGender,
|
||||
}),
|
||||
guardians: formDataRef.current.selectedGuardians.length
|
||||
? formDataRef.current.selectedGuardians.map((guardianId) => ({
|
||||
id: guardianId,
|
||||
}))
|
||||
: formDataRef.current.isExistingParentProfile
|
||||
? [
|
||||
{
|
||||
profile_role_data: {
|
||||
establishment: selectedEstablishmentId,
|
||||
role_type: 2,
|
||||
is_active: false,
|
||||
profile: formData.existingProfileId,
|
||||
is_active: true,
|
||||
profile: formDataRef.current.existingProfileId,
|
||||
},
|
||||
last_name: formData.guardianLastName,
|
||||
first_name: formData.guardianFirstName,
|
||||
birth_date: formData.guardianBirthDate,
|
||||
phone: formData.guardianPhone,
|
||||
last_name: formDataRef.current.guardianLastName,
|
||||
first_name: formDataRef.current.guardianFirstName,
|
||||
birth_date: formDataRef.current.guardianBirthDate,
|
||||
phone: formDataRef.current.guardianPhone,
|
||||
},
|
||||
]
|
||||
: [
|
||||
@ -305,14 +382,14 @@ export default function CreateSubscriptionPage() {
|
||||
role_type: 2,
|
||||
is_active: false,
|
||||
profile_data: {
|
||||
email: formData.guardianEmail,
|
||||
email: formDataRef.current.guardianEmail,
|
||||
password: 'Provisoire01!',
|
||||
username: formData.guardianEmail,
|
||||
username: formDataRef.current.guardianEmail,
|
||||
},
|
||||
},
|
||||
last_name: formData.guardianLastName,
|
||||
first_name: formData.guardianFirstName,
|
||||
phone: formData.guardianPhone,
|
||||
last_name: formDataRef.current.guardianLastName,
|
||||
first_name: formDataRef.current.guardianFirstName,
|
||||
phone: formDataRef.current.guardianPhone,
|
||||
},
|
||||
],
|
||||
sibling: [],
|
||||
@ -321,7 +398,7 @@ export default function CreateSubscriptionPage() {
|
||||
discounts: allDiscountsIds,
|
||||
fileGroup: selectedFileGroup,
|
||||
establishment: selectedEstablishmentId,
|
||||
school_year: formData.schoolYear,
|
||||
school_year: formDataRef.current.schoolYear,
|
||||
};
|
||||
|
||||
setIsLoading(true);
|
||||
@ -590,9 +667,19 @@ export default function CreateSubscriptionPage() {
|
||||
return finalAmount.toFixed(2);
|
||||
};
|
||||
|
||||
if (isLoading === true) {
|
||||
return <Loader />; // Affichez le composant Loader
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="container mx-auto max-w-8xl space-y-12">
|
||||
<h1 className="text-2xl font-bold">Créer un dossier d'inscription</h1>
|
||||
{registerFormID ? (
|
||||
<h1 className="text-2xl font-bold">
|
||||
Modifier un dossier d'inscription
|
||||
</h1>
|
||||
) : (
|
||||
<h1 className="text-2xl font-bold">Créer un dossier d'inscription</h1>
|
||||
)}
|
||||
|
||||
{/* Sélection de l'année scolaire */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-5 gap-8">
|
||||
@ -1000,7 +1087,7 @@ export default function CreateSubscriptionPage() {
|
||||
{/* Bouton de soumission */}
|
||||
<div className="flex justify-end">
|
||||
<Button
|
||||
text="Créer le dossier"
|
||||
text={`${registerFormID ? 'Modifier' : 'Créer'} le dossier`}
|
||||
onClick={createRF}
|
||||
className={`px-6 py-2 rounded-md shadow ${
|
||||
isSubmitDisabled()
|
||||
|
||||
Reference in New Issue
Block a user