ConceptPortal-public/rsconcept/frontend/src/pages/RSFormPage/DlgCreateCst.tsx

126 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-10-08 15:24:41 +03:00
import { useEffect, useLayoutEffect, useState } from 'react';
2023-07-25 20:27:29 +03:00
2023-09-11 17:56:32 +03:00
import Modal, { ModalProps } from '../../components/Common/Modal';
2023-09-14 16:53:38 +03:00
import SelectSingle from '../../components/Common/SelectSingle';
import TextArea from '../../components/Common/TextArea';
2023-10-08 15:24:41 +03:00
import TextInput from '../../components/Common/TextInput';
import RSInput from '../../components/RSInput';
2023-10-08 15:24:41 +03:00
import { CstType,ICstCreateData, IRSForm } from '../../models/rsform';
2023-09-21 14:58:01 +03:00
import { labelCstType } from '../../utils/labels';
2023-10-08 15:24:41 +03:00
import { createAliasFor, getCstTypePrefix } from '../../utils/misc';
2023-09-29 15:33:32 +03:00
import { SelectorCstType } from '../../utils/selectors';
2023-07-22 12:24:14 +03:00
2023-09-11 17:56:32 +03:00
interface DlgCreateCstProps
extends Pick<ModalProps, 'hideWindow'> {
initial?: ICstCreateData
2023-10-08 15:24:41 +03:00
schema: IRSForm
onCreate: (data: ICstCreateData) => void
2023-07-22 12:24:14 +03:00
}
2023-10-08 15:24:41 +03:00
function DlgCreateCst({ hideWindow, initial, schema, onCreate }: DlgCreateCstProps) {
2023-07-22 12:24:14 +03:00
const [validated, setValidated] = useState(false);
const [selectedType, setSelectedType] = useState<CstType>(CstType.BASE);
2023-10-08 15:24:41 +03:00
const [alias, setAlias] = useState('');
const [term, setTerm] = useState('');
const [textDefinition, setTextDefinition] = useState('');
const [expression, setExpression] = useState('');
const [convention, setConvention] = useState('');
function getData(): ICstCreateData {
return {
cst_type: selectedType,
insert_after: initial?.insert_after ?? null,
2023-10-08 15:24:41 +03:00
alias: alias,
convention: convention,
definition_formal: expression,
definition_raw: textDefinition,
term_raw: term
2023-09-11 17:56:32 +03:00
};
}
2023-07-22 12:24:14 +03:00
2023-08-27 16:35:17 +03:00
const handleSubmit = () => onCreate(getData());
2023-07-22 12:24:14 +03:00
2023-10-08 15:24:41 +03:00
useLayoutEffect(() => {
if (initial) {
setSelectedType(initial.cst_type);
setTerm(initial.term_raw);
setTextDefinition(initial.definition_raw);
setExpression(initial.definition_formal);
setConvention(initial.convention);
2023-10-08 15:24:41 +03:00
setAlias(initial.alias);
}
}, [initial]);
2023-10-08 15:24:41 +03:00
useLayoutEffect(
() => {
setAlias(createAliasFor(selectedType, schema));
}, [selectedType, schema]);
useEffect(
() => {
if(alias.length < 2 || alias[0] !== getCstTypePrefix(selectedType)) {
setValidated(false);
} else {
setValidated(!schema.items.find(cst => cst.alias === alias))
}
}, [alias, selectedType, schema]);
2023-07-22 12:24:14 +03:00
return (
2023-07-25 20:27:29 +03:00
<Modal
2023-07-22 12:24:14 +03:00
title='Создание конституенты'
2023-07-25 22:29:33 +03:00
hideWindow={hideWindow}
2023-07-22 12:24:14 +03:00
canSubmit={validated}
onSubmit={handleSubmit}
>
<div className='h-fit w-[35rem] px-2 mb-2 flex flex-col justify-stretch gap-3'>
2023-10-08 15:24:41 +03:00
<div className='flex justify-center w-full gap-6'>
2023-09-14 16:53:38 +03:00
<SelectSingle
className='my-2 min-w-[15rem] self-center'
2023-09-14 16:53:38 +03:00
options={SelectorCstType}
placeholder='Выберите тип'
2023-09-21 14:58:01 +03:00
value={selectedType ? { value: selectedType, label: labelCstType(selectedType) } : null}
onChange={data => setSelectedType(data?.value ?? CstType.BASE)}
/>
2023-10-08 15:24:41 +03:00
<TextInput id='alias' label='Имя'
singleRow
dimensions='w-[7rem]'
value={alias}
onChange={event => setAlias(event.target.value)}
/>
</div>
<TextArea id='term' label='Термин'
placeholder='Схемный или предметный термин, обозначающий данное понятие или утверждение'
rows={2}
value={term}
spellCheck
onChange={event => setTerm(event.target.value)}
/>
<RSInput id='expression' label='Формальное выражение'
2023-10-04 18:46:52 +03:00
placeholder='Родоструктурное выражение, задающее формальное определение'
editable
height='5.5rem'
value={expression}
onChange={value => setExpression(value)}
/>
<TextArea id='definition' label='Текстовое определение'
placeholder='Лингвистическая интерпретация формального выражения'
rows={2}
value={textDefinition}
spellCheck
2023-08-27 16:35:17 +03:00
onChange={event => setTextDefinition(event.target.value)}
/>
<TextArea id='convention' label='Конвенция / Комментарий'
placeholder='Договоренность об интерпретации неопределяемого понятия&#x000D;&#x000A;Комментарий к производному понятию'
rows={2}
value={convention}
spellCheck
2023-08-27 16:35:17 +03:00
onChange={event => setConvention(event.target.value)}
/>
</div>
2023-07-22 12:24:14 +03:00
</Modal>
2023-07-29 03:31:21 +03:00
);
2023-07-22 12:24:14 +03:00
}
2023-07-28 00:03:37 +03:00
export default DlgCreateCst;