import { type AxiosResponse } from 'axios'; import { useCallback, useLayoutEffect, useState } from 'react'; import { toast } from 'react-toastify'; import SubmitButton from '../../components/Common/SubmitButton'; import TextArea from '../../components/Common/TextArea'; import { DumpBinIcon, SaveIcon, SmallPlusIcon } from '../../components/Icons'; import { useRSForm } from '../../context/RSFormContext'; import { type CstType, EditMode, type INewCstData } from '../../utils/models'; import { createAliasFor, getCstTypeLabel } from '../../utils/staticUI'; import ConstituentsSideList from './ConstituentsSideList'; import CreateCstModal from './CreateCstModal'; import ExpressionEditor from './ExpressionEditor'; function ConstituentEditor() { const { activeCst, activeID, schema, setActiveID, processing, isEditable, cstDelete, cstUpdate, cstCreate } = useRSForm(); const [showCstModal, setShowCstModal] = useState(false); const [editMode, setEditMode] = useState(EditMode.TEXT); const [alias, setAlias] = useState(''); const [type, setType] = useState(''); const [term, setTerm] = useState(''); const [textDefinition, setTextDefinition] = useState(''); const [expression, setExpression] = useState(''); const [convention, setConvention] = useState(''); const [typification, setTypification] = useState('N/A'); useLayoutEffect(() => { if (schema?.items && schema?.items.length > 0) { // TODO: figure out why schema.items could be undef? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion setActiveID((prev) => (prev ?? schema?.items![0].id ?? undefined)); } }, [schema, setActiveID]) useLayoutEffect(() => { if (activeCst) { setAlias(activeCst.alias); setType(getCstTypeLabel(activeCst.cstType)); setConvention(activeCst.convention ?? ''); setTerm(activeCst.term?.raw ?? ''); setTextDefinition(activeCst.definition?.text?.raw ?? ''); setExpression(activeCst.definition?.formal ?? ''); setTypification(activeCst?.parse?.typification ?? 'N/A'); } }, [activeCst]); const handleSubmit = (event: React.FormEvent) => { event.preventDefault(); if (!processing) { const data = { alias: alias, convention: convention, definition_formal: expression, definition_text: { raw: textDefinition, resolved: '' }, term: { raw: term, resolved: '', forms: activeCst?.term?.forms ?? [] } }; cstUpdate(data, () => toast.success('Изменения сохранены')); } }; const handleDelete = useCallback( () => { if (!activeID || !schema?.items || !window.confirm('Вы уверены, что хотите удалить конституенту?')) { return; } const data = { items: [{ id: activeID }] } const index = schema.items.findIndex((cst) => cst.id === activeID); if (index !== -1 && index + 1 < schema.items.length) { setActiveID(schema.items[index + 1].id); } cstDelete(data, () => toast.success('Конституента удалена')); }, [activeID, schema, setActiveID, cstDelete]); const handleAddNew = useCallback( (csttype?: CstType) => { if (!activeID || !schema?.items) { return; } if (!csttype) { setShowCstModal(true); } else { const data: INewCstData = { csttype: csttype, alias: createAliasFor(csttype, schema), insert_after: activeID } cstCreate(data, (response: AxiosResponse) => { setActiveID(response.data.new_cst.id); toast.success(`Конституента добавлена: ${response.data.new_cst.alias as string}`); }); } }, [activeID, schema, cstCreate, setActiveID]); const handleRename = useCallback(() => { toast.info('Переименование в разработке'); }, []); const handleChangeType = useCallback(() => { toast.info('Изменение типа в разработке'); }, []); return (
{ setShowCstModal(!showCstModal); }} onCreate={handleAddNew} defaultType={activeCst?.cstType as CstType} />
{alias} {type}