From cfe0c8985ab15a72863a90c5898ebcdced978fb7 Mon Sep 17 00:00:00 2001 From: mikheikinnv Date: Fri, 14 Jun 2024 11:42:33 +0300 Subject: [PATCH] MSP-3205 add ConfigurationEditorPage component --- src/pages/ConfigurationEditorPage/index.tsx | 209 ++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 src/pages/ConfigurationEditorPage/index.tsx diff --git a/src/pages/ConfigurationEditorPage/index.tsx b/src/pages/ConfigurationEditorPage/index.tsx new file mode 100644 index 0000000..ee48a6e --- /dev/null +++ b/src/pages/ConfigurationEditorPage/index.tsx @@ -0,0 +1,209 @@ +import { RollbackOutlined } from '@ant-design/icons' +import { Button, Dropdown, MenuProps, message, Radio, RadioChangeEvent, Spin } from 'antd' +import { Dispatch, FC, SetStateAction, useEffect, useState } from 'react' +import { useLocation, useNavigate, useParams } from 'react-router-dom' + +import ConfigurationEditorCode from '@components/ConfigurationEditorCode' +import ConfigurationEditorJson from '@components/ConfigurationEditorJson' + +import configServiceApi from '@services/configService.ts' +import modulesServiceApi from '@services/modulesService.ts' + +import './configuration-editor-page.scss' +import ConfigSchemaModal from '@components/ConfigSchemaModal' +import { cleanEmptyParamsObject, sortObject } from '@utils/objectUtils.ts' +import { ConfigType, ResponseSchemaType } from '@pages/ModulesPage/module.type.ts' +import ConfirmConfigModal from '@components/ConfirmConfigModal/ConfirmConfigModal.tsx' +import ErrorConfigModal from '@components/ErrorConfigModal/ErrorConfigModal.tsx' + +export interface ConfigurationEditorPropsType { + JsonSchema?: ResponseSchemaType + bufConfig?: ConfigType + setBufConfig: Dispatch> + isCurrentConfigLoading: boolean + setDisableBtn?: Dispatch> +} + +const ConfigurationEditorPage: FC = () => { + const navigate = useNavigate() + const { moduleId = '' } = useParams() + const { id = '' } = useParams() + const isNew = id === 'new' + const { data: JsonSchema, isLoading: isLoadingJsonSchema } = + modulesServiceApi.useGetByModuleIdQuery(moduleId) + const { + data: currentConfig, + isLoading: isCurrentConfigLoading, + } = configServiceApi.useGetConfigByIdQuery(id) + const [createUpdateConfig] = configServiceApi.useCreateUpdateConfigMutation() + const { state } = useLocation() + const [showConfirmModal, setShowConfirmModal] = useState(false) + const [showSchemeModal, setShowSchemeModal] = useState(false) + const [radioValue, setRadioValue] = useState('json') + const [bufConfig, setBufConfig] = useState() + const [detailsErrors, setDetailsError] = useState({ details: {}, isOpenDetailsErrorModal: false }) + const [disableBtn, setDisableBtn] = useState(false) + + useEffect(() => { + setBufConfig(isNew ? {} : sortObject(currentConfig)) + }, [isCurrentConfigLoading]) + + const itemsSaveBtn = [ + { + key: 'save', + label: 'Сохранить' + }, + { + key: 'unsafe', + label: 'Сохранить небезопасно' + } + ] + + const onSaveBtn: MenuProps['onClick'] = (e, upVersion = undefined) => { + const isUnsafe = e.key === 'unsafe' + handleSaveClick(upVersion, isUnsafe) + } + + const onRadioChange = (e: RadioChangeEvent) => { + setRadioValue(e.target.value) + } + + const handleSaveClick = (upVersion?: number, unsafe?: boolean) => { + let newData = {} + if (bufConfig) { + if (isNew) { + newData = { + name: state.name, + moduleId: moduleId, + id: '', + version: 0, + data: bufConfig.data, + unsafe: unsafe + } as ConfigType + } + if (currentConfig) { + newData = { + ...bufConfig, + version: upVersion ? upVersion : currentConfig.version, + unsafe: unsafe + } as ConfigType + } + } + createUpdateConfig(cleanEmptyParamsObject(newData)) + .unwrap() + .then(({ id }) => { + message.success('Конфигурация успешно сохранена') + isNew && navigate(`/${moduleId}/configEditor/${id}`) + }) + .catch((e) => { + if (e.data.errorCode === 2004) { + setShowConfirmModal(true) + } + if (e.data.errorCode === 2003) { + setDetailsError({ details: e.data.details, isOpenDetailsErrorModal: true }) + } + if (e.data.errorCode === 400) { + message.error('Невалидный JSON объект') + } + }) + } + + if (isLoadingJsonSchema || isCurrentConfigLoading) { + return + } + + + const renderContent = () => { + + switch (radioValue) { + // case 'form': + // return ( + // + // ) + case 'json': + return ( + + ) + case 'code': + return ( + + ) + } + } + + + return ( +
+
+ +
+ + { + handleSaveClick() + navigate(`/modules/${moduleId}/configurations`) + }} + disabled={disableBtn} + type="primary" + className="configuration-editor-page__save-btn" + menu={{ items: itemsSaveBtn, onClick: onSaveBtn }} + > + Сохранить и выйти + +
+ +
+
+ + Форма + JSON + Редактор кода + + + {renderContent()} +
+ + + setShowSchemeModal(false)} + /> + + setShowConfirmModal(false)} /> + + setDetailsError({details: {}, isOpenDetailsErrorModal: false })} /> + +
+ ) +} + +export default ConfigurationEditorPage