2023-08-23 01:36:17 +03:00
|
|
|
import { useLayoutEffect, useState } from 'react';
|
2023-08-22 23:45:59 +03:00
|
|
|
|
2023-11-27 12:11:39 +03:00
|
|
|
import Modal, { ModalProps } from '../components/Common/Modal';
|
|
|
|
import SelectSingle from '../components/Common/SelectSingle';
|
|
|
|
import TextInput from '../components/Common/TextInput';
|
2023-11-01 13:47:49 +03:00
|
|
|
import { useRSForm } from '../context/RSFormContext';
|
2023-11-01 20:14:50 +03:00
|
|
|
import usePartialUpdate from '../hooks/usePartialUpdate';
|
2023-11-01 13:47:49 +03:00
|
|
|
import { CstType, ICstRenameData } from '../models/rsform';
|
|
|
|
import { labelCstType } from '../utils/labels';
|
2023-11-01 20:14:50 +03:00
|
|
|
import { createAliasFor, validateCstAlias } from '../utils/misc';
|
2023-11-01 13:47:49 +03:00
|
|
|
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-11-01 20:14:50 +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);
|
2023-11-01 20:14:50 +03:00
|
|
|
const [cstData, updateData] = usePartialUpdate(initial);
|
|
|
|
|
|
|
|
const handleSubmit = () => onRename(cstData);
|
2023-08-23 01:36:17 +03:00
|
|
|
|
|
|
|
useLayoutEffect(
|
|
|
|
() => {
|
2023-11-01 20:14:50 +03:00
|
|
|
if (schema && initial && cstData.cst_type !== initial.cst_type) {
|
|
|
|
updateData({ alias: createAliasFor(cstData.cst_type, schema)});
|
2023-08-22 23:45:59 +03:00
|
|
|
}
|
2023-11-01 20:14:50 +03:00
|
|
|
}, [initial, cstData.cst_type, updateData, schema]);
|
2023-08-22 23:45:59 +03:00
|
|
|
|
2023-08-23 01:36:17 +03:00
|
|
|
useLayoutEffect(
|
|
|
|
() => {
|
2023-11-01 20:14:50 +03:00
|
|
|
setValidated(
|
|
|
|
!!schema &&
|
|
|
|
cstData.alias !== initial.alias &&
|
|
|
|
validateCstAlias(cstData.alias, cstData.cst_type, schema)
|
|
|
|
);
|
|
|
|
}, [cstData.cst_type, cstData.alias, initial, schema]);
|
2023-08-22 23:45:59 +03:00
|
|
|
|
|
|
|
return (
|
2023-12-04 14:19:54 +03:00
|
|
|
<Modal
|
|
|
|
title='Переименование конституенты'
|
|
|
|
submitText='Переименовать'
|
|
|
|
submitInvalidTooltip={'Введите незанятое имя, соответствующее типу'}
|
|
|
|
hideWindow={hideWindow}
|
|
|
|
canSubmit={validated}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
>
|
2023-12-05 01:22:44 +03:00
|
|
|
<div className='flex justify-center items-center gap-6 w-full min-w-[22rem] my-3'>
|
2023-12-04 14:19:54 +03:00
|
|
|
<SelectSingle
|
|
|
|
placeholder='Выберите тип'
|
|
|
|
className='min-w-[14rem] self-center'
|
|
|
|
options={SelectorCstType}
|
|
|
|
value={{
|
|
|
|
value: cstData.cst_type,
|
|
|
|
label: labelCstType(cstData.cst_type)
|
|
|
|
}}
|
|
|
|
onChange={data => updateData({cst_type: data?.value ?? CstType.BASE})}
|
|
|
|
/>
|
|
|
|
<div>
|
2023-12-05 01:22:44 +03:00
|
|
|
<TextInput dense
|
|
|
|
label='Имя'
|
2023-12-04 14:19:54 +03:00
|
|
|
dimensions='w-[7rem]'
|
|
|
|
value={cstData.alias}
|
|
|
|
onChange={event => updateData({alias: event.target.value})}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Modal>);
|
2023-08-22 23:45:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default DlgRenameCst;
|