MSP-3423 fix ConfigurationEditorPage
This commit is contained in:
1 parent
a39327be36
commit
f1abba6352
1 file changed
+79
-55
@@ -1,51 +1,60 @@
|
||||
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 {
|
||||
Button,
|
||||
Dropdown,
|
||||
MenuProps,
|
||||
message,
|
||||
Radio,
|
||||
RadioChangeEvent,
|
||||
Spin
|
||||
} from 'antd'
|
||||
import { FC, useEffect, useRef, useState } from 'react'
|
||||
import { useLocation, useNavigate, useParams } from 'react-router-dom'
|
||||
|
||||
import ConfigSchemaModal from '@components/ConfigSchemaModal'
|
||||
import ConfigurationEditorCode from '@components/ConfigurationEditorCode'
|
||||
import ConfigurationEditorForm from '@components/ConfigurationEditorForm'
|
||||
import ConfigurationEditorJson from '@components/ConfigurationEditorJson'
|
||||
import ConfirmConfigModal from '@components/ConfirmConfigModal/ConfirmConfigModal.tsx'
|
||||
import ErrorConfigModal from '@components/ErrorConfigModal/ErrorConfigModal.tsx'
|
||||
|
||||
import { ConfigType } from '@pages/ModulesPage/module.type.ts'
|
||||
|
||||
import { cleanEmptyParamsObject, sortObject } from '@utils/objectUtils.ts'
|
||||
|
||||
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<SetStateAction<ConfigType | undefined>>
|
||||
isCurrentConfigLoading: boolean
|
||||
setDisableBtn?: Dispatch<SetStateAction<boolean>>
|
||||
}
|
||||
|
||||
const ConfigurationEditorPage: FC = () => {
|
||||
const navigate = useNavigate()
|
||||
const { moduleId = '' } = useParams()
|
||||
const { id = '' } = useParams()
|
||||
const isNew = id === 'new'
|
||||
const { data: JsonSchema, isLoading: isLoadingJsonSchema } =
|
||||
const { data: jsonSchema, isLoading: isLoadingJsonSchema } =
|
||||
modulesServiceApi.useGetByModuleIdQuery(moduleId)
|
||||
const {
|
||||
data: currentConfig,
|
||||
isLoading: isCurrentConfigLoading,
|
||||
} = configServiceApi.useGetConfigByIdQuery(id)
|
||||
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<ConfigType>()
|
||||
const [detailsErrors, setDetailsError] = useState({ details: {}, isOpenDetailsErrorModal: false })
|
||||
const [detailsErrors, setDetailsError] = useState({
|
||||
details: {},
|
||||
isOpenDetailsErrorModal: false
|
||||
})
|
||||
const [disableBtn, setDisableBtn] = useState(false)
|
||||
const submitRef = useRef<any>(null)
|
||||
|
||||
const formRadio = 'form'
|
||||
const codeRadio = 'code'
|
||||
const jsonRadio = 'json'
|
||||
|
||||
useEffect(() => {
|
||||
setBufConfig(isNew ? {} : sortObject(currentConfig))
|
||||
setBufConfig(isNew ? {} : sortObject(currentConfig))
|
||||
}, [isCurrentConfigLoading])
|
||||
|
||||
const itemsSaveBtn = [
|
||||
@@ -65,11 +74,19 @@ const ConfigurationEditorPage: FC = () => {
|
||||
}
|
||||
|
||||
const onRadioChange = (e: RadioChangeEvent) => {
|
||||
if (!bufConfig) return
|
||||
if (submitRef.current && radioValue === formRadio) {
|
||||
setBufConfig({ ...bufConfig, data: { ...submitRef.current } })
|
||||
}
|
||||
setRadioValue(e.target.value)
|
||||
}
|
||||
|
||||
const handleSaveClick = (upVersion?: number, unsafe?: boolean) => {
|
||||
let newData = {}
|
||||
const sentData =
|
||||
radioValue === formRadio
|
||||
? { ...bufConfig, data: { ...submitRef.current } }
|
||||
: { ...bufConfig }
|
||||
if (bufConfig) {
|
||||
if (isNew) {
|
||||
newData = {
|
||||
@@ -77,13 +94,13 @@ const ConfigurationEditorPage: FC = () => {
|
||||
moduleId: moduleId,
|
||||
id: '',
|
||||
version: 0,
|
||||
data: bufConfig.data,
|
||||
data: sentData.data,
|
||||
unsafe: unsafe
|
||||
} as ConfigType
|
||||
}
|
||||
if (currentConfig) {
|
||||
newData = {
|
||||
...bufConfig,
|
||||
...sentData,
|
||||
version: upVersion ? upVersion : currentConfig.version,
|
||||
unsafe: unsafe
|
||||
} as ConfigType
|
||||
@@ -100,29 +117,35 @@ const ConfigurationEditorPage: FC = () => {
|
||||
setShowConfirmModal(true)
|
||||
}
|
||||
if (e.data.errorCode === 2003) {
|
||||
setDetailsError({ details: e.data.details, isOpenDetailsErrorModal: true })
|
||||
setDetailsError({
|
||||
details: e.data.details,
|
||||
isOpenDetailsErrorModal: true
|
||||
})
|
||||
}
|
||||
if (e.data.errorCode === 400) {
|
||||
message.error('Невалидный JSON объект')
|
||||
}
|
||||
message.error('Ошибка обновления элемента')
|
||||
})
|
||||
}
|
||||
|
||||
if (isLoadingJsonSchema || isCurrentConfigLoading) {
|
||||
return <Spin className="spin" />
|
||||
}
|
||||
|
||||
|
||||
const renderContent = () => {
|
||||
|
||||
switch (radioValue) {
|
||||
// case 'form':
|
||||
// return (
|
||||
// <ConfigurationEditorForm
|
||||
// bufConfig={bufConfig}
|
||||
// setBufConfig={setBufConfig}
|
||||
// isCurrentConfigLoading={isCurrentConfigLoading}
|
||||
// />
|
||||
// )
|
||||
case 'json':
|
||||
case formRadio:
|
||||
return (
|
||||
<ConfigurationEditorForm
|
||||
submitRef={submitRef}
|
||||
bufConfig={bufConfig}
|
||||
setBufConfig={setBufConfig}
|
||||
jsonSchema={jsonSchema}
|
||||
isCurrentConfigLoading={isCurrentConfigLoading}
|
||||
setDisableBtn={setDisableBtn}
|
||||
/>
|
||||
)
|
||||
case jsonRadio:
|
||||
return (
|
||||
<ConfigurationEditorJson
|
||||
bufConfig={bufConfig}
|
||||
@@ -130,7 +153,7 @@ const ConfigurationEditorPage: FC = () => {
|
||||
isCurrentConfigLoading={isCurrentConfigLoading}
|
||||
/>
|
||||
)
|
||||
case 'code':
|
||||
case codeRadio:
|
||||
return (
|
||||
<ConfigurationEditorCode
|
||||
setDisableBtn={setDisableBtn}
|
||||
@@ -142,7 +165,6 @@ const ConfigurationEditorPage: FC = () => {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<main className="configuration-editor-page">
|
||||
<section className="configuration-editor-page__header">
|
||||
@@ -153,17 +175,14 @@ const ConfigurationEditorPage: FC = () => {
|
||||
<h1>{bufConfig?.name}</h1>
|
||||
<div className="configuration-editor-page__controll">
|
||||
<Button
|
||||
disabled={!JsonSchema}
|
||||
disabled={!jsonSchema}
|
||||
className="configurations____buttons__show-schema-btn"
|
||||
onClick={() => setShowSchemeModal(true)}
|
||||
>
|
||||
Текущая схема конфигурации
|
||||
</Button>
|
||||
<Dropdown.Button
|
||||
onClick={() => {
|
||||
handleSaveClick()
|
||||
navigate(`/modules/${moduleId}/configurations`)
|
||||
}}
|
||||
onClick={() => handleSaveClick()}
|
||||
disabled={disableBtn}
|
||||
type="primary"
|
||||
className="configuration-editor-page__save-btn"
|
||||
@@ -172,21 +191,21 @@ const ConfigurationEditorPage: FC = () => {
|
||||
Сохранить и выйти
|
||||
</Dropdown.Button>
|
||||
</div>
|
||||
|
||||
</section>
|
||||
<div className="configuration-editor-page__content">
|
||||
<Radio.Group defaultValue="json" size="large" onChange={onRadioChange}>
|
||||
<Radio.Button disabled value="form">Форма</Radio.Button>
|
||||
<Radio.Button disabled={disableBtn} value="json">JSON</Radio.Button>
|
||||
<Radio.Button value="code">Редактор кода</Radio.Button>
|
||||
<Radio.Group defaultValue={jsonRadio} disabled={disableBtn} size="large" onChange={onRadioChange}>
|
||||
<Radio.Button value={formRadio}>Форма</Radio.Button>
|
||||
<Radio.Button disabled={disableBtn} value={jsonRadio}>
|
||||
JSON
|
||||
</Radio.Button>
|
||||
<Radio.Button value={codeRadio}>Редактор кода</Radio.Button>
|
||||
</Radio.Group>
|
||||
|
||||
{renderContent()}
|
||||
</div>
|
||||
|
||||
|
||||
<ConfigSchemaModal
|
||||
schema={JsonSchema?.schema}
|
||||
schema={jsonSchema?.schema}
|
||||
open={showSchemeModal}
|
||||
onClose={() => setShowSchemeModal(false)}
|
||||
/>
|
||||
@@ -196,11 +215,16 @@ const ConfigurationEditorPage: FC = () => {
|
||||
setBufConfig={setBufConfig}
|
||||
open={showConfirmModal}
|
||||
handleSaveClick={handleSaveClick}
|
||||
onClose={() => setShowConfirmModal(false)} />
|
||||
|
||||
<ErrorConfigModal details={detailsErrors.details} open={detailsErrors.isOpenDetailsErrorModal}
|
||||
onClose={() => setDetailsError({details: {}, isOpenDetailsErrorModal: false })} />
|
||||
onClose={() => setShowConfirmModal(false)}
|
||||
/>
|
||||
|
||||
<ErrorConfigModal
|
||||
details={detailsErrors.details}
|
||||
open={detailsErrors.isOpenDetailsErrorModal}
|
||||
onClose={() =>
|
||||
setDetailsError({ details: {}, isOpenDetailsErrorModal: false })
|
||||
}
|
||||
/>
|
||||
</main>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user