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