diff --git a/src/components/admin/NewAdminNavigation.tsx b/src/components/admin/NewAdminNavigation.tsx index 286514b71..43aa856ce 100644 --- a/src/components/admin/NewAdminNavigation.tsx +++ b/src/components/admin/NewAdminNavigation.tsx @@ -1,13 +1,16 @@ import Link from 'next/link'; +import { useRouter } from 'next/router'; import React from 'react'; import { Container, List, ListItem, ListItemIcon, ListItemText } from '@mui/material'; +import { UserContext } from 'src/contexts/userContext'; import AnalyserIcon from 'src/svg/analyser.svg'; import CreerIcon from 'src/svg/creer.svg'; import GererIcon from 'src/svg/gerer.svg'; import MediathequeIcon from 'src/svg/mediatheque.svg'; import PublierIcon from 'src/svg/publier.svg'; +import { UserType } from 'types/user.type'; interface NavItemProps { key?: number; @@ -22,6 +25,7 @@ interface Tab { path: string; label: string; Icon: React.ElementType; + rights: number[]; } const containerStyle = { @@ -47,33 +51,40 @@ const NavItem = ({ path, selected, onClick, Icon, primary }: NavItemProps) => ( ); export const NewAdminNavigation = () => { + const router = useRouter(); + const { user } = React.useContext(UserContext); + const [selectedTab, setSelectedTab] = React.useState('Créer'); const tabs: Tab[] = [ - { path: '/admin/newportal/create', label: 'Créer', Icon: CreerIcon }, - { path: '/admin/newportal/publish', label: 'Publier', Icon: PublierIcon }, - { path: '/admin/newportal/manage', label: 'Gérer', Icon: GererIcon }, - { path: '/admin/newportal/analyze', label: 'Analyser', Icon: AnalyserIcon }, - { path: '/admin/newportal/medialibrary', label: 'Médiathèque', Icon: MediathequeIcon }, + { path: '/admin/newportal/create', label: 'Créer', Icon: CreerIcon, rights: [UserType.ADMIN] }, + { path: '/admin/newportal/publish', label: 'Publier', Icon: PublierIcon, rights: [UserType.ADMIN] }, + { path: '/admin/newportal/manage', label: 'Gérer', Icon: GererIcon, rights: [UserType.ADMIN] }, + { path: '/admin/newportal/analyze', label: 'Analyser', Icon: AnalyserIcon, rights: [UserType.ADMIN, UserType.OBSERVATOR, UserType.MEDIATOR] }, + { path: '/admin/newportal/medialibrary', label: 'Médiathèque', Icon: MediathequeIcon, rights: [UserType.ADMIN, UserType.MEDIATOR] }, ]; const onTabClick = (label: string) => { setSelectedTab(label); }; - + if (!user) { + router.push('/'); + } return ( - {tabs?.map((tab, id) => ( - onTabClick(tab.label)} - Icon={tab.Icon} - primary={tab.label} - /> - ))} + {tabs + ?.filter((tab) => tab.rights.includes(user?.type ?? 10)) + .map((tab, id) => ( + onTabClick(tab.label)} + Icon={tab.Icon} + primary={tab.label} + /> + ))} ); diff --git a/src/pages/admin/newportal/create/index.tsx b/src/pages/admin/newportal/create/index.tsx index 31f48716a..a822bb607 100644 --- a/src/pages/admin/newportal/create/index.tsx +++ b/src/pages/admin/newportal/create/index.tsx @@ -1,13 +1,15 @@ import Link from 'next/link'; import { useRouter } from 'next/router'; -import React from 'react'; +import React, { useEffect } from 'react'; import { List, ListItem, ListItemIcon, ListItemText } from '@mui/material'; +import { UserContext } from 'src/contexts/userContext'; import { VillageContext } from 'src/contexts/villageContext'; import { useActivity } from 'src/hooks/useActivity'; import DoubleChevronRightIcon from 'src/svg/mdi-light_chevron-double-right.svg'; import { ActivityType } from 'types/activity.type'; +import { UserType } from 'types/user.type'; type Link = { name: string; @@ -21,8 +23,33 @@ interface NavItemProps { action?: (() => void) | undefined; } +const NavItem = ({ link, primary, action }: NavItemProps) => ( + + { + e.preventDefault(); + action(); + } + : undefined + } + > + + + + + + +); + const Creer = () => { const router = useRouter(); + const { user } = React.useContext(UserContext); + const { createNewActivity } = useActivity(); const { selectedPhase } = React.useContext(VillageContext); @@ -39,32 +66,18 @@ const Creer = () => { { name: 'Paramétrer l’hymne', link: 'https://' }, { name: 'Mixer l’hymne', link: 'https://' }, ]; + useEffect(() => { + if (user?.type === UserType.OBSERVATOR) { + router.push('/admin/newportal/analyze'); + } + }, [router, user]); + const hasAccess = user !== null && user.type in [UserType.MEDIATOR, UserType.ADMIN, UserType.SUPER_ADMIN]; - const NavItem = ({ link, primary, action }: NavItemProps) => ( - - { - e.preventDefault(); - action(); - } - : undefined - } - > - - - - - - - ); - - const renderTitle = () => { - return ( + if (!hasAccess) { + return

Vous n'avez pas accès à cette page, vous devez être médiateur ou admin.

; + } + return ( + <>

Créer

@@ -72,24 +85,13 @@ const Creer = () => { villages-mondes et accéder à la liste complète des utilisateurs.{' '}

- ); - }; - - const renderLinks = () => { - return ( - links && ( + {links && ( {links?.map((item, id) => ( ))} - ) - ); - }; - return ( - <> - {renderTitle()} - {renderLinks()} + )} ); }; diff --git a/src/pages/admin/newportal/manage/index.tsx b/src/pages/admin/newportal/manage/index.tsx index 6ffdc8a53..868324d77 100644 --- a/src/pages/admin/newportal/manage/index.tsx +++ b/src/pages/admin/newportal/manage/index.tsx @@ -10,22 +10,26 @@ import { UserType } from 'types/user.type'; type Link = { name: string; link: string; + rights: number[]; }; const Gerer = () => { const { user } = React.useContext(UserContext); - const hasAccess = user !== null && user.type in [UserType.MEDIATOR, UserType.ADMIN, UserType.SUPER_ADMIN]; + const hasAccess = user !== null && user.type in [UserType.ADMIN, UserType.SUPER_ADMIN]; if (!hasAccess) { - return

Vous n'avez pas accès à cette page, vous devez être médiateur, modérateur ou super admin.

; + return

Vous n'avez pas accès à cette page, vous devez être super admin.

; } - const links: Link[] = [ - { name: 'Les villages-mondes', link: '/admin/newportal/manage/villages' }, - { name: 'Les utilisateurs', link: '/admin/newportal/manage/users' }, - { name: 'Les consignes des activités', link: '/admin/newportal/manage/activities' }, - { name: 'Paramétrer 1Village', link: '/admin/newportal/manage/settings' }, - { name: "Les droits d'accès", link: '/admin/newportal/manage/access' }, + { name: 'Les villages-mondes', link: '/admin/newportal/manage/villages', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] }, + { name: 'Les utilisateurs', link: '/admin/newportal/manage/users', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] }, + { + name: 'Les consignes des activités', + link: '/admin/newportal/manage/activities', + rights: [UserType.SUPER_ADMIN], + }, + { name: 'Paramétrer 1Village', link: '/admin/newportal/manage/settings', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] }, + { name: "Les droits d'accès", link: '/admin/newportal/manage/access', rights: [UserType.SUPER_ADMIN] }, ]; return ( @@ -38,16 +42,18 @@ const Gerer = () => {

- {links?.map((item, id) => ( - - - - - - - - - ))} + {links + ?.filter((link) => link.rights.includes(user.type)) + .map((item, id) => ( + + + + + + + + + ))} ); diff --git a/src/pages/admin/newportal/manage/settings/index.tsx b/src/pages/admin/newportal/manage/settings/index.tsx index 3a2542bb4..5ba3553cf 100644 --- a/src/pages/admin/newportal/manage/settings/index.tsx +++ b/src/pages/admin/newportal/manage/settings/index.tsx @@ -11,21 +11,22 @@ import { UserType } from 'types/user.type'; type Link = { name: string; link: string; + rights: number[]; }; const Gerer = () => { const { user } = React.useContext(UserContext); - const hasAccess = user !== null && user.type in [UserType.MEDIATOR, UserType.ADMIN, UserType.SUPER_ADMIN]; + const hasAccess = user !== null && user.type in [UserType.ADMIN, UserType.SUPER_ADMIN]; if (!hasAccess) { - return

Vous n'avez pas accès à cette page, vous devez être médiateur, modérateur ou super admin.

; + return

Vous n'avez pas accès à cette page, vous devez être modérateur ou super admin.

; } const links: Link[] = [ - { name: 'Archiver', link: '/admin/newportal/manage/settings/archive' }, - { name: 'Présenatation de Pélico', link: '/admin/newportal/manage/settings/pelico' }, - { name: 'Paramétrer la home', link: '/admin/newportal/manage/settings/home' }, - { name: 'Paramétrer les phases', link: '/admin/newportal/manage/settings/phases' }, + { name: 'Archiver', link: '/admin/newportal/manage/settings/archive', rights: [UserType.SUPER_ADMIN] }, + { name: 'Présenatation de Pélico', link: '/admin/newportal/manage/settings/pelico', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] }, + { name: 'Paramétrer la home', link: '/admin/newportal/manage/settings/home', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] }, + { name: 'Paramétrer les phases', link: '/admin/newportal/manage/settings/phases', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] }, ]; return ( @@ -42,16 +43,18 @@ const Gerer = () => {

- {links?.map((item, id) => ( - - - - - - - - - ))} + {links + ?.filter((link) => link.rights.includes(user.type)) + .map((item, id) => ( + + + + + + + + + ))} ); diff --git a/src/pages/admin/newportal/publish/index.tsx b/src/pages/admin/newportal/publish/index.tsx index f0ef48b65..d87790958 100644 --- a/src/pages/admin/newportal/publish/index.tsx +++ b/src/pages/admin/newportal/publish/index.tsx @@ -1,13 +1,15 @@ import { useRouter } from 'next/router'; -import React from 'react'; +import React, { useEffect } from 'react'; import type { GridColDef, GridRowsProp } from '@mui/x-data-grid'; import { DataGrid } from '@mui/x-data-grid'; import { useGetActivities } from 'src/api/activities/activities.get'; import ActivityCardAdminList from 'src/components/activities/ActivityCard/activity-admin/ActivityCardAdminList'; +import { UserContext } from 'src/contexts/userContext'; import PelicoStar from 'src/svg/pelico/pelico_star.svg'; import PelicoVacances from 'src/svg/pelico/pelico_vacances.svg'; +import { UserType } from 'types/user.type'; const rows: GridRowsProp = [ // A row example of how it should look @@ -16,12 +18,19 @@ const rows: GridRowsProp = [ const columns: GridColDef[] = []; const Publier = () => { + const { user } = React.useContext(UserContext); const draftActivities = useGetActivities({ limit: 2, isDraft: true, isPelico: true }); const publishedActivities = useGetActivities({ limit: 2, isDraft: false, isPelico: true }); const router = useRouter(); + useEffect(() => { + if (user?.type === UserType.OBSERVATOR) { + router.push('/admin/newportal/analyze'); + } + }, [router, user]); if (draftActivities.isError) return

Error!

; if (draftActivities.isLoading || draftActivities.isIdle) return

Loading...

; + return (