From 18b03ca7810be3c671c5b55368f0042c688ad850 Mon Sep 17 00:00:00 2001 From: hamed <15238-genius.ha@users.noreply.drupalcode.org> Date: Wed, 19 Nov 2025 11:48:40 +0330 Subject: [PATCH] refactor: enhance user role handling in List and ability definitions --- components/layout/sidebar/List.js | 85 ++++++++++++++++++++++--------- lib/ability.js | 2 +- 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/components/layout/sidebar/List.js b/components/layout/sidebar/List.js index c2c8835..b52683a 100644 --- a/components/layout/sidebar/List.js +++ b/components/layout/sidebar/List.js @@ -8,8 +8,10 @@ import ProfileD from "@/components/icons/ProfileD"; import NotificationD from "@/components/icons/NotificationD"; import { Button } from "@mui/material"; import ArrowLeftSideBar from "@/components/icons/ArrowLeftSideBar"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import ModalLogout from "../../../app/component/ModalLogout"; +import Cookies from "js-cookie"; +import { useRouter } from "next/navigation"; function List({ value, @@ -18,19 +20,63 @@ function List({ setIsTurnsDetails, setIsOpenSide, }) { + const router = useRouter(); + const [hasRepresentationRole, setHasRepresentationRole] = useState(false); + + useEffect(() => { + const userInfo = Cookies.get("userInfo"); + if (userInfo) { + try { + const parsedUserInfo = JSON.parse(userInfo); + const roles = parsedUserInfo?.roles || {}; + const hasRole = Object.values(roles).includes("representation"); + setHasRepresentationRole(hasRole); + } catch (error) { + console.error("Error parsing userInfo:", error); + } + } + }, []); + + const baseListMenu = [ + { name: "حساب کاربری", icon: , action: "tab" }, + { name: "نوبت های من", icon: , action: "tab" }, + { name: "تراکنش های من", icon: , action: "tab" }, + ]; + + // اگر نقش representation دارد، داشبورد نماینده را اضافه کن + if (hasRepresentationRole) { + baseListMenu.push({ + name: "داشبورد نماینده", + icon: , + action: "navigate", + path: "/panel/dashboard" + }); + } + const listMenu = [ - { name: "حساب کاربری", icon: }, - { name: "نوبت های من", icon: }, - { name: "تراکنش های من", icon: }, - { name: "نظرات من", icon: }, - { name: "پیام ها", icon: }, - { name: "خروج", icon: }, + ...baseListMenu, + { name: "نظرات من", icon: , action: "tab" }, + { name: "پیام ها", icon: , action: "tab" }, + { name: "خروج", icon: , action: "logout" }, ]; const [open, setOpen] = useState(false); const handleOpen = () => setOpen(true); const handleClose = () => setOpen(false); + const handleMenuClick = (item, idx) => { + setIsOpenSide(false); + !isTurnsDetails && setIsTurnsDetails(false); + + if (item.action === "logout") { + handleOpen(); + } else if (item.action === "navigate") { + router.push(item.path); + } else { + setValue(idx); + } + }; + return (
    @@ -41,28 +87,21 @@ function List({ transition-all duration-300 before:transition-all before:duration-300 !relative before:!absolute before:!right-0 !flex !justify-between !items-center before:!top-1/2 before:!-translate-y-1/2 before:!h-[40px] before:!w-[2px] - ${ - value === idx - ? "before:md:!bg-[#5559CE]" - : "before:!bg-transparent" - }`} - onClick={() => { - setIsOpenSide(false); - !isTurnsDetails && setIsTurnsDetails(false); - if (idx === 5) handleOpen(); - else setValue(idx); - }} + ${value === idx + ? "before:md:!bg-[#5559CE]" + : "before:!bg-transparent" + }`} + onClick={() => handleMenuClick(item, idx)} >
    {item.icon}

    {item.name} diff --git a/lib/ability.js b/lib/ability.js index 32629bb..f266dab 100644 --- a/lib/ability.js +++ b/lib/ability.js @@ -8,7 +8,7 @@ export function defineAbilitiesFor(user) { cannot("access", "Login"); const parsedData = JSON.parse(user.value); - if (parsedData.roles.find((item) => item === "representation")) { + if (parsedData.roles && Object.values(parsedData.roles).includes("representation")) { can("access", "Panel"); } } else {