From 3b7f9c8b2a57a8034323e7fe51795f76106420e7 Mon Sep 17 00:00:00 2001 From: Yasiu1376 Date: Mon, 6 Nov 2023 11:35:27 +0330 Subject: [PATCH] CFE-12 implement of sidebar --- .../layouts/Dashboard/Sidebar/SidebarList.jsx | 34 +++++++++++++++---- .../Dashboard/Sidebar/SidebarListItem.jsx | 32 ++++++++--------- .../Dashboard/Sidebar/SidebarListSubItem.jsx | 7 ++-- .../layouts/Dashboard/Sidebar/index.jsx | 2 ++ 4 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/components/layouts/Dashboard/Sidebar/SidebarList.jsx b/src/components/layouts/Dashboard/Sidebar/SidebarList.jsx index 1e570f5..265da6c 100644 --- a/src/components/layouts/Dashboard/Sidebar/SidebarList.jsx +++ b/src/components/layouts/Dashboard/Sidebar/SidebarList.jsx @@ -1,5 +1,5 @@ import {Divider, List} from "@mui/material"; -import {Fragment, 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"; @@ -15,10 +15,22 @@ function reducer(state, action) { ); case "SELECTED": return state.map((itemsArr) => - itemsArr.map((item) => ({ - ...item, - selected: item.type === "page" && action.route === item.route, - })) + 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(); @@ -29,13 +41,23 @@ 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]); + + useEffect(() => { + selectedKey && document.querySelector(`[data-route="${selectedKey}"]`)?.scrollIntoView({ + behavior: "smooth", + block: "center" + }); + }, [selectedKey]); + return ( - + {itemMenu.map((itemArr, index) => ( {itemArr.map((item) => diff --git a/src/components/layouts/Dashboard/Sidebar/SidebarListItem.jsx b/src/components/layouts/Dashboard/Sidebar/SidebarListItem.jsx index 2195ed3..e9657a9 100644 --- a/src/components/layouts/Dashboard/Sidebar/SidebarListItem.jsx +++ b/src/components/layouts/Dashboard/Sidebar/SidebarListItem.jsx @@ -10,26 +10,26 @@ import useNotification from "@/lib/app/hooks/useNotification"; const SidebarListItem = ({item, dispatch, handleDrawerToggle}) => { const t = useTranslations(); const { notification_count } = useNotification(); - const hasSubItems = item.type === "menu" && item.subItem && item.subItem.length > 0; - - const renderBadge = () => ( - - - - ); + const renderBadge = () => { + return !hasSubItems ? ( + + + + ) : null; + }; return ( <> - + { +const SidebarListSubItem = ({item, handleDrawerToggle }) => { const t = useTranslations(); const {notification_count} = useNotification(); - const renderBadge = (subitem) => ( { ); return ( + - + {item.subItem.map((subitem) => ( { ))} + ); }; diff --git a/src/components/layouts/Dashboard/Sidebar/index.jsx b/src/components/layouts/Dashboard/Sidebar/index.jsx index c0b6d6d..3c4cf12 100644 --- a/src/components/layouts/Dashboard/Sidebar/index.jsx +++ b/src/components/layouts/Dashboard/Sidebar/index.jsx @@ -22,6 +22,7 @@ const Sidebar = (props) => { "& .MuiDrawer-paper": { boxSizing: "border-box", width: props.drawerWidth, + overflow: "hidden", }, }} > @@ -34,6 +35,7 @@ const Sidebar = (props) => { "& .MuiDrawer-paper": { boxSizing: "border-box", width: props.drawerWidth, + overflow: "hidden", }, }} open