From e28f3b3bf9df08991dcf73fa0d297bc81250b69b Mon Sep 17 00:00:00 2001 From: mikheikinnv Date: Fri, 14 Jun 2024 11:41:10 +0300 Subject: [PATCH] MSP-3205 add ConfigurationEditorCode component --- .../ConfigurationEditorCode/index.tsx | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/components/ConfigurationEditorCode/index.tsx diff --git a/src/components/ConfigurationEditorCode/index.tsx b/src/components/ConfigurationEditorCode/index.tsx new file mode 100644 index 0000000..891f258 --- /dev/null +++ b/src/components/ConfigurationEditorCode/index.tsx @@ -0,0 +1,62 @@ +import { FC } from 'react' +import { ConfigurationEditorPropsType } from '@pages/ConfigurationEditorPage' +import Editor from '@monaco-editor/react' +import { ConfigType } from '@pages/ModulesPage/module.type.ts' +import { message } from 'antd' + +const ConfigurationEditorCode: FC = ({setDisableBtn= () => {}, bufConfig, setBufConfig }) => { + const debounce = (func: (...args: any[]) => void, wait: number) => { + let timeout: NodeJS.Timeout + return (...args: []) => { + clearTimeout(timeout) + timeout = setTimeout(() => { + func.apply(this, [...args]) + }, wait) + } + } + + const debouncedHandleError = debounce(() => { + message.error('Невалидный JSON объект') + }, 500) + + const handleChange = (data: string | undefined) => { + + if (data) { + try { + const newData = { + ...bufConfig, + data: {...JSON.parse(data) } + } as ConfigType + setDisableBtn(false) + setBufConfig(newData) + } catch (error) { + debouncedHandleError() + setDisableBtn(true) + } + } + } + + const editorValue = JSON.stringify(bufConfig?.data, null, '\t') + + return ( +
+ +
+ ) +} + +export default ConfigurationEditorCode \ No newline at end of file