Skip to content

Commit

Permalink
feat(client): handle nav tabs admin access and add admin column for u…
Browse files Browse the repository at this point in the history
…sers list
  • Loading branch information
Jozwiaczek committed Aug 31, 2021
1 parent 004fae4 commit 563e2bc
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/elements/AppBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const defaultTabs: Array<AppBarItem> = [
path: mapRoutesToArray(admin),
label: 'menu.admin',
icon: <AdminIcon />,
onlyAdmin: false,
onlyAdmin: true,
component: AdminDashboard,
exact: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Children, isValidElement, ReactNode } from 'react';

import { Role } from '../../../../enums/role.enum';
import { ApiUser } from '../../../../interfaces/api.types';
import isAdmin from '../../../../utils/isAdmin';
import { TabProps } from '../Tab/Tab.types';
import { GetIndicatorAnimationSpace, GetIndicatorSizeProps, IndicatorSize } from './Tabs.types';

Expand Down Expand Up @@ -30,8 +30,13 @@ export const getIndicatorAnimationSpace = ({
return emptyWidthForValue + tabsWidthForValue + additionalSpaceForIndicatorWidth;
};

export const hasAccess = (onlyAdmin?: boolean, user?: ApiUser) =>
!(onlyAdmin && !user?.roles?.includes(Role.Admin));
export const hasAccess = (onlyAdmin?: boolean, user?: ApiUser) => {
if (!onlyAdmin) {
return true;
}

return isAdmin(user?.roles);
};

export const countAvailableChildren = (children: ReactNode, user?: ApiUser) =>
Children.count(
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/enums/role.enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum Role {
User = 'user',
Admin = 'admin',
SuperAdmin = 'superAdmin',
}
16 changes: 15 additions & 1 deletion packages/client/src/pages/authorized/Admin/Users/Users.styled.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import styled from 'styled-components';
import styled, { css } from 'styled-components';

import { KeyIcon } from '../../../../icons';

export const Wrapper = styled.div`
height: 100%;
Expand All @@ -15,3 +17,15 @@ export const ListContainer = styled.div`
overflow-x: auto;
padding-top: 60px;
`;

export const AdminAccessIcon = styled(KeyIcon)(
({ theme: { palette } }) => css`
color: ${palette.colors.orange};
`,
);

export const UserAccessIcon = styled(KeyIcon)(
({ theme: { palette } }) => css`
color: ${palette.text.secondary};
`,
);
12 changes: 11 additions & 1 deletion packages/client/src/pages/authorized/Admin/Users/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import React from 'react';
import { CardList, DateField, DetailedList, FunctionField, TextField } from '../../../../elements';
import { useMediaQuery } from '../../../../hooks';
import { ApiUser } from '../../../../interfaces/api.types';
import { ListContainer, Wrapper } from './Users.styled';
import { isAdmin } from '../../../../utils';
import { AdminAccessIcon, ListContainer, UserAccessIcon, Wrapper } from './Users.styled';

const Users = () => {
const isMobile = useMediaQuery(({ breakpoints, down }) => down(breakpoints.lg));
Expand All @@ -27,6 +28,15 @@ const Users = () => {
label="user.name"
render={({ firstName, lastName }) => `${firstName} ${lastName}`}
/>
<FunctionField<ApiUser>
label="Is admin"
render={({ roles }) => {
if (isAdmin(roles)) {
return <AdminAccessIcon />;
}
return <UserAccessIcon />;
}}
/>
<TextField source="email" />
<DateField source="createdAt" showTime />
</DetailedList>
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { default as getCssColor } from './getCssColor';
export { default as getLabelFromSource } from './getLabelFromSource';
export { default as getPlaceholderFromSource } from './getPlaceholderFromSource';
export { default as hexToRgba } from './hexToRgba';
export { default as isAdmin } from './isAdmin';
export { default as mapHistoryEventToLabel } from './mapHistoryEventToLabel';
export { default as onlyOnDevEnv } from './onlyOnDevEnv';
export { default as registerWebPush } from './registerWebPush';
14 changes: 14 additions & 0 deletions packages/client/src/utils/isAdmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Role } from '../enums/role.enum';

const isAdmin = (roles?: Role[]): boolean => {
if (!roles) {
return false;
}

const hasAdminRole = roles.includes(Role.Admin);
const hasSuperAdminRole = roles.includes(Role.SuperAdmin);

return hasAdminRole || hasSuperAdminRole;
};

export default isAdmin;

0 comments on commit 563e2bc

Please sign in to comment.