THere's a Menu
on my UI
and what im trying to achieve is to provide an optional hiding
for each specified menu
item
, all that based on logged user role
.
In my Menu
component im iterating through MenuItems
and showing/hiding them based on their item.Hide
property:
Structure of menu item array MenuItems[]
is as following:
const MenuItems[] = [
{
label: ''
icon: ''
hide: showOrHideByUserRole(userRole, ItemIDs.search) // I added this property
},
{
label: ''
icon: ''
hide: showOrHideByUserRole(userRole, ItemIDs.about) // I added this property
},
{
*etc*
}
]
Of course there's ItemIDs enum
which is only providing an id
for a specific item name
(string). And userRole
state.
And here's my showOrHideByUserRole
function which im concerned about:
function showOrHideByUserRole(roleName:string, itemID: number){
switch (itemID) {
case 1:
if(roleName === 'AAA')
return false;
else if(roleName === 'BBB')
return false;
else
return true;
case 2:
if(roleName === 'AAA')
return true;
else if(roleName === 'BBB')
return false;
else
return true;
case 3:
if(roleName === 'AAA')
return false;
else if(roleName === 'BBB')
return false;
else
return true;
case 4:
if(roleName === 'AAA')
return false;
else if(roleName === 'BBB')
return false;
else
return true;
case 5:
if(roleName === 'AAA')
return false;
else if(roleName === 'BBB')
return false;
else
return true;
default:
return true;
}
}
As you can see this looks kind of ugly (to me at least?) and im wondering if there's a prettier/better way to do what I want?
What needs to be considered is future expansion role
-wise. Meaning there might be more roles in the future.