From dd0884bbce6b6549f0f3fca991045f7170889710 Mon Sep 17 00:00:00 2001 From: Luc SORIGNET Date: Mon, 14 Apr 2025 18:53:35 +0200 Subject: [PATCH] fix: Correction des Protected Routes avec multi role --- Front-End/src/app/[locale]/admin/layout.js | 3 +- Front-End/src/app/[locale]/admin/page.js | 89 ++++++++++------------ Front-End/src/components/ProtectedRoute.js | 11 ++- 3 files changed, 49 insertions(+), 54 deletions(-) diff --git a/Front-End/src/app/[locale]/admin/layout.js b/Front-End/src/app/[locale]/admin/layout.js index b3c7f3f..d3315a4 100644 --- a/Front-End/src/app/[locale]/admin/layout.js +++ b/Front-End/src/app/[locale]/admin/layout.js @@ -59,7 +59,6 @@ export default function Layout({ "settings": { "id": "settings", "name": t('settings'), "url": FE_ADMIN_SETTINGS_URL, "icon": Settings } }; - const [isLoading, setIsLoading] = useState(false); const [isPopupVisible, setIsPopupVisible] = useState(false); const pathname = usePathname(); @@ -111,7 +110,7 @@ export default function Layout({ }, [pathname]); return ( - +
{/* Retirer la condition !isLoading car on gère déjà le chargement au début */} {/* Sidebar avec hauteur forcée */} diff --git a/Front-End/src/app/[locale]/admin/page.js b/Front-End/src/app/[locale]/admin/page.js index db781da..fd45b4b 100644 --- a/Front-End/src/app/[locale]/admin/page.js +++ b/Front-End/src/app/[locale]/admin/page.js @@ -9,7 +9,6 @@ import StatCard from '@/components/StatCard'; import logger from '@/utils/logger'; import { fetchRegisterForms } from '@/app/actions/subscriptionAction'; import { fetchUpcomingEvents } from '@/app/actions/planningAction'; -import { getSession } from 'next-auth/react'; import { useEstablishment } from '@/context/EstablishmentContext'; @@ -41,65 +40,55 @@ export default function DashboardPage() { const [classes, setClasses] = useState([]); - const [establishmentId, setEstablishmentId] = useState(null); const { selectedEstablishmentId } = useEstablishment(); + useEffect(() => { - getSession() - .then(session => { - setEstablishmentId(selectedEstablishmentId); + if (!selectedEstablishmentId) return; + + setIsLoading(true); // Début du chargement + + // Fetch des classes + fetchClasses(selectedEstablishmentId) + .then(data => { + setClasses(data); + logger.info('Classes fetched:', data); + + const nbMaxStudents = data.reduce((acc, classe) => acc + classe.number_of_students, 0); + const nbStudents = data.reduce((acc, classe) => acc + classe.students.length, 0); + setStructureCapacity(nbMaxStudents); + setTotalStudents(nbStudents); }) - .catch(err => { - logger.error('Error fetching session:', err); + .catch(error => { + logger.error('Error fetching classes:', error); }); - }, []); - useEffect(() => { - if (establishmentId) { - // Fetch data for classes - fetchClasses(establishmentId).then(data => { - setClasses(data); - logger.info('Classes fetched:', data); - const nbMaxStudents = data.reduce((acc, classe) => acc + classe.number_of_students, 0); - const nbStudents = data.reduce((acc, classe) => acc + classe.students.length, 0); - setStructureCapacity(nbMaxStudents); - setTotalStudents(nbStudents); - - }) - .catch(error => { - logger.error('Error fetching classes:', error); - }); - - fetchRegisterForms().then(data => { - logger.info('Pending registrations fetched:', data); - setPendingRegistration(data.count); - }) - .catch(error => { - logger.error('Error fetching pending registrations:', error); - }); - - fetchUpcomingEvents().then(data => { - setUpcomingEvents(data); - }).catch(error => { - logger.error('Error fetching upcoming events:', error); - }); - - // Simulation de chargement des données - setTimeout(() => { - setMonthlyStats({ - inscriptions: [150, 180, 210, 245], - completionRate: 78 - }); - setIsLoading(false); - }, 1000); - } - } - , [establishmentId]); + // Fetch des formulaires d'inscription + fetchRegisterForms() + .then(data => { + logger.info('Pending registrations fetched:', data); + setPendingRegistration(data.count); + }) + .catch(error => { + logger.error('Error fetching pending registrations:', error); + }); + // Fetch des événements à venir + fetchUpcomingEvents() + .then(data => { + setUpcomingEvents(data); + }) + .catch(error => { + logger.error('Error fetching upcoming events:', error); + }) + .finally(() => { + setIsLoading(false); // Fin du chargement + }); + }, [selectedEstablishmentId]); if (isLoading) return ; return ( -
+

{t('dashboard')}

{/* Statistiques principales */} diff --git a/Front-End/src/components/ProtectedRoute.js b/Front-End/src/components/ProtectedRoute.js index fa26ae6..41f3ca8 100644 --- a/Front-End/src/components/ProtectedRoute.js +++ b/Front-End/src/components/ProtectedRoute.js @@ -1,13 +1,20 @@ import { useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { useEstablishment } from '@/context/EstablishmentContext'; -import { FE_USERS_LOGIN_URL,getRedirectUrlFromRole } from '@/utils/Url'; +import { FE_USERS_LOGIN_URL, getRedirectUrlFromRole } from '@/utils/Url'; const ProtectedRoute = ({ children, requiredRight }) => { const { user, profileRole } = useEstablishment(); const router = useRouter(); - const hasRequiredRight = (profileRole === requiredRight); + let hasRequiredRight = false; + + if(requiredRight && Array.isArray(requiredRight) ){ + // Vérifier si l'utilisateur a le droit requis + hasRequiredRight = requiredRight.some((right) => profileRole === right); + }else{ + hasRequiredRight = (profileRole === requiredRight); + } // Vérifier si l'utilisateur a au moins un rôle correspondant au requiredRight useEffect(() => {