import { useLayoutEffect, useState } from 'react'; import { TabList, TabPanel, Tabs } from 'react-tabs'; import ConceptTab from '../../components/Common/ConceptTab'; import ConceptTooltip from '../../components/Common/ConceptTooltip'; import Modal, { ModalProps } from '../../components/Common/Modal'; import HelpRSTemplates from '../../components/Help/HelpRSTemplates'; import { HelpIcon } from '../../components/Icons'; import usePartialUpdate from '../../hooks/usePartialUpdate'; import { CstType, ICstCreateData, IRSForm } from '../../models/rsform'; import { inferTemplatedType, substituteTemplateArgs } from '../../models/rslangAPI'; import { createAliasFor, validateCstAlias } from '../../utils/misc'; import ArgumentsTab, { IArgumentsState } from './ArgumentsTab'; import ConstituentaTab from './ConstituentaTab'; import TemplateTab, { ITemplateState } from './TemplateTab'; interface DlgConstituentaTemplateProps extends Pick { schema: IRSForm onCreate: (data: ICstCreateData) => void insertAfter?: number } export enum TabID { TEMPLATE = 0, ARGUMENTS = 1, CONSTITUENTA = 2 } function DlgConstituentaTemplate({ hideWindow, schema, onCreate, insertAfter }: DlgConstituentaTemplateProps) { const [validated, setValidated] = useState(false); const [template, updateTemplate] = usePartialUpdate({}); const [substitutes, updateSubstitutes] = usePartialUpdate({ definition: '', arguments: [] }); const [constituenta, updateConstituenta] = usePartialUpdate({ cst_type: CstType.TERM, insert_after: insertAfter ?? null, alias: '', convention: '', definition_formal: '', definition_raw: '', term_raw: '', term_forms: [] }); const [activeTab, setActiveTab] = useState(TabID.TEMPLATE); const handleSubmit = () => onCreate(constituenta); useLayoutEffect( () => { updateConstituenta({ alias: createAliasFor(constituenta.cst_type, schema) }); }, [constituenta.cst_type, updateConstituenta, schema]); useLayoutEffect( () => { setValidated(validateCstAlias(constituenta.alias, constituenta.cst_type, schema)); }, [constituenta.alias, constituenta.cst_type, schema]); useLayoutEffect( () => { if (!template.prototype) { updateConstituenta({ definition_raw: '', definition_formal: '', term_raw: '' }); updateSubstitutes({ definition: '', arguments: [] }); } else { updateConstituenta({ cst_type: template.prototype.cst_type, definition_raw: template.prototype.definition_raw, definition_formal: template.prototype.definition_formal, term_raw: template.prototype.term_raw }); updateSubstitutes({ definition: template.prototype.definition_formal, arguments: template.prototype.parse.args.map( arg => ({ alias: arg.alias, typification: arg.typification, value: '' }) ) }); } }, [template.prototype, updateConstituenta, updateSubstitutes]); useLayoutEffect( () => { if (substitutes.arguments.length === 0 || !template.prototype) { return; } const definition = substituteTemplateArgs(template.prototype.definition_formal, substitutes.arguments); const type = inferTemplatedType(template.prototype.cst_type, substitutes.arguments); updateConstituenta({ cst_type: type, definition_formal: definition, }); updateSubstitutes({ definition: definition, }); }, [substitutes.arguments, template.prototype, updateConstituenta, updateSubstitutes]); return (
); } export default DlgConstituentaTemplate;