From ffd4aae3e01b859eb4f3584d4aa17fc181127d30 Mon Sep 17 00:00:00 2001 From: Med Marrouchi Date: Mon, 20 Jan 2025 14:43:11 +0100 Subject: [PATCH] fix: apply pr review --- frontend/src/hooks/useAvailableMenuItems.ts | 13 ++++++++++++- frontend/src/hooks/useHasPermission.ts | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/frontend/src/hooks/useAvailableMenuItems.ts b/frontend/src/hooks/useAvailableMenuItems.ts index ca3a54c2..3a702cf7 100644 --- a/frontend/src/hooks/useAvailableMenuItems.ts +++ b/frontend/src/hooks/useAvailableMenuItems.ts @@ -14,7 +14,12 @@ import { PermissionAction } from "@/types/permission.types"; import { useHasPermission } from "./useHasPermission"; -// Helper function to check permissions for a menu item +/** + * Helper function to check permissions for a menu item + * @param menuItem - The menu item + * @param hasPermission - Callback function + * @returns True if hasPermission() is true for all required permissions. + */ const isMenuItemAllowed = ( menuItem: MenuItem, hasPermission: (entityType: EntityType, action: PermissionAction) => boolean, @@ -30,6 +35,12 @@ const isMenuItemAllowed = ( ) ); }; + +/** + * Filters menu items based on user permissions. + * @param menuItems - The list of menu items to filter. + * @returns A filtered list of menu items that the user is allowed to access. + */ const filterMenuItems = ( menuItems: MenuItem[], hasPermission: (entityType: EntityType, action: PermissionAction) => boolean, diff --git a/frontend/src/hooks/useHasPermission.ts b/frontend/src/hooks/useHasPermission.ts index 1c1fa2a3..0d9079c6 100644 --- a/frontend/src/hooks/useHasPermission.ts +++ b/frontend/src/hooks/useHasPermission.ts @@ -19,7 +19,7 @@ export const useHasPermission = () => { (type: EntityType, action: PermissionAction) => { const allowedActions = getAllowedActions(type); - return allowedActions?.includes(action) ? true : false; + return !!allowedActions && allowedActions?.includes(action); }, [getAllowedActions], );