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

92 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-08-23 01:36:17 +03:00
import { useLayoutEffect, useState } from 'react';
2023-08-22 23:45:59 +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';
2023-08-22 23:45:59 +03:00
import TextInput from '../../components/Common/TextInput';
2023-08-23 01:36:17 +03:00
import { useRSForm } from '../../context/RSFormContext';
2023-09-11 20:31:54 +03:00
import { CstType, ICstRenameData } from '../../models/rsform';
2023-09-14 16:53:38 +03:00
import { SelectorCstType } from '../../utils/selectors';
import { createAliasFor, getCstTypeLabel, getCstTypePrefix } from '../../utils/staticUI';
2023-08-22 23:45:59 +03:00
2023-09-11 17:56:32 +03:00
interface DlgRenameCstProps
extends Pick<ModalProps, 'hideWindow'> {
2023-08-23 01:36:17 +03:00
initial?: ICstRenameData
2023-08-22 23:45:59 +03:00
onRename: (data: ICstRenameData) => void
}
function DlgRenameCst({ hideWindow, initial, onRename }: DlgRenameCstProps) {
2023-08-23 01:36:17 +03:00
const { schema } = useRSForm();
2023-08-22 23:45:59 +03:00
const [validated, setValidated] = useState(false);
const [cstType, setCstType] = useState<CstType>(CstType.BASE);
const [cstID, setCstID] = useState(0)
const [alias, setAlias] = useState('');
function getData(): ICstRenameData {
return {
cst_type: cstType,
alias: alias,
id: cstID
}
}
2023-08-23 01:36:17 +03:00
const handleSubmit = () => onRename(getData());
2023-08-22 23:45:59 +03:00
2023-08-23 01:36:17 +03:00
useLayoutEffect(
2023-08-27 16:35:17 +03:00
() => {
if (schema && initial && cstType !== initial.cst_type) {
setAlias(createAliasFor(cstType, schema));
}
}, [initial, cstType, schema]);
2023-08-23 01:36:17 +03:00
useLayoutEffect(
() => {
2023-08-22 23:45:59 +03:00
if (initial) {
setCstType(initial.cst_type);
setAlias(initial.alias);
setCstID(initial.id);
}
}, [initial]);
2023-08-23 01:36:17 +03:00
useLayoutEffect(
() => {
if (!initial || !schema) {
setValidated(false);
} else if(alias === initial.alias || alias.length < 2 || alias[0] !== getCstTypePrefix(cstType)) {
setValidated(false);
} else {
setValidated(!schema.items.find(cst => cst.alias === alias))
}
}, [cstType, alias, initial, schema]);
2023-08-22 23:45:59 +03:00
return (
<Modal
2023-08-23 01:36:17 +03:00
title='Переименование конституенты'
2023-08-22 23:45:59 +03:00
hideWindow={hideWindow}
canSubmit={validated}
onSubmit={handleSubmit}
2023-09-11 17:56:32 +03:00
submitInvalidTooltip={'Введите незанятое имя, соответствующее типу'}
2023-08-23 01:36:17 +03:00
submitText='Переименовать'
2023-08-22 23:45:59 +03:00
>
2023-08-27 15:39:49 +03:00
<div className='flex items-center gap-4 px-2 my-2 h-fit min-w-[25rem]'>
2023-09-14 16:53:38 +03:00
<SelectSingle
2023-09-11 17:56:32 +03:00
className='min-w-[14rem] self-center z-modal-top'
2023-09-14 16:53:38 +03:00
options={SelectorCstType}
2023-08-23 01:36:17 +03:00
placeholder='Выберите тип'
value={cstType ? { value: cstType, label: getCstTypeLabel(cstType) } : null}
onChange={data => setCstType(data?.value ?? CstType.BASE)}
2023-08-23 01:36:17 +03:00
/>
<div>
<TextInput id='alias' label='Имя'
singleRow
dimensions='w-[7rem]'
2023-08-23 01:36:17 +03:00
value={alias}
onChange={event => setAlias(event.target.value)}
/>
</div>
2023-08-22 23:45:59 +03:00
</div>
</Modal>
);
}
export default DlgRenameCst;