remove 'users' from navigation for role User

This commit is contained in:
Davor 2022-05-18 16:16:13 +02:00
parent db16fe5a37
commit 6fec33685e

View file

@ -5,17 +5,29 @@ import { useAuth } from 'src/services/auth';
import Gravatar from 'react-gravatar'; import Gravatar from 'react-gravatar';
import { Link, useLocation } from 'react-router-dom'; import { Link, useLocation } from 'react-router-dom';
import clsx from 'clsx'; import clsx from 'clsx';
import { User, UserRole } from 'src/services/users';
import { CurrentUserModal } from './components/CurrentUserModal'; import { CurrentUserModal } from './components/CurrentUserModal';
const navigation = [ const navigation = [
{ name: 'Dashboard', to: '/dashboard' }, { name: 'Dashboard', to: '/dashboard', roles: ['admin', 'user'] },
{ name: 'Users', to: '/users' }, { name: 'Users', to: '/users', roles: ['admin'] },
]; ];
function classNames(...classes: any[]) { function classNames(...classes: any[]) {
return classes.filter(Boolean).join(' '); return classes.filter(Boolean).join(' ');
} }
function availableNavigation(currentUser: User | null) {
if (currentUser === null) {
return [];
}
const userDashboardRole = currentUser.app_roles.find((appRole) => appRole.name?.toLowerCase() === 'dashboard')!.role;
return navigation.filter((item) =>
item.roles.includes(String(userDashboardRole != null ? userDashboardRole : UserRole.User)),
);
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface // eslint-disable-next-line @typescript-eslint/no-empty-interface
interface HeaderProps {} interface HeaderProps {}
@ -30,6 +42,8 @@ const Header: React.FC<HeaderProps> = () => {
}; };
const currentUserModalClose = () => setCurrentUserModal(false); const currentUserModalClose = () => setCurrentUserModal(false);
const navigationItems = availableNavigation(currentUser);
return ( return (
<> <>
<Disclosure as="nav" className="bg-white shadow relative z-10"> <Disclosure as="nav" className="bg-white shadow relative z-10">
@ -55,7 +69,7 @@ const Header: React.FC<HeaderProps> = () => {
</Link> </Link>
<div className="hidden sm:ml-6 sm:flex sm:space-x-8"> <div className="hidden sm:ml-6 sm:flex sm:space-x-8">
{/* Current: "border-primary-500 text-gray-900", Default: "border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700" */} {/* Current: "border-primary-500 text-gray-900", Default: "border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700" */}
{navigation.map((item) => ( {navigationItems.map((item) => (
<Link <Link
key={item.name} key={item.name}
to={item.to} to={item.to}