mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
fix: Correction des Protected Routes avec multi role
This commit is contained in:
@ -59,7 +59,6 @@ export default function Layout({
|
|||||||
"settings": { "id": "settings", "name": t('settings'), "url": FE_ADMIN_SETTINGS_URL, "icon": Settings }
|
"settings": { "id": "settings", "name": t('settings'), "url": FE_ADMIN_SETTINGS_URL, "icon": Settings }
|
||||||
};
|
};
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
|
||||||
const [isPopupVisible, setIsPopupVisible] = useState(false);
|
const [isPopupVisible, setIsPopupVisible] = useState(false);
|
||||||
|
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
@ -111,7 +110,7 @@ export default function Layout({
|
|||||||
}, [pathname]);
|
}, [pathname]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ProtectedRoute requiredRight={RIGHTS.ADMIN}>
|
<ProtectedRoute requiredRight={[RIGHTS.ADMIN, RIGHTS.TEACHER]}>
|
||||||
<div className="flex min-h-screen bg-gray-50 relative">
|
<div className="flex min-h-screen bg-gray-50 relative">
|
||||||
{/* Retirer la condition !isLoading car on gère déjà le chargement au début */}
|
{/* Retirer la condition !isLoading car on gère déjà le chargement au début */}
|
||||||
{/* Sidebar avec hauteur forcée */}
|
{/* Sidebar avec hauteur forcée */}
|
||||||
|
|||||||
@ -9,7 +9,6 @@ import StatCard from '@/components/StatCard';
|
|||||||
import logger from '@/utils/logger';
|
import logger from '@/utils/logger';
|
||||||
import { fetchRegisterForms } from '@/app/actions/subscriptionAction';
|
import { fetchRegisterForms } from '@/app/actions/subscriptionAction';
|
||||||
import { fetchUpcomingEvents } from '@/app/actions/planningAction';
|
import { fetchUpcomingEvents } from '@/app/actions/planningAction';
|
||||||
import { getSession } from 'next-auth/react';
|
|
||||||
import { useEstablishment } from '@/context/EstablishmentContext';
|
import { useEstablishment } from '@/context/EstablishmentContext';
|
||||||
|
|
||||||
|
|
||||||
@ -41,35 +40,31 @@ export default function DashboardPage() {
|
|||||||
|
|
||||||
|
|
||||||
const [classes, setClasses] = useState([]);
|
const [classes, setClasses] = useState([]);
|
||||||
const [establishmentId, setEstablishmentId] = useState(null);
|
|
||||||
const { selectedEstablishmentId } = useEstablishment();
|
const { selectedEstablishmentId } = useEstablishment();
|
||||||
useEffect(() => {
|
|
||||||
getSession()
|
|
||||||
.then(session => {
|
|
||||||
setEstablishmentId(selectedEstablishmentId);
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
logger.error('Error fetching session:', err);
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (establishmentId) {
|
if (!selectedEstablishmentId) return;
|
||||||
// Fetch data for classes
|
|
||||||
fetchClasses(establishmentId).then(data => {
|
setIsLoading(true); // Début du chargement
|
||||||
|
|
||||||
|
// Fetch des classes
|
||||||
|
fetchClasses(selectedEstablishmentId)
|
||||||
|
.then(data => {
|
||||||
setClasses(data);
|
setClasses(data);
|
||||||
logger.info('Classes fetched:', data);
|
logger.info('Classes fetched:', data);
|
||||||
|
|
||||||
const nbMaxStudents = data.reduce((acc, classe) => acc + classe.number_of_students, 0);
|
const nbMaxStudents = data.reduce((acc, classe) => acc + classe.number_of_students, 0);
|
||||||
const nbStudents = data.reduce((acc, classe) => acc + classe.students.length, 0);
|
const nbStudents = data.reduce((acc, classe) => acc + classe.students.length, 0);
|
||||||
setStructureCapacity(nbMaxStudents);
|
setStructureCapacity(nbMaxStudents);
|
||||||
setTotalStudents(nbStudents);
|
setTotalStudents(nbStudents);
|
||||||
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
logger.error('Error fetching classes:', error);
|
logger.error('Error fetching classes:', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
fetchRegisterForms().then(data => {
|
// Fetch des formulaires d'inscription
|
||||||
|
fetchRegisterForms()
|
||||||
|
.then(data => {
|
||||||
logger.info('Pending registrations fetched:', data);
|
logger.info('Pending registrations fetched:', data);
|
||||||
setPendingRegistration(data.count);
|
setPendingRegistration(data.count);
|
||||||
})
|
})
|
||||||
@ -77,29 +72,23 @@ export default function DashboardPage() {
|
|||||||
logger.error('Error fetching pending registrations:', error);
|
logger.error('Error fetching pending registrations:', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
fetchUpcomingEvents().then(data => {
|
// Fetch des événements à venir
|
||||||
|
fetchUpcomingEvents()
|
||||||
|
.then(data => {
|
||||||
setUpcomingEvents(data);
|
setUpcomingEvents(data);
|
||||||
}).catch(error => {
|
})
|
||||||
|
.catch(error => {
|
||||||
logger.error('Error fetching upcoming events:', error);
|
logger.error('Error fetching upcoming events:', error);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
setIsLoading(false); // Fin du chargement
|
||||||
});
|
});
|
||||||
|
}, [selectedEstablishmentId]);
|
||||||
// Simulation de chargement des données
|
|
||||||
setTimeout(() => {
|
|
||||||
setMonthlyStats({
|
|
||||||
inscriptions: [150, 180, 210, 245],
|
|
||||||
completionRate: 78
|
|
||||||
});
|
|
||||||
setIsLoading(false);
|
|
||||||
}, 1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
, [establishmentId]);
|
|
||||||
|
|
||||||
|
|
||||||
if (isLoading) return <Loader />;
|
if (isLoading) return <Loader />;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="p-6">
|
<div key={selectedEstablishmentId} className="p-6">
|
||||||
<h1 className="text-2xl font-bold mb-6">{t('dashboard')}</h1>
|
<h1 className="text-2xl font-bold mb-6">{t('dashboard')}</h1>
|
||||||
|
|
||||||
{/* Statistiques principales */}
|
{/* Statistiques principales */}
|
||||||
|
|||||||
@ -1,13 +1,20 @@
|
|||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useEstablishment } from '@/context/EstablishmentContext';
|
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 ProtectedRoute = ({ children, requiredRight }) => {
|
||||||
|
|
||||||
const { user, profileRole } = useEstablishment();
|
const { user, profileRole } = useEstablishment();
|
||||||
const router = useRouter();
|
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
|
// Vérifier si l'utilisateur a au moins un rôle correspondant au requiredRight
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user