All files / src/components/Navbar NavLinkList.tsx

100% Statements 37/37
100% Branches 28/28
100% Functions 18/18
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                        3x 3x     3x 3x 3x   3x 3x   3x 1x 1x                   24x 24x 18x   24x 24x   24x   3x             4x 4x                                         2x 2x                                   1x 1x                                                                       1x 1x                                                                     2x                              
import Button from '@mui/material/Button';
import List from '@mui/material/List';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import Divider from '@mui/material/Divider';
import { useAuthStore } from '../../stores/useAuthStore';
import { useCartStore } from '../../stores/useCartStore';
import { useNavigate } from 'react-router-dom';
import ActiveLink from '../ActiveLink';
import { navItems } from './navItems';
import { useTranslation } from 'react-i18next';
import { logout } from '../../utils/authHelper';
 
interface Props {
  isMobile: boolean;
  toggleDrawer?: () => void;
}
 
export function NavLinkList({ isMobile, toggleDrawer }: Props) {
  const { t } = useTranslation();
  const navigate = useNavigate();
 
  // Stores & helpers pour le logout
  const clearAuthToken = useAuthStore((s) => s.clearToken);
  const clearGuestCartId = useCartStore((s) => s.setGuestCartId);
  const loadCart = useCartStore((s) => s.loadCart);
 
  const authToken = useAuthStore((s) => s.authToken);
  const role      = useAuthStore((s) => s.role);
 
  const handleLogout = async () => {
    toggleDrawer?.();
    await logout(
      clearAuthToken,
      clearGuestCartId,
      loadCart,
      navigate,
      '/login'
    );
  };
 
  // Filtrer les items 
  const publicItems = navItems.filter(i => i.group==='public');
  const loginItems = navItems.filter(i => i.group==='login' && !authToken);
  const forgotPasswordItem = navItems.find(i => i.group === 'password' && !authToken);
 
  const dashboardItems = navItems.filter(i => i.group==='dashboard' && authToken && (i.role === 'all' || i.role === role));
  const authItems = navItems.filter(i => i.group==='auth' && authToken && (i.role === 'all' || i.role === role));
 
  const logoutItem = navItems.find(i => i.group === 'logout' && authToken);
 
  return (
    <List>
      {isMobile ? (
        // ────────── MOBILE ──────────
        <>
          {/* 1) Public */}
          {publicItems.map((item) => {
            const Icon = item.icon;
            return (
              <ListItemButton
                key={item.key}
                component={ActiveLink}
                to={item.href!}
                onClick={toggleDrawer}
                aria-label={t(`navbar.${item.key}`)}
              >
                <ListItemIcon sx={{ minWidth: 40 }}>
                  <Icon fontSize="small" />
                </ListItemIcon>
                <ListItemText primary={t(`navbar.${item.key}`)} />
              </ListItemButton>
            );
          })}
 
          <Divider sx={{ my: 1 }} />
 
          {/* 2) Login / Signup, seulement si non connecté */}
          {!authToken &&
            loginItems.map((item) => {
              const Icon = item.icon;
              return (
                <ListItemButton
                  key={item.key}
                  component={ActiveLink}
                  to={item.href!}
                  onClick={toggleDrawer}
                  aria-label={t(`navbar.${item.key}`)}
                >
                  <ListItemIcon sx={{ minWidth: 40 }}><Icon fontSize="small" /></ListItemIcon>
                  <ListItemText primary={t(`navbar.${item.key}`)} />
                </ListItemButton>
              );
            })
          }
 
          {/* 3) Dashboard, User, Employee, Admin */}
          {authToken && 
            dashboardItems.map((item) => {
              const Icon = item.icon;
              return (
                <ListItemButton
                  key={item.key}
                  component={ActiveLink}
                  to={item.href!}
                  onClick={toggleDrawer}
                  aria-label={t(`navbar.${item.key}`)}
                >
                  <ListItemIcon sx={{ minWidth: 40 }}>
                    <Icon fontSize="small" />
                  </ListItemIcon>
                  <ListItemText primary={t(`navbar.${item.key}`)} />
                </ListItemButton>
              );
            })
          }
 
          <Divider sx={{ my: 1 }} />
 
          {/* ForgotPassword */}
          {!authToken && forgotPasswordItem && (
            <ListItemButton
              key={forgotPasswordItem.key}
              component={ActiveLink}
              to={forgotPasswordItem.href!}
              onClick={toggleDrawer}
              aria-label={t(`navbar.${forgotPasswordItem.key}`)}
            >
              <ListItemIcon sx={{ minWidth: 40 }}><forgotPasswordItem.icon fontSize="small" /></ListItemIcon>
              <ListItemText primary={t(`navbar.${forgotPasswordItem.key}`)} />
            </ListItemButton>
          )}
 
          {/* 2) Auth */}
          {authToken &&
            authItems.map((item) => {
              const Icon = item.icon;
              return (
                <ListItemButton
                  key={item.key}
                  component={ActiveLink}
                  to={item.href!}
                  onClick={toggleDrawer}
                  aria-label={t(`navbar.${item.key}`)}
                >
                  <ListItemIcon sx={{ minWidth: 40 }}>
                    <Icon fontSize="small" />
                  </ListItemIcon>
                  <ListItemText primary={t(`navbar.${item.key}`)} />
                </ListItemButton>
              );
            })
          }
 
          {/* 3) Logout */}
          {authToken && logoutItem && (
            <>
              <Divider sx={{ my: 1 }} />
 
              <ListItemButton onClick={handleLogout} aria-label={t(`navbar.${logoutItem.key}`)}>
                <ListItemIcon sx={{ minWidth: 40 }}>
                  <logoutItem.icon fontSize="small" />
                </ListItemIcon>
                <ListItemText primary={t(`navbar.${logoutItem.key}`)} />
              </ListItemButton>
            </>
          )}
        </>
      ) : (
        // ────────── DESKTOP ──────────
        <>
          {publicItems.map((item) => (
            <Button
              key={item.key}
              component={ActiveLink}
              to={item.href!}
              color="inherit"
              aria-label={t(`navbar.${item.key}`)}
            >
              {t(`navbar.${item.key}`)}
            </Button>
          ))}
        </>
      )}
    </List>
  );
}