import { useCallback, useEffect, useState } from 'react'; import { useRSForm } from '../../context/RSFormContext'; import { CstType, EditMode, INewCstData } from '../../utils/models'; import { toast } from 'react-toastify'; import TextArea from '../../components/Common/TextArea'; import ExpressionEditor from './ExpressionEditor'; import SubmitButton from '../../components/Common/SubmitButton'; import { createAliasFor, getCstTypeLabel } from '../../utils/staticUI'; import ConstituentsSideList from './ConstituentsSideList'; import { DumpBinIcon, SaveIcon, SmallPlusIcon } from '../../components/Icons'; import CreateCstModal from './CreateCstModal'; import { AxiosResponse } from 'axios'; import { useNavigate } from 'react-router-dom'; import { RSFormTabsList } from './RSFormTabs'; function ConstituentEditor() { const navigate = useNavigate(); const { active, schema, setActive, processing, isEditable, reload, 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'); useEffect(() => { if (schema?.items && schema?.items.length > 0) { setActive((prev) => (prev || schema?.items![0])); } }, [schema, setActive]) useEffect(() => { if (active) { setAlias(active.alias); setType(getCstTypeLabel(active.cstType)); setConvention(active.convention || ''); setTerm(active.term?.raw || ''); setTextDefinition(active.definition?.text?.raw || ''); setExpression(active.definition?.formal || ''); setTypification(active?.parse?.typification || 'N/A'); } }, [active]); const handleSubmit = async (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': active?.term?.forms || [], } }; cstUpdate(data) .then(() => { toast.success('Изменения сохранены'); reload(); }); } }; const handleDelete = useCallback( async () => { if (!active || !window.confirm('Вы уверены, что хотите удалить конституенту?')) { return; } const data = { 'items': [active.entityUID] } const index = schema?.items?.indexOf(active) await cstDelete(data); if (schema?.items && index && index + 1 < schema?.items?.length) { setActive(schema?.items[index + 1]); } toast.success(`Конституента удалена: ${active.alias}`); reload(); }, [active, schema, setActive, cstDelete, reload]); const handleAddNew = useCallback( async (csttype?: CstType) => { if (!active || !schema) { return; } if (!csttype) { setShowCstModal(true); } else { const data: INewCstData = { 'csttype': csttype, 'alias': createAliasFor(csttype, schema!), 'insert_after': active.entityUID } cstCreate(data, (response: AxiosResponse) => { navigate(`/rsforms/${schema.id}?tab=${RSFormTabsList.CST_EDIT}&active=${response.data['entityUID']}`); window.location.reload(); }); } }, [active, schema, cstCreate, navigate]); const handleRename = useCallback(() => { toast.info('Переименование в разработке'); }, []); const handleChangeType = useCallback(() => { toast.info('Изменение типа в разработке'); }, []); return (
setShowCstModal(!showCstModal)} onCreate={handleAddNew} defaultType={active?.cstType as CstType} />
{alias} {type}