From bc8db8d248da4c93c21f40b6b7a4e2fab1d25996 Mon Sep 17 00:00:00 2001 From: Davor Date: Fri, 27 May 2022 21:28:34 +0200 Subject: [PATCH] Modify FE for No access role - fix user settings modal - CurrentUserModal - TODO: refactor UserModal and CurrentUserModal - it's same thing --- .../CurrentUserModal/CurrentUserModal.tsx | 14 +++++++--- .../components/CurrentUserModal/consts.ts | 12 ++++---- .../users/components/UserModal/UserModal.tsx | 2 ++ .../users/components/UserModal/consts.ts | 8 +++--- src/services/users/transformations.ts | 28 +++++++++++++++++-- src/services/users/types.ts | 1 + 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/components/Header/components/CurrentUserModal/CurrentUserModal.tsx b/src/components/Header/components/CurrentUserModal/CurrentUserModal.tsx index 29a3c16..aaa29c2 100644 --- a/src/components/Header/components/CurrentUserModal/CurrentUserModal.tsx +++ b/src/components/Header/components/CurrentUserModal/CurrentUserModal.tsx @@ -83,6 +83,7 @@ export const CurrentUserModal = ({ open, onClose, user }: UserModalProps) => { name={`app_roles.${index}.role`} label="Role" options={[ + { value: UserRole.NoAccess, name: 'No access' }, { value: UserRole.Admin, name: 'Admin' }, { value: UserRole.User, name: 'User' }, ]} @@ -121,9 +122,12 @@ export const CurrentUserModal = ({ open, onClose, user }: UserModalProps) => {
    - {fields - .filter((field) => field.name !== 'dashboard') - .map((item, index) => ( + {fields.map((item, index) => { + if (item.name === 'dashboard') { + return null; + } + + return (
  • @@ -142,6 +146,7 @@ export const CurrentUserModal = ({ open, onClose, user }: UserModalProps) => { control={control} name={`app_roles.${index}.role`} options={[ + { value: UserRole.NoAccess, name: 'No access' }, { value: UserRole.Admin, name: 'Admin' }, { value: UserRole.User, name: 'User' }, ]} @@ -149,7 +154,8 @@ export const CurrentUserModal = ({ open, onClose, user }: UserModalProps) => {
  • - ))} + ); + })}
diff --git a/src/components/Header/components/CurrentUserModal/consts.ts b/src/components/Header/components/CurrentUserModal/consts.ts index abda708..1b02fa3 100644 --- a/src/components/Header/components/CurrentUserModal/consts.ts +++ b/src/components/Header/components/CurrentUserModal/consts.ts @@ -12,7 +12,7 @@ export const appAccessList = [ label: 'Wordpress', }, { - name: 'next-cloud', + name: 'nextcloud', image: '/assets/nextcloud.svg', label: 'NextCloud', }, @@ -30,19 +30,19 @@ const initialAppRoles = [ }, { name: 'wekan', - role: UserRole.User, + role: UserRole.NoAccess, }, { name: 'wordpress', - role: UserRole.User, + role: UserRole.NoAccess, }, { - name: 'next-cloud', - role: UserRole.User, + name: 'nextcloud', + role: UserRole.NoAccess, }, { name: 'zulip', - role: UserRole.User, + role: UserRole.NoAccess, }, ]; diff --git a/src/modules/users/components/UserModal/UserModal.tsx b/src/modules/users/components/UserModal/UserModal.tsx index b31d218..a810f30 100644 --- a/src/modules/users/components/UserModal/UserModal.tsx +++ b/src/modules/users/components/UserModal/UserModal.tsx @@ -133,6 +133,7 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) name={`app_roles.${index}.role`} label="Role" options={[ + { value: UserRole.NoAccess, name: 'No access' }, { value: UserRole.User, name: 'User' }, { value: UserRole.Admin, name: 'Admin' }, ]} @@ -194,6 +195,7 @@ export const UserModal = ({ open, onClose, userId, setUserId }: UserModalProps) control={control} name={`app_roles.${index}.role`} options={[ + { value: UserRole.NoAccess, name: 'No Access' }, { value: UserRole.User, name: 'User' }, { value: UserRole.Admin, name: 'Admin' }, ]} diff --git a/src/modules/users/components/UserModal/consts.ts b/src/modules/users/components/UserModal/consts.ts index a47d6de..1b02fa3 100644 --- a/src/modules/users/components/UserModal/consts.ts +++ b/src/modules/users/components/UserModal/consts.ts @@ -30,19 +30,19 @@ const initialAppRoles = [ }, { name: 'wekan', - role: UserRole.User, + role: UserRole.NoAccess, }, { name: 'wordpress', - role: UserRole.User, + role: UserRole.NoAccess, }, { name: 'nextcloud', - role: UserRole.User, + role: UserRole.NoAccess, }, { name: 'zulip', - role: UserRole.User, + role: UserRole.NoAccess, }, ]; diff --git a/src/services/users/transformations.ts b/src/services/users/transformations.ts index 5f1c2f7..e296df9 100644 --- a/src/services/users/transformations.ts +++ b/src/services/users/transformations.ts @@ -1,16 +1,38 @@ import { AppRoles, User, UserRole } from './types'; +const transformRoleById = (roleId: any): UserRole => { + switch (roleId) { + case 1: + return UserRole.Admin; + case 2: + return UserRole.User; + default: + return UserRole.NoAccess; + } +}; + +const transformRoleIdByRole = (role: UserRole | null): number | null => { + switch (role) { + case UserRole.Admin: + return 1; + case UserRole.User: + return 2; + default: + return null; + } +}; + export const transformAppRoles = (data: any): AppRoles => { - const resolvedAdminRole = data.role_id === 1 ? UserRole.Admin : UserRole.User; + const userRole = transformRoleById(data.role_id); return { name: data.name ?? '', - role: resolvedAdminRole ?? UserRole.User, + role: userRole, }; }; export const transformRequestAppRoles = (data: AppRoles): any => { - const resolvedRequestRole = data.role === UserRole.Admin ? 1 : null; + const resolvedRequestRole = transformRoleIdByRole(data.role) ?? null; return { name: data.name ?? '', diff --git a/src/services/users/types.ts b/src/services/users/types.ts index 9245247..f97f435 100644 --- a/src/services/users/types.ts +++ b/src/services/users/types.ts @@ -13,6 +13,7 @@ export interface FormUser extends User { } export enum UserRole { + NoAccess = 'no_access', Admin = 'admin', User = 'user', }