ConceptPortal-public/rsconcept/frontend/src/dialogs/DlgRenameCst.tsx

93 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-11-01 13:47:49 +03:00
import Modal, { ModalProps } from '../components/Common/Modal';
import SelectSingle from '../components/Common/SelectSingle';
import TextInput from '../components/Common/TextInput';
import { useRSForm } from '../context/RSFormContext';
import { CstType, ICstRenameData } from '../models/rsform';
import { labelCstType } from '../utils/labels';
import { createAliasFor, getCstTypePrefix } from '../utils/misc';
import { SelectorCstType } from '../utils/selectors';
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='Выберите тип'
2023-09-21 14:58:01 +03:00
value={cstType ? { value: cstType, label: labelCstType(cstType) } : null}
onChange={data => setCstType(data?.value ?? CstType.BASE)}
2023-08-23 01:36:17 +03:00
/>
<div>
<TextInput id='alias' label='Имя'
2023-10-23 18:22:55 +03:00
dense
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;