'use client'; import React, { useState, useEffect } from 'react'; import StructureManagement from '@/components/Structure/Configuration/StructureManagement'; import ScheduleManagement from '@/components/Structure/Planning/ScheduleManagement'; import FeesManagement from '@/components/Structure/Tarification/FeesManagement'; import DjangoCSRFToken from '@/components/DjangoCSRFToken'; import { useCsrfToken } from '@/context/CsrfContext'; import { ClassesProvider } from '@/context/ClassesContext'; import { createDatas, updateDatas, removeDatas, fetchSpecialities, fetchTeachers, fetchClasses, fetchRegistrationDiscounts, fetchTuitionDiscounts, fetchRegistrationFees, fetchTuitionFees, fetchRegistrationPaymentPlans, fetchTuitionPaymentPlans, fetchRegistrationPaymentModes, fetchTuitionPaymentModes, fetchEstablishmentCompetencies, } from '@/app/actions/schoolAction'; import { fetchProfiles } from '@/app/actions/authAction'; import SidebarTabs from '@/components/SidebarTabs'; import FilesGroupsManagement from '@/components/Structure/Files/FilesGroupsManagement'; import { fetchRegistrationSchoolFileMasters } from '@/app/actions/registerFileGroupAction'; import logger from '@/utils/logger'; import { useEstablishment } from '@/context/EstablishmentContext'; import { PlanningProvider, PlanningModes } from '@/context/PlanningContext'; import CompetenciesList from '@/components/Structure/Competencies/CompetenciesList'; export default function Page() { const [specialities, setSpecialities] = useState([]); const [classes, setClasses] = useState([]); const [teachers, setTeachers] = useState([]); const [registrationDiscounts, setRegistrationDiscounts] = useState([]); const [tuitionDiscounts, setTuitionDiscounts] = useState([]); const [registrationFees, setRegistrationFees] = useState([]); const [tuitionFees, setTuitionFees] = useState([]); const [fichiers, setFichiers] = useState([]); const [registrationPaymentPlans, setRegistrationPaymentPlans] = useState([]); const [tuitionPaymentPlans, setTuitionPaymentPlans] = useState([]); const [registrationPaymentModes, setRegistrationPaymentModes] = useState([]); const [tuitionPaymentModes, setTuitionPaymentModes] = useState([]); const [profiles, setProfiles] = useState([]); const [establishmentCompetencies, setEstablishmentCompetencies] = useState( [] ); const csrfToken = useCsrfToken(); const { selectedEstablishmentId } = useEstablishment(); useEffect(() => { if (selectedEstablishmentId) { // Fetch data for specialities handleSpecialities(); // Fetch data for teachers handleTeachers(); // Fetch data for classes handleClasses(); // Fetch data for registration discounts handleRegistrationDiscounts(); // Fetch data for tuition discounts handleTuitionDiscounts(); // Fetch data for registration fees handleRegistrationFees(); // Fetch data for tuition fees handleTuitionFees(); // Fetch data for registration file schoolFileTemplates fetchRegistrationSchoolFileMasters(selectedEstablishmentId) .then((data) => { setFichiers(data); }) .catch((error) => logger.error('Error fetching files:', error)); // Fetch data for registration payment plans handleRegistrationPaymentPlans(); // Fetch data for tuition payment plans handleTuitionPaymentPlans(); // Fetch data for registration payment modes handleRegistrationPaymentModes(); // Fetch data for tuition payment modes handleTuitionPaymentModes(); fetchProfiles() .then((data) => { setProfiles(data); }) .catch((error) => { logger.error('Error fetching profileRoles:', error); }); // Fetch data for establishment competencies handleEstablishmentCompetencies(); } }, [selectedEstablishmentId]); const handleEstablishmentCompetencies = (cycle = 1) => { fetchEstablishmentCompetencies(selectedEstablishmentId, cycle) .then((data) => { setEstablishmentCompetencies(data); }) .catch((error) => logger.error('Error fetching setEstablishmentCompetencies:', error) ); }; const handleSpecialities = () => { fetchSpecialities(selectedEstablishmentId) .then((data) => { setSpecialities(data); }) .catch((error) => logger.error('Error fetching specialities:', error)); }; const handleTeachers = () => { fetchTeachers(selectedEstablishmentId) .then((data) => { setTeachers(data); }) .catch((error) => logger.error('Error fetching teachers:', error)); }; const handleClasses = () => { fetchClasses(selectedEstablishmentId) .then((data) => { setClasses(data); }) .catch((error) => logger.error('Error fetching classes:', error)); }; const handleRegistrationDiscounts = () => { fetchRegistrationDiscounts(selectedEstablishmentId) .then((data) => { setRegistrationDiscounts(data); }) .catch((error) => logger.error('Error fetching registration discounts:', error) ); }; const handleTuitionDiscounts = () => { fetchTuitionDiscounts(selectedEstablishmentId) .then((data) => { setTuitionDiscounts(data); }) .catch((error) => logger.error('Error fetching tuition discounts:', error) ); }; const handleRegistrationFees = () => { fetchRegistrationFees(selectedEstablishmentId) .then((data) => { setRegistrationFees(data); }) .catch((error) => logger.error('Error fetching registration fees:', error) ); }; const handleTuitionFees = () => { fetchTuitionFees(selectedEstablishmentId) .then((data) => { setTuitionFees(data); }) .catch((error) => logger.error('Error fetching tuition fees', error)); }; const handleRegistrationPaymentPlans = () => { fetchRegistrationPaymentPlans(selectedEstablishmentId) .then((data) => { setRegistrationPaymentPlans(data); }) .catch((error) => logger.error('Error fetching registration payment plans:', error) ); }; const handleTuitionPaymentPlans = () => { fetchTuitionPaymentPlans(selectedEstablishmentId) .then((data) => { setTuitionPaymentPlans(data); }) .catch((error) => logger.error('Error fetching tuition payment plans:', error) ); }; const handleRegistrationPaymentModes = () => { fetchRegistrationPaymentModes(selectedEstablishmentId) .then((data) => { setRegistrationPaymentModes(data); }) .catch((error) => logger.error('Error fetching registration payment modes:', error) ); }; const handleTuitionPaymentModes = () => { fetchTuitionPaymentModes(selectedEstablishmentId) .then((data) => { setTuitionPaymentModes(data); }) .catch((error) => logger.error('Error fetching tuition payment modes:', error) ); }; const handleCreate = (url, newData, setDatas) => { return createDatas(url, newData, csrfToken) .then((data) => { setDatas((prevState) => [...prevState, data]); return data; }) .catch((error) => { logger.error('Error creating data:', error); throw error; }); }; const handleEdit = (url, id, updatedData, setDatas) => { return updateDatas(url, id, updatedData, csrfToken) .then((data) => { setDatas((prevState) => prevState.map((item) => (item.id === id ? data : item)) ); return data; }) .catch((error) => { logger.error('Error editing data:', error); throw error; }); }; const handleDelete = (url, id, setDatas) => { return removeDatas(url, id, csrfToken) .then((data) => { setDatas((prevState) => prevState.filter((item) => item.id !== id)); return data; }) .catch((error) => { logger.error('Error deleting data:', error); throw error; }); }; const handleUpdatePlanning = (url, planningId, updatedData) => { fetch(`${url}/${planningId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'X-CSRFToken': csrfToken, }, body: JSON.stringify(updatedData), credentials: 'include', }) .then((response) => response.json()) .then((data) => { logger.debug('Planning mis à jour avec succès :', data); //setDatas(data); }) .catch((error) => { logger.error('Erreur :', error); }); }; const tabs = [ { id: 'Configuration', label: 'Classes', content: (