mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
feat(FE-331): only show menu if user has the permission
This commit is contained in:
@@ -2,6 +2,7 @@ import Link from 'next/link';
|
|||||||
import Menu from '@/components/menu/Menu';
|
import Menu from '@/components/menu/Menu';
|
||||||
import { Icon } from '@iconify/react';
|
import { Icon } from '@iconify/react';
|
||||||
import { cn, isPathActive } from '@/lib/helper';
|
import { cn, isPathActive } from '@/lib/helper';
|
||||||
|
import { useAuth } from '@/services/hooks/useAuth';
|
||||||
|
|
||||||
export interface SidebarMenuItem {
|
export interface SidebarMenuItem {
|
||||||
type?: 'item' | 'title';
|
type?: 'item' | 'title';
|
||||||
@@ -9,6 +10,7 @@ export interface SidebarMenuItem {
|
|||||||
link: string;
|
link: string;
|
||||||
icon?: string;
|
icon?: string;
|
||||||
submenu?: SidebarMenuItem[];
|
submenu?: SidebarMenuItem[];
|
||||||
|
permission?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SidebarMenuItemProps {
|
interface SidebarMenuItemProps {
|
||||||
@@ -22,8 +24,17 @@ interface SidebarMenuProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const SidebarMenuItem = ({ item, activeLink }: SidebarMenuItemProps) => {
|
const SidebarMenuItem = ({ item, activeLink }: SidebarMenuItemProps) => {
|
||||||
|
const { permissionCheck } = useAuth();
|
||||||
const isItemActive = isPathActive(activeLink, item.link);
|
const isItemActive = isPathActive(activeLink, item.link);
|
||||||
|
|
||||||
|
const isUserPermitted = item.permission
|
||||||
|
? item.permission?.some((permissionName) => permissionCheck(permissionName))
|
||||||
|
: true;
|
||||||
|
|
||||||
|
if (!isUserPermitted) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const menuItemWithoutSubmenu = (
|
const menuItemWithoutSubmenu = (
|
||||||
<li>
|
<li>
|
||||||
<Link
|
<Link
|
||||||
@@ -78,13 +89,15 @@ const SidebarMenuItem = ({ item, activeLink }: SidebarMenuItemProps) => {
|
|||||||
const SidebarMenu = ({ menu, activeLink }: SidebarMenuProps) => {
|
const SidebarMenu = ({ menu, activeLink }: SidebarMenuProps) => {
|
||||||
return (
|
return (
|
||||||
<Menu>
|
<Menu>
|
||||||
{menu.map((menuItem, menuIdx) => (
|
{menu.map((menuItem, menuIdx) => {
|
||||||
<SidebarMenuItem
|
return (
|
||||||
key={menuIdx}
|
<SidebarMenuItem
|
||||||
item={menuItem}
|
key={menuIdx}
|
||||||
activeLink={activeLink}
|
item={menuItem}
|
||||||
/>
|
activeLink={activeLink}
|
||||||
))}
|
/>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</Menu>
|
</Menu>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user