diff --git a/src/layouts/dashboardLayouts/sidebar/SidebarList.jsx b/src/layouts/dashboardLayouts/sidebar/SidebarList.jsx index a030b52..265da6c 100644 --- a/src/layouts/dashboardLayouts/sidebar/SidebarList.jsx +++ b/src/layouts/dashboardLayouts/sidebar/SidebarList.jsx @@ -1,5 +1,5 @@ import {Divider, List} from "@mui/material"; -import {useEffect, useReducer} from "react"; +import {Fragment, useEffect, useReducer, useRef, useState} from "react"; import SidebarListItem from "./SidebarListItem"; import sidebarMenu from "@/core/data/sidebarMenu"; import {useRouter} from "next/router"; @@ -10,18 +10,27 @@ function reducer(state, action) { case "COLLAPSE_MENU": return state.map((itemsArr) => itemsArr.map((item) => - action.key == item.key - ? {...item, showSubItem: !item.showSubItem} - : item + action.key === item.key ? { ...item, showSubItem: !item.showSubItem } : item ) ); case "SELECTED": return state.map((itemsArr) => - itemsArr.map((item) => - action.route == item.route - ? {...item, selected: true} - : {...item, selected: false} - ) + itemsArr.map((item) => { + return item.type === "page" + ? { ...item, selected: action.route === item.route } + : item.subItem && Array.isArray(item.subItem) + ? { + ...item, + subItem: item.subItem.map((subitem) => ({ + ...subitem, + selected: subitem.route === action.route, + })), + showSubItem: item.subItem.some( + (subitem) => subitem.selected + ), + } + : item; + }) ); default: throw new Error(); @@ -32,26 +41,39 @@ export default function SidebarList({handleDrawerToggle}) { const [itemMenu, dispatch] = useReducer(reducer, sidebarMenu); const {user} = useUser(); const router = useRouter(); + const [selectedKey, setSelectedKey] = useState(null); useEffect(() => { dispatch({type: "SELECTED", route: router.pathname}); + setSelectedKey(router.pathname); }, [router.pathname]); - const filteredItemMenu = itemMenu[0].filter( - (item) => user.permissions.includes(item.permission) || item.permission === "all" - ); + + useEffect(() => { + selectedKey && document.querySelector(`[data-route="${selectedKey}"]`)?.scrollIntoView({ + behavior: "smooth", + block: "center" + }); + }, [selectedKey]); return ( - - {filteredItemMenu.map((item, index) => ( - + + {itemMenu.map((itemArr, index) => ( + + {itemArr.map((item) => + + {(user?.permissions?.includes(item.permission) || item.permission === "all") && + } + + )} + + ))} - {filteredItemMenu.length > 0 && } + ); } diff --git a/src/layouts/dashboardLayouts/sidebar/SidebarListItem.jsx b/src/layouts/dashboardLayouts/sidebar/SidebarListItem.jsx index ebeb14c..5e9039b 100644 --- a/src/layouts/dashboardLayouts/sidebar/SidebarListItem.jsx +++ b/src/layouts/dashboardLayouts/sidebar/SidebarListItem.jsx @@ -9,23 +9,27 @@ import useNotification from "@/lib/app/hooks/useNotification"; const SidebarListItem = ({item, dispatch, handleDrawerToggle}) => { const t = useTranslations(); - const {notification_count} = useNotification() + const { notification_count } = useNotification(); + const hasSubItems = item.type === "menu" && item.subItem && item.subItem.length > 0; + const renderBadge = () => { + return !hasSubItems ? ( + + 0 ? notification_count[item.name] : 0} + color="error" + variant="standard" + anchorOrigin={{ + vertical: "top", + horizontal: "right", + }} + /> + + ) : null; + }; return ( - - - - - }> + <> + { }, })} onClick={() => { - if (item.type == "menu") { - dispatch({type: "COLLAPSE_MENU", key: item.key}); + if (hasSubItems) { + dispatch({ type: "COLLAPSE_MENU", key: item.key }); } - handleDrawerToggle(); }} sx={{ minHeight: 48, @@ -66,9 +69,7 @@ const SidebarListItem = ({item, dispatch, handleDrawerToggle}) => { } /> - - {item.type == "menu" && - (item.showSubItem ? : )} + {hasSubItems && (item.showSubItem ? : )} {item.subItem && ( @@ -77,7 +78,7 @@ const SidebarListItem = ({item, dispatch, handleDrawerToggle}) => { handleDrawerToggle={handleDrawerToggle} /> )} - + ); }; diff --git a/src/layouts/dashboardLayouts/sidebar/SidebarListSubItem.jsx b/src/layouts/dashboardLayouts/sidebar/SidebarListSubItem.jsx index 3407081..c94d797 100644 --- a/src/layouts/dashboardLayouts/sidebar/SidebarListSubItem.jsx +++ b/src/layouts/dashboardLayouts/sidebar/SidebarListSubItem.jsx @@ -1,18 +1,43 @@ import {NextLinkComposed} from "@/core/components/LinkRouting"; -import InboxIcon from "@mui/icons-material/MoveToInbox"; -import {Collapse, List, ListItemButton, ListItemIcon, ListItemText,} from "@mui/material"; +import { + Badge, + Collapse, + IconButton, + List, + ListItem, + ListItemButton, + ListItemIcon, + ListItemText, + Typography, +} from "@mui/material"; import {useTranslations} from "next-intl"; -import {Fragment} from "react"; +import useNotification from "@/lib/app/hooks/useNotification"; -const SidebarListSubItem = ({item, handleDrawerToggle}) => { +const SidebarListSubItem = ({item, handleDrawerToggle }) => { const t = useTranslations(); + const {notification_count} = useNotification(); + const renderBadge = (subitem) => ( + + 0 ? notification_count[subitem.name] : 0} + color="error" + variant="standard" + anchorOrigin={{ + vertical: "top", + horizontal: "right", + }} + /> + + ); return ( - - - {item.subItem.map((subitem, index) => ( - + + + + {item.subItem.map((subitem) => ( + { sx={{ minHeight: 48, }} - onClick={(event) => { - if (item.type == "menu") { - dispatch({type: "COLLAPSE_MENU", key: item.key}); - } - handleDrawerToggle(); - }} > { pr: 2, }} > - + {subitem.icon} - + + {t(subitem.secondary)} + + ) : null + }/> - + ))} + ); }; diff --git a/src/layouts/dashboardLayouts/sidebar/index.jsx b/src/layouts/dashboardLayouts/sidebar/index.jsx index 2498dfd..bdab3a2 100644 --- a/src/layouts/dashboardLayouts/sidebar/index.jsx +++ b/src/layouts/dashboardLayouts/sidebar/index.jsx @@ -21,6 +21,7 @@ const Sidebar = (props) => { "& .MuiDrawer-paper": { boxSizing: "border-box", width: props.drawerWidth, + overflow: "hidden", }, }} > @@ -33,6 +34,7 @@ const Sidebar = (props) => { "& .MuiDrawer-paper": { boxSizing: "border-box", width: props.drawerWidth, + overflow: "hidden", }, }} open