Skip to content
Snippets Groups Projects
Router.tsx 1.71 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bastien DUMONT's avatar
    Bastien DUMONT committed
    import React, { useContext } from 'react'
    
    import { Navigate, Route, Routes } from 'react-router-dom'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { UserContext } from '../../hooks/userContext'
    import Consents from '../Consents/Consents'
    import Login from '../Login/Login'
    import Newsletter from '../Newsletter/Newsletter'
    import Popups from '../Popups/Popups'
    import Prices from '../Prices/Prices'
    
    
    export const links: Record<
      string,
      { label: string; path: string; adminOnly?: boolean }
    > = {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      newsletter: {
        label: 'Newsletter',
        path: '/newsletter',
      },
      popups: {
        label: 'Pop-ups',
        path: '/popups',
      },
      prices: {
        label: 'Prix',
        path: '/prices',
      },
      consents: {
        label: 'Consentements',
        path: '/consents',
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    export const routes = Object.keys(links).map(key => ({
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      label: links[key].label,
      path: links[key].path,
    
      adminOnly: links[key].adminOnly,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    }))
    
    const Router: React.FC = () => {
      const { user } = useContext(UserContext)
    
      return (
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          {user ? (
            <>
    
              <Route path={links.newsletter.path} element={<Newsletter />} />
              <Route path={links.prices.path} element={<Prices />} />
              <Route path={links.popups.path} element={<Popups />} />
    
              {user.isAdmin && (
    
                <Route path={links.consents.path} element={<Consents />} />
    
              <Route
                path="*"
                element={<Navigate replace to={links.newsletter.path} />}
              />
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            </>
          ) : (
            <>
    
              <Route path="/login" element={<Login />} />
              <Route path="*" element={<Navigate replace to="/login" />} />
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            </>
          )}
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      )
    }
    
    export default Router