mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 16:03:21 +00:00
refactor: Création de nouveaux composants / update formulaire de
création de classe (#2)
This commit is contained in:
140
Front-End/src/context/ClassesContext.js
Normal file
140
Front-End/src/context/ClassesContext.js
Normal file
@ -0,0 +1,140 @@
|
||||
import React, { createContext, useContext } from 'react';
|
||||
|
||||
const ClassesContext = createContext();
|
||||
|
||||
export const useClasses = () => useContext(ClassesContext);
|
||||
|
||||
export const ClassesProvider = ({ children }) => {
|
||||
const currentYear = new Date().getFullYear();
|
||||
|
||||
const schoolYears = [
|
||||
{ value: '', label: 'Sélectionner une période' },
|
||||
{ value: `${currentYear}-${currentYear + 1}`, label: `${currentYear}-${currentYear + 1}` },
|
||||
{ value: `${currentYear + 1}-${currentYear + 2}`, label: `${currentYear + 1}-${currentYear + 2}` },
|
||||
{ value: `${currentYear + 2}-${currentYear + 3}`, label: `${currentYear + 2}-${currentYear + 3}` },
|
||||
];
|
||||
|
||||
const niveauxPremierCycle = [
|
||||
{ id: 1, name: 'TPS', age: 2 },
|
||||
{ id: 2, name: 'PS', age: 3 },
|
||||
{ id: 3, name: 'MS', age: 4 },
|
||||
{ id: 4, name: 'GS', age: 5 },
|
||||
];
|
||||
|
||||
const niveauxSecondCycle = [
|
||||
{ id: 5, name: 'CP', age: 6 },
|
||||
{ id: 6, name: 'CE1', age: 7 },
|
||||
{ id: 7, name: 'CE2', age: 8 },
|
||||
];
|
||||
|
||||
const niveauxTroisiemeCycle = [
|
||||
{ id: 8, name: 'CM1', age: 9 },
|
||||
{ id: 9, name: 'CM2', age: 10 },
|
||||
];
|
||||
|
||||
const allNiveaux = [...niveauxPremierCycle, ...niveauxSecondCycle, ...niveauxTroisiemeCycle];
|
||||
|
||||
const typeEmploiDuTemps = [
|
||||
{ id: 1, label: 'Annuel' },
|
||||
{ id: 2, label: 'Semestriel' },
|
||||
{ id: 3, label: 'Trimestriel' },
|
||||
];
|
||||
|
||||
const selectedDays = {
|
||||
1: 'lundi',
|
||||
2: 'mardi',
|
||||
3: 'mercredi',
|
||||
4: 'jeudi',
|
||||
5: 'vendredi',
|
||||
6: 'samedi',
|
||||
7: 'dimanche'
|
||||
};
|
||||
|
||||
const getNiveauxLabels = (niveaux) => {
|
||||
return niveaux.map(niveauId => {
|
||||
const niveau = allNiveaux.find(n => n.id === niveauId);
|
||||
return niveau ? niveau.name : niveauId;
|
||||
});
|
||||
};
|
||||
|
||||
const generateAgeToNiveaux = (minAge, maxAge) => {
|
||||
if (minAge === null || isNaN(minAge)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return allNiveaux
|
||||
.filter(({ age }) => age === minAge || (age >= minAge && (maxAge !== null && !isNaN(maxAge) && age < maxAge)))
|
||||
.map(({ id }) => id);
|
||||
};
|
||||
|
||||
const updatePlanning = (formData) => {
|
||||
|
||||
let updatedPlanning = { ...formData.planning };
|
||||
|
||||
const emploiDuTemps = formData.jours_ouverture.reduce((acc, dayId) => {
|
||||
const dayName = selectedDays[dayId];
|
||||
if (dayName) {
|
||||
acc[dayName] = [];
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
if (formData.planning_type === 1) {
|
||||
updatedPlanning = {
|
||||
type: 1,
|
||||
plageHoraire: formData.plage_horaire,
|
||||
joursOuverture: formData.jours_ouverture,
|
||||
emploiDuTemps
|
||||
};
|
||||
} else if (formData.planning_type === 2) {
|
||||
updatedPlanning = {
|
||||
type: 2,
|
||||
plageHoraire: formData.plage_horaire,
|
||||
joursOuverture: formData.jours_ouverture,
|
||||
emploiDuTemps: {
|
||||
S1: {
|
||||
DateDebut: formData.date_debut_semestre_1,
|
||||
DateFin: formData.date_fin_semestre_1,
|
||||
...emploiDuTemps
|
||||
},
|
||||
S2: {
|
||||
DateDebut: formData.date_debut_semestre_2,
|
||||
DateFin: formData.date_fin_semestre_2,
|
||||
...emploiDuTemps
|
||||
}
|
||||
}
|
||||
};
|
||||
} else if (formData.planning_type === 3) {
|
||||
updatedPlanning = {
|
||||
type: 3,
|
||||
plageHoraire: formData.plage_horaire,
|
||||
joursOuverture: formData.jours_ouverture,
|
||||
emploiDuTemps: {
|
||||
T1: {
|
||||
DateDebut: formData.date_debut_trimestre_1,
|
||||
DateFin: formData.date_fin_trimestre_1,
|
||||
...emploiDuTemps
|
||||
},
|
||||
T2: {
|
||||
DateDebut: formData.date_debut_trimestre_2,
|
||||
DateFin: formData.date_fin_trimestre_2,
|
||||
...emploiDuTemps
|
||||
},
|
||||
T3: {
|
||||
DateDebut: formData.date_debut_trimestre_3,
|
||||
DateFin: formData.date_fin_trimestre_3,
|
||||
...emploiDuTemps
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return updatedPlanning;
|
||||
};
|
||||
|
||||
return (
|
||||
<ClassesContext.Provider value={{ schoolYears, getNiveauxLabels, generateAgeToNiveaux, niveauxPremierCycle, niveauxSecondCycle, niveauxTroisiemeCycle, typeEmploiDuTemps, updatePlanning }}>
|
||||
{children}
|
||||
</ClassesContext.Provider>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user