From 1c39ab6309643c6613469024b513d901cf0a3da8 Mon Sep 17 00:00:00 2001 From: mikheykinnv Date: Wed, 24 Apr 2024 11:50:04 +0800 Subject: [PATCH] MSP-3106 add applicationsContent component --- src/components/ApplicationsContent/index.tsx | 360 +++++++++++++++++++ 1 file changed, 360 insertions(+) create mode 100644 src/components/ApplicationsContent/index.tsx diff --git a/src/components/ApplicationsContent/index.tsx b/src/components/ApplicationsContent/index.tsx new file mode 100644 index 0000000..941472f --- /dev/null +++ b/src/components/ApplicationsContent/index.tsx @@ -0,0 +1,360 @@ +import { + EditOutlined, + DeleteOutlined, + PlusSquareOutlined +} from '@ant-design/icons' +import { Button, message, Spin, Table } from 'antd' +import { ColumnsType } from 'antd/es/table' +import { FormComponents } from 'isp-ui-kit' +import { EmptyData } from 'isp-ui-kit/dist/Layout' +import { FC, useState } from 'react' +import { useForm } from 'react-hook-form' + +import { ValidationRules } from '@constants/form/validationRules.ts' + +import Modal from '@widgets/Modal' + +import CanEdit from '@components/CanEdit' + +import { + ApplicationAppType, + ApplicationsServiceType, + ApplicationTokenType, + NewApplicationAppType, + NewApplicationTokenType, + UpdateApplicationAppType +} from '@pages/ApplicationsPage/applications.type.ts' + +import applicationsApi from '@services/applicationsService.ts' +import tokensApi from '@services/tokensService.ts' + +import './applications-content.scss' + +const { FormInput, FormSelect } = FormComponents + +interface ApplicationsContentPropTypes { + id: number +} + +const ApplicationsContent: FC = (id) => { + const { data: applications, isLoading: isLoadingApplicationsContent = [] } = + applicationsApi.useGetApplicationsByServiceIdQuery(id) + + const [createApplicationService] = + applicationsApi.useCreateApplicationServiceMutation() + const [updateApplication] = + applicationsApi.useUpdateApplicationsServiceMutation() + const [removeApplicationsService] = + applicationsApi.useRemoveApplicationsServiceMutation() + const [createToken] = tokensApi.useCreateTokenMutation() + const [showApplicationsModal, setShowApplicationsModal] = useState({ + addModal: false, + updateModal: false, + addToken: false + }) + const [currentApplicationsApp, setCurrentApplicationsApp] = + useState() + + const { + handleSubmit, + control: controlApplicationApp, + reset + } = useForm({ + mode: 'onChange' + }) + + const { handleSubmit: handleSubmitTokens, control: controlTokens } = + useForm({ + mode: 'onChange' + }) + + const handleShowUpdateModalApplicationApp = (data: ApplicationAppType) => { + setShowApplicationsModal({ + ...showApplicationsModal, + updateModal: true + }) + setCurrentApplicationsApp(data) + } + const handleShowCreateModalApplicationApp = () => { + setShowApplicationsModal({ + ...showApplicationsModal, + addModal: true + }) + } + const handleShowAddModalToken = (data: ApplicationAppType) => { + setShowApplicationsModal({ + ...showApplicationsModal, + addToken: true + }) + setCurrentApplicationsApp(data) + } + + const handleUpdateApplicationApp = (data: UpdateApplicationAppType) => { + if (currentApplicationsApp) { + const updateApplications: UpdateApplicationAppType = { + id: currentApplicationsApp.id, + name: data.name, + description: data.description + ? data.description + : currentApplicationsApp.description, + serviceId: id.id, + type: 'SYSTEM' + } + updateApplication({ ...currentApplicationsApp, ...updateApplications }) + .unwrap() + .then(() => { + setShowApplicationsModal({ + ...showApplicationsModal, + updateModal: false + }) + reset() + message.info('Элемент сохранен') + }) + .catch(() => message.error('Ошибка обновления элемента')) + } + } + + const handleCreateApplicationApp = (data: NewApplicationAppType) => { + const newApplicationApp: NewApplicationAppType = { + name: data.name, + description: data.description, + serviceId: id.id, + type: 'SYSTEM' + } + + createApplicationService(newApplicationApp) + .unwrap() + .then(message.info('Элемент сохранен')) + .catch((e) => message.error(e)) + setShowApplicationsModal({ + ...showApplicationsModal, + addModal: false + }) + reset() + } + const handleRemoveApplicationApp = (id: number) => + removeApplicationsService([id]) + .then(() => { + message.info('Элемент удален') + }) + .catch(message.error('Ошибка добавления элемента')) + + const handleCreateToken = (data: NewApplicationTokenType) => { + if (currentApplicationsApp) { + const newToken: NewApplicationTokenType = { + expireTimeMs: data.expireTimeMs, + appId: currentApplicationsApp.id + } + + createToken(newToken) + .then(() => { + setShowApplicationsModal({ + ...showApplicationsModal, + addToken: false + }) + reset() + message.info('Элемент добавлен') + }) + .catch(() => { + message.error('Ошибка добавления элемента') + }) + } + } + + const columns: ColumnsType = [ + { + title: 'id', + dataIndex: ['app', 'id'], + key: 'id' + }, + { + title: 'Наименование', + dataIndex: ['app', 'name'], + key: 'name' + }, + { + title: 'Описание', + dataIndex: ['app', 'description'], + key: 'description' + }, + { + title: 'Type', + dataIndex: ['app', 'type'], + key: 'Type' + }, + { + title: 'Количество токенов', + dataIndex: 'tokens', + key: 'tokensAmount', + render: (value) => { + return value.length + } + }, + { + title: 'Действия', + dataIndex: 'app', + key: 'actions', + render: (record) => { + return ( + + + + ( + + + + ) + }} + className="appliactions-content__table" + rowKey={(record) => record.app.id} + pagination={false} + dataSource={applications} + columns={columns} + /> + + + setShowApplicationsModal({ + ...showApplicationsModal, + updateModal: false + }) + } + > +
+ + + +
+ + + setShowApplicationsModal({ + ...showApplicationsModal, + addModal: false + }) + } + > +
+ + + +
+ + + setShowApplicationsModal({ + ...showApplicationsModal, + addToken: false + }) + } + > +
+ + +
+ + ) +} + +export default ApplicationsContent