From 90ef1d99795f8e388002fea0e92026c0527a42a1 Mon Sep 17 00:00:00 2001 From: veselovnk Date: Fri, 12 Apr 2024 15:56:52 +0300 Subject: [PATCH] MSP-3107 create AppAccessContent --- .../AppAccessContent/app-access-content.scss | 36 +++ .../app-access-content.type.ts | 4 + src/components/AppAccessContent/index.tsx | 208 ++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 src/components/AppAccessContent/app-access-content.scss create mode 100644 src/components/AppAccessContent/app-access-content.type.ts create mode 100644 src/components/AppAccessContent/index.tsx diff --git a/src/components/AppAccessContent/app-access-content.scss b/src/components/AppAccessContent/app-access-content.scss new file mode 100644 index 0000000..72cc6f7 --- /dev/null +++ b/src/components/AppAccessContent/app-access-content.scss @@ -0,0 +1,36 @@ +@import 'src/constants/style/styleConst'; + +.app-access-content { + height: 100%; + width: 100%; + min-width: 760px; + + padding: $basic-padding * 2; + border: 1px solid $border-color; + + &__header { + display: flex; + justify-content: space-between; + margin-bottom: $basic-margin * 2; + + &__action-buttons { + display: flex; + gap: $basic-margin * 2; + } + } + + &__main { + display: grid; + grid-template-columns:1fr 1fr; + + + &__tree-search-input { + width: 350px; + margin-bottom: $basic-margin * 2; + } + } + + .unknown-label { + color: $error-red + } +} diff --git a/src/components/AppAccessContent/app-access-content.type.ts b/src/components/AppAccessContent/app-access-content.type.ts new file mode 100644 index 0000000..3eb7c73 --- /dev/null +++ b/src/components/AppAccessContent/app-access-content.type.ts @@ -0,0 +1,4 @@ +export interface AppAccessContentPropsType { + id: number + paramPrefix: string +} diff --git a/src/components/AppAccessContent/index.tsx b/src/components/AppAccessContent/index.tsx new file mode 100644 index 0000000..b62d59a --- /dev/null +++ b/src/components/AppAccessContent/index.tsx @@ -0,0 +1,208 @@ +import { SearchOutlined } from '@ant-design/icons' +import { Button, Input, message, Spin, TreeProps } from 'antd' +import { ChangeEvent, FC, memo, useEffect, useState } from 'react' +import { Navigate, useSearchParams } from 'react-router-dom' + +import AccessListTree from '@widgets/AccessListTree' +import SelectedAccessMethod from '@widgets/SelectedAccessMethod' + +import { AppAccessContentPropsType } from '@components/AppAccessContent/app-access-content.type.ts' + +import { setSearchValue } from '@utils/columnLayoutUtils.ts' + +import useRole from '@hooks/useRole.tsx' + +import accessListApi from '@services/accessListService.ts' +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 './app-access-content.scss' + + +const unknownMethodKey = 'неизвестные методы' + +const AppAccessContent: FC = ({ + id, + paramPrefix +}) => { + const [messageApi, contextHolder] = message.useMessage() + const { hasPermission } = useRole() + + const canWrite = hasPermission(PermissionKeys.write) + + const { + data: methods = [], + isLoading: isMethodsLoading, + isError: isMethodsError, + isSuccess: isMethodsSuccess + } = accessListApi.useGetByIdQuery({ id }) + const [setList] = accessListApi.useSetListMutation() + const [setOne] = accessListApi.useSetOneMutation() + + const { + data: routes = { + originalResponse: [], + moduleEndpoints: {}, + allRouteMap: {} + }, + isLoading: isRouteLoading, + isError: isRouteError, + isSuccess: isRouteSuccess + } = routeApi.useGetAllRoutesQuery() + const { moduleEndpoints, allRouteMap } = routes + const [defaultAllRoutes, setDefaultAllRoutes] = useState< + Record + >({}) + + const [searchParams, setSearchParams] = useSearchParams() + const searchValue = searchParams.get(paramPrefix) || '' + + const isLoading = isMethodsLoading || isRouteLoading + const isError = isMethodsError || isRouteError + const isSuccess = isMethodsSuccess && isRouteSuccess + + const openSuccessMessage = (body: string) => { + messageApi.success(body) + } + + const openErrorMessage = (body: string) => { + messageApi.error(body) + } + + const createAllMethodList = (status: boolean) => { + return Object.keys(allRouteMap).map((key) => { + return { + method: key, + value: status + } + }) + } + + const onCheck: TreeProps['onCheck'] = (_, info): void | never[] => { + const { node } = info + if (!node.children) { + const sendData = { + appId: id, + method: node.key as string, + value: !node.checked + } + setOne(sendData) + .then(() => { + openSuccessMessage('Статус метода успешно изменен') + }) + .catch(() => { + openErrorMessage('Не удалось изменить статус метода') + }) + return + } + + const newCheckedKeys = node.children.map((child) => ({ + method: child.key as string, + value: !node.checked + })) + setListMethods(newCheckedKeys) + } + + const onChange = (e: ChangeEvent) => { + const { value } = e.target + setSearchValue(value.trim().toLowerCase(), setSearchParams, paramPrefix) + } + + const setListMethods = (methods: AccessListMethodType[]) => { + setList({ + appId: id, + methods: methods + }) + .then(() => { + openSuccessMessage('Статус методов успешно изменен') + }) + .catch(() => { + openErrorMessage('Не удалось изменить статус методов') + }) + } + + const setAllMethods = (status: boolean) => { + const methods = createAllMethodList(status) + setListMethods(methods) + } + + const getAllRoutes = () => { + const missingRoute: EndpointType[] = [] + const copyAllRoutes = { ...moduleEndpoints } + + methods.forEach((item) => { + if (!allRouteMap[item.method]) { + missingRoute.push({ path: item.method } as EndpointType) + } + }) + + if (missingRoute.length) { + copyAllRoutes[unknownMethodKey] = missingRoute + } + return copyAllRoutes + } + + useEffect(() => { + const allRoutes = getAllRoutes() + setDefaultAllRoutes(allRoutes) + }, [isSuccess]) + + if (isLoading) { + return + } + + if (isError) { + return + } + return ( +
+ {contextHolder} +
+ } + allowClear + className="app-access-content__main__tree-search-input" + value={searchValue} + placeholder="Найти метод" + onChange={onChange} + /> +
+ + +
+
+
+ + +
+
+ ) +} + +export default memo(AppAccessContent)