ConceptPortal-public/rsconcept/frontend/src/dialogs/DlgConstituentaTemplate/index.tsx

181 lines
5.7 KiB
TypeScript
Raw Normal View History

2023-11-07 10:05:50 +03:00
import { useLayoutEffect, useState } from 'react';
2023-11-06 02:20:16 +03:00
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';
2023-11-06 02:20:16 +03:00
import { createAliasFor, validateCstAlias } from '../../utils/misc';
import ArgumentsTab, { IArgumentsState } from './ArgumentsTab';
import ConstituentaTab from './ConstituentaTab';
import TemplateTab, { ITemplateState } from './TemplateTab';
interface DlgConstituentaTemplateProps
extends Pick<ModalProps, 'hideWindow'> {
schema: IRSForm
onCreate: (data: ICstCreateData) => void
}
export enum TabID {
TEMPLATE = 0,
ARGUMENTS = 1,
CONSTITUENTA = 2
}
function DlgConstituentaTemplate({ hideWindow, schema, onCreate }: DlgConstituentaTemplateProps) {
2023-11-07 10:05:50 +03:00
const [ validated, setValidated ] = useState(false);
2023-11-06 18:44:14 +03:00
const [ template, updateTemplate ] = usePartialUpdate<ITemplateState>({});
const [ substitutes, updateSubstitutes ] = usePartialUpdate<IArgumentsState>({
definition: '',
arguments: []
2023-11-06 02:20:16 +03:00
});
2023-11-06 18:44:14 +03:00
const [constituenta, updateConstituenta] = usePartialUpdate<ICstCreateData>({
2023-11-06 02:20:16 +03:00
cst_type: CstType.TERM,
insert_after: null,
alias: '',
convention: '',
definition_formal: '',
definition_raw: '',
term_raw: '',
term_forms: []
});
const [ activeTab, setActiveTab ] = useState(TabID.TEMPLATE);
2023-11-06 18:44:14 +03:00
const handleSubmit = () => onCreate(constituenta);
2023-11-06 02:20:16 +03:00
useLayoutEffect(
() => {
2023-11-06 18:44:14 +03:00
updateConstituenta({ alias: createAliasFor(constituenta.cst_type, schema) });
}, [constituenta.cst_type, updateConstituenta, schema]);
2023-11-06 02:20:16 +03:00
2023-11-07 10:05:50 +03:00
useLayoutEffect(
2023-11-06 02:20:16 +03:00
() => {
2023-11-06 18:44:14 +03:00
setValidated(validateCstAlias(constituenta.alias, constituenta.cst_type, schema));
}, [constituenta.alias, constituenta.cst_type, schema]);
2023-11-06 02:20:16 +03:00
useLayoutEffect(
() => {
2023-11-06 18:44:14 +03:00
if (!template.prototype) {
updateConstituenta({
2023-11-06 02:20:16 +03:00
definition_raw: '',
definition_formal: '',
term_raw: ''
});
2023-11-06 18:44:14 +03:00
updateSubstitutes({
2023-11-06 02:20:16 +03:00
definition: '',
arguments: []
});
} else {
2023-11-06 18:44:14 +03:00
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
2023-11-06 02:20:16 +03:00
});
2023-11-06 18:44:14 +03:00
updateSubstitutes({
definition: template.prototype.definition_formal,
arguments: template.prototype.parse.args.map(
arg => ({
alias: arg.alias,
typification: arg.typification,
value: ''
})
)
2023-11-06 02:20:16 +03:00
});
}
2023-11-06 18:44:14 +03:00
}, [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]);
2023-11-06 02:20:16 +03:00
return (
<Modal
title='Создание конституенты из шаблона'
hideWindow={hideWindow}
canSubmit={validated}
onSubmit={handleSubmit}
submitText='Создать'
>
<div className='max-w-[40rem] min-w-[40rem] min-h-[35rem] px-2 mb-1'>
<Tabs
selectedIndex={activeTab}
onSelect={setActiveTab}
defaultFocus
2023-11-07 10:05:50 +03:00
forceRenderTabPanel
2023-11-06 02:20:16 +03:00
selectedTabClassName='clr-selected'
className='flex flex-col items-center'
>
<div className='flex gap-1 pl-6 mb-3'>
<TabList className='flex items-start font-semibold text-center border select-none clr-controls small-caps'>
<ConceptTab tooltip='Выбор шаблона выражения' className='border-r w-[8rem]'>
Шаблон
</ConceptTab>
<ConceptTab tooltip='Подстановка аргументов шаблона' className='border-r w-[8rem]'>
Аргументы
</ConceptTab>
<ConceptTab tooltip='Редактирование атрибутов конституенты' className='w-[8rem]'>
Конституента
</ConceptTab>
</TabList>
<div id='templates-help' className='px-1 py-1'>
<HelpIcon color='text-primary' size={5} />
</div>
<ConceptTooltip
anchorSelect='#templates-help'
className='max-w-[30rem] z-modal-tooltip'
offset={4}
>
<HelpRSTemplates />
</ConceptTooltip>
</div>
<div className='w-full'>
2023-11-07 10:05:50 +03:00
<TabPanel style={{ display: activeTab === TabID.TEMPLATE ? '': 'none' }}>
2023-11-06 02:20:16 +03:00
<TemplateTab
2023-11-06 18:44:14 +03:00
state={template}
partialUpdate={updateTemplate}
2023-11-06 02:20:16 +03:00
/>
</TabPanel>
2023-11-07 10:05:50 +03:00
<TabPanel style={{ display: activeTab === TabID.ARGUMENTS ? '': 'none' }}>
2023-11-06 02:20:16 +03:00
<ArgumentsTab
schema={schema}
2023-11-06 18:44:14 +03:00
state={substitutes}
partialUpdate={updateSubstitutes}
2023-11-06 02:20:16 +03:00
/>
</TabPanel>
2023-11-07 10:05:50 +03:00
<TabPanel style={{ display: activeTab === TabID.CONSTITUENTA ? '': 'none' }}>
2023-11-06 02:20:16 +03:00
<ConstituentaTab
2023-11-06 18:44:14 +03:00
state={constituenta}
partialUpdate={updateConstituenta}
2023-11-06 02:20:16 +03:00
/>
</TabPanel>
</div>
</Tabs>
</div>
</Modal>);
}
export default DlgConstituentaTemplate;