Skip to content

Commit

Permalink
Refactoring MultiPageMenu to be more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bizz committed Apr 26, 2024
1 parent ddd984e commit 014d25e
Showing 1 changed file with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from 'react';
import React from 'react';
import Close from '@mui/icons-material/Close';
import {
Box,
Expand All @@ -9,6 +9,7 @@ import {
Typography,
} from '@mui/material';
import { styled } from '@mui/material/styles';
import { Session } from 'next-auth';
import { useTranslation } from 'react-i18next';
import { makeStyles } from 'tss-react/mui';
import { useGetDesignationAccountsQuery } from 'src/components/EditDonationModal/EditDonationModal.generated';
Expand All @@ -18,7 +19,11 @@ import { useAccountListId } from 'src/hooks/useAccountListId';
import { useRequiredSession } from 'src/hooks/useRequiredSession';
import { Item } from './Item/Item';
import { useManageOrganizationsAccessQuery } from './MultiPageMenu.generated';
import { reportNavItems, settingsNavItems } from './MultiPageMenuItems';
import {
NavItems,
reportNavItems,
settingsNavItems,
} from './MultiPageMenuItems';

export enum NavTypeEnum {
Reports = 'reports',
Expand Down Expand Up @@ -55,6 +60,31 @@ const FilterList = styled(List)(({ theme }) => ({
},
}));

type ShowMenuItemProps = {
item: NavItems;
user: Session['user'];
hasOrganizationsAccess: boolean;
};

const showMenuItem = ({
item,
user,
hasOrganizationsAccess,
}: ShowMenuItemProps): boolean => {
if (item?.grantedAccess?.length) {
if (hasOrganizationsAccess && item.id.startsWith('organizations')) {
return true;
}
if (item.grantedAccess.indexOf('admin') !== -1 && user.admin) {
return true;
}
if (item.grantedAccess.indexOf('developer') !== -1 && user.developer) {
return true;
}
} else return true;
return false;
};

export const MultiPageMenu: React.FC<Props & BoxProps> = ({
selectedId,
isOpen,
Expand Down Expand Up @@ -130,31 +160,9 @@ export const MultiPageMenu: React.FC<Props & BoxProps> = ({
/>
)}
{navItems.map((item) => {
const showItem = useMemo(() => {
if (item?.grantedAccess?.length) {
if (
hasOrganizationsAccess &&
['organizations'].includes(item.id)
) {
return true;
}
if (
item.grantedAccess.indexOf('admin') !== -1 &&
user.admin
) {
return true;
}
if (
item.grantedAccess.indexOf('developer') !== -1 &&
user.developer
) {
return true;
}
} else return true;
return false;
}, [item, user]);

if (!showItem) return null;
if (!showMenuItem({ item, user, hasOrganizationsAccess })) {
return null;
}
return (
<Item
key={item.id}
Expand Down

0 comments on commit 014d25e

Please sign in to comment.