From 42ee1697c4dd95dce52a85343e45e6bda5434db6 Mon Sep 17 00:00:00 2001 From: Anastasiia Date: Wed, 17 Apr 2024 18:19:41 +0300 Subject: [PATCH] MSP-3105 change layout.type.ts and roles.type.ts --- src/components/AdminPermission/index.tsx | 4 ++-- src/components/AppAccessContent/index.tsx | 4 ++-- src/components/Layout/index.tsx | 22 +++++++++++----------- src/components/Layout/layout.type.ts | 16 ++++++++-------- src/hooks/useRole.tsx | 4 ++-- src/pages/AppAccessPage/index.tsx | 4 ++-- src/type/roles.type.ts | 15 +++++---------- src/widgets/AccessListTree/index.tsx | 4 ++-- 8 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/components/AdminPermission/index.tsx b/src/components/AdminPermission/index.tsx index 310f505..557861f 100644 --- a/src/components/AdminPermission/index.tsx +++ b/src/components/AdminPermission/index.tsx @@ -2,12 +2,12 @@ import { PropsWithChildren } from 'react' import { useRole } from '@hooks/useRole.tsx' -import { PermissionKeys } from '@type/roles.type.ts' +import { PermissionKeysType } from '@type/roles.type.ts' const AdminPermission = ({ children }: PropsWithChildren) => { const { hasPermission } = useRole() - if (hasPermission(PermissionKeys.read)) { + if (hasPermission(PermissionKeysType.read)) { return <>{children} } return null diff --git a/src/components/AppAccessContent/index.tsx b/src/components/AppAccessContent/index.tsx index b62d59a..c4f09a1 100644 --- a/src/components/AppAccessContent/index.tsx +++ b/src/components/AppAccessContent/index.tsx @@ -18,7 +18,7 @@ import routeApi from '@services/routeService.ts' import { routePaths } from '@routes/routePaths.ts' import { AccessListMethodType, EndpointType } from '@type/accessList.type.ts' -import { PermissionKeys } from '@type/roles.type.ts' +import { PermissionKeysType } from '@type/roles.type.ts' import './app-access-content.scss' @@ -32,7 +32,7 @@ const AppAccessContent: FC = ({ const [messageApi, contextHolder] = message.useMessage() const { hasPermission } = useRole() - const canWrite = hasPermission(PermissionKeys.write) + const canWrite = hasPermission(PermissionKeysType.write) const { data: methods = [], diff --git a/src/components/Layout/index.tsx b/src/components/Layout/index.tsx index fdffbf5..fbaaaeb 100644 --- a/src/components/Layout/index.tsx +++ b/src/components/Layout/index.tsx @@ -6,7 +6,7 @@ import { Navigate, Outlet, useLocation, useNavigate } from 'react-router-dom' import { localStorageKeys } from '@constants/localStorageKeys.ts' import { - MenuItemKeys, + MenuItemKeysType, MenuItemType, menuKeys } from '@components/Layout/layout.type.ts' @@ -22,7 +22,7 @@ import { StateProfileStatus } from '@stores/redusers/ProfileSlice.ts' import { routePaths } from '@routes/routePaths.ts' -import { PermissionKeys } from '@type/roles.type.ts' +import { PermissionKeysType } from '@type/roles.type.ts' import './layout.scss' @@ -32,16 +32,16 @@ const LayoutComponent = () => { const navigate = useNavigate() const [collapsed, setCollapsed] = useState(false) - const [selectedMenuKeys, setSelectedMenuKeys] = useState([]) + const [selectedMenuKeys, setSelectedMenuKeys] = useState([]) const [openKeys, setOpenKeys] = useState([]) const dispatch = useAppDispatch() const { status } = useAppSelector((state) => state.profileReducer) const location = useLocation() - const userToken = LocalStorage.get(localStorageKeys.USER_TOKEN) - const { hasPermission } = useRole() - const hideItem = (permission: PermissionKeys | PermissionKeys[]) => { + const userToken = LocalStorage.get(localStorageKeys.USER_TOKEN) + + const hideItem = (permission: PermissionKeysType | PermissionKeysType[]) => { if (Array.isArray(permission)) { const result = permission.reduce((acc, currentValue) => { const hasPermissionFunc = hasPermission(currentValue) @@ -55,19 +55,19 @@ const LayoutComponent = () => { { label: 'Доступы приложений', key: 'appAccess', - className: hideItem([PermissionKeys.read]), + className: hideItem([PermissionKeysType.read]), icon: }, { label: 'Пользователи и роли', key: 'sessionManagement', - className: hideItem([PermissionKeys.read]), + className: hideItem([PermissionKeysType.read]), icon: , children: [ { label: 'Пользователи', key: 'users', - className: hideItem(PermissionKeys.read) + className: hideItem(PermissionKeysType.read) } ] } @@ -80,7 +80,7 @@ const LayoutComponent = () => { }, []) useEffect(() => { - const menuKey = location.pathname.split('/')[1] as MenuItemKeys + const menuKey = location.pathname.split('/')[1] as MenuItemKeysType const menuItem = menuKeys[menuKey] @@ -92,7 +92,7 @@ const LayoutComponent = () => { const handlerOnClickMenu = ({ key }: any): void => { switch (key) { - case MenuItemKeys.appAccess: + case MenuItemKeysType.appAccess: navigate(routePaths.appAccess) break default: diff --git a/src/components/Layout/layout.type.ts b/src/components/Layout/layout.type.ts index c3027ad..bcd5b94 100644 --- a/src/components/Layout/layout.type.ts +++ b/src/components/Layout/layout.type.ts @@ -1,21 +1,21 @@ import { MenuProps } from 'antd' -export enum MenuItemKeys { +export enum MenuItemKeysType { users = 'users', appAccess = 'appAccess' } -type MenuItem = { - key: MenuItemKeys +interface MenuItem { + key: MenuItemKeysType parent: string[] } -export const menuKeys: Record = { - [MenuItemKeys.users]: { - key: MenuItemKeys.users, +export const menuKeys: Record = { + [MenuItemKeysType.users]: { + key: MenuItemKeysType.users, parent: ['sessionManagement'] }, - [MenuItemKeys.appAccess]: { - key: MenuItemKeys.appAccess, + [MenuItemKeysType.appAccess]: { + key: MenuItemKeysType.appAccess, parent: [] } } diff --git a/src/hooks/useRole.tsx b/src/hooks/useRole.tsx index b17c70e..da69254 100644 --- a/src/hooks/useRole.tsx +++ b/src/hooks/useRole.tsx @@ -1,6 +1,6 @@ import { useMemo } from 'react' -import { PermissionKeys } from '@type/roles.type.ts' +import { PermissionKeysType } from '@type/roles.type.ts' import { useAppSelector } from './redux.ts' @@ -8,7 +8,7 @@ export const useRole = () => { const { profile } = useAppSelector((state: any) => state.profileReducer) const role: string = useMemo(() => profile && profile.role, [profile]) - const hasPermission = (permission: PermissionKeys) => { + const hasPermission = (permission: PermissionKeysType) => { const userPermission = profile.permissions || [] return userPermission.includes(permission) } diff --git a/src/pages/AppAccessPage/index.tsx b/src/pages/AppAccessPage/index.tsx index fd00d4d..77f8292 100644 --- a/src/pages/AppAccessPage/index.tsx +++ b/src/pages/AppAccessPage/index.tsx @@ -24,7 +24,7 @@ import appApi from '@services/appService.ts' import { routePaths } from '@routes/routePaths.ts' import { SystemTreeAppType } from '@type/app.type.ts' -import { PermissionKeys } from '@type/roles.type.ts' +import { PermissionKeysType } from '@type/roles.type.ts' import './app-access-page.scss' @@ -48,7 +48,7 @@ const AppAccessPage = () => { const { id: selectedItemId = '' } = useParams() const [searchParams, setSearchParams] = useSearchParams() const searchValue = searchParams.get(firstColumnSearchParam) || '' - const canRead = hasPermission(PermissionKeys.read) + const canRead = hasPermission(PermissionKeysType.read) useEffect(() => { if (!canRead) { diff --git a/src/type/roles.type.ts b/src/type/roles.type.ts index ce617ac..05df90b 100644 --- a/src/type/roles.type.ts +++ b/src/type/roles.type.ts @@ -1,21 +1,16 @@ -export enum RoleKeys { - user = 'user', - admin = 'admin' -} - -export enum PermissionKeys { +export enum PermissionKeysType { read = 'read', // Просмотр страниц write = 'write', // Редактирование страниц } -export type RoleType = { +export interface RoleType { changeMessage?: string createdAt: string description: string externalGroup: string id: number name: string - permissions: PermissionKeys[] + permissions: PermissionKeysType[] updatedAt: string immutable: boolean exclusive: boolean @@ -23,7 +18,7 @@ export type RoleType = { export type NewRoleType = Omit -export type PermissionType = { - key: PermissionKeys +export interface PermissionType { + key: PermissionKeysType name: string } diff --git a/src/widgets/AccessListTree/index.tsx b/src/widgets/AccessListTree/index.tsx index bac898d..62dd993 100644 --- a/src/widgets/AccessListTree/index.tsx +++ b/src/widgets/AccessListTree/index.tsx @@ -7,7 +7,7 @@ import { AccessListTreePropsType } from '@widgets/AccessListTree/access-list-tre import useRole from '@hooks/useRole.tsx' import { EndpointType } from '@type/accessList.type.ts' -import { PermissionKeys } from '@type/roles.type.ts' +import { PermissionKeysType } from '@type/roles.type.ts' import './access-list-tree.scss' @@ -123,7 +123,7 @@ const AccessListTree: FC = ({
setExpandedKeys(keys as string[])}