refactor admin tabs

This commit is contained in:
Davor 2022-05-18 18:13:22 +02:00
parent 6fec33685e
commit 79ec69a8f2

View file

@ -5,27 +5,23 @@ 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', roles: ['admin', 'user'] }, { name: 'Dashboard', to: '/dashboard', requiresAdmin: false },
{ name: 'Users', to: '/users', roles: ['admin'] }, { name: 'Users', to: '/users', requiresAdmin: true },
]; ];
function classNames(...classes: any[]) { function classNames(...classes: any[]) {
return classes.filter(Boolean).join(' '); return classes.filter(Boolean).join(' ');
} }
function availableNavigation(currentUser: User | null) { function filterNavigationByDashboardRole(isAdmin: boolean) {
if (currentUser === null) { if (isAdmin) {
return []; return navigation;
} }
const userDashboardRole = currentUser.app_roles.find((appRole) => appRole.name?.toLowerCase() === 'dashboard')!.role;
return navigation.filter((item) => return navigation.filter((item) => !item.requiresAdmin);
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
@ -33,7 +29,7 @@ interface HeaderProps {}
const Header: React.FC<HeaderProps> = () => { const Header: React.FC<HeaderProps> = () => {
const [currentUserModal, setCurrentUserModal] = useState(false); const [currentUserModal, setCurrentUserModal] = useState(false);
const { logOut, currentUser } = useAuth(); const { logOut, currentUser, isAdmin } = useAuth();
const { pathname } = useLocation(); const { pathname } = useLocation();
@ -42,7 +38,7 @@ const Header: React.FC<HeaderProps> = () => {
}; };
const currentUserModalClose = () => setCurrentUserModal(false); const currentUserModalClose = () => setCurrentUserModal(false);
const navigationItems = availableNavigation(currentUser); const navigationItems = filterNavigationByDashboardRole(isAdmin);
return ( return (
<> <>