2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2023-08-23 01:36:17 +03:00
|
|
|
import { useLayoutEffect, useState } from 'react';
|
2023-08-22 23:45:59 +03:00
|
|
|
|
2024-01-04 19:38:12 +03:00
|
|
|
import Modal, { ModalProps } from '@/components/ui/Modal';
|
|
|
|
import SelectSingle from '@/components/ui/SelectSingle';
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
2023-12-13 14:32:57 +03:00
|
|
|
import { useRSForm } from '@/context/RSFormContext';
|
|
|
|
import usePartialUpdate from '@/hooks/usePartialUpdate';
|
|
|
|
import { CstType, ICstRenameData } from '@/models/rsform';
|
2024-01-04 14:35:46 +03:00
|
|
|
import { generateAlias, validateNewAlias } from '@/models/rsformAPI';
|
2023-12-13 14:32:57 +03:00
|
|
|
import { labelCstType } from '@/utils/labels';
|
|
|
|
import { SelectorCstType } from '@/utils/selectors';
|
2023-08-22 23:45:59 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
interface DlgRenameCstProps extends Pick<ModalProps, 'hideWindow'> {
|
|
|
|
initial: ICstRenameData;
|
|
|
|
onRename: (data: ICstRenameData) => void;
|
2023-08-22 23:45:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-12-28 14:04:44 +03:00
|
|
|
|
2023-11-01 20:14:50 +03:00
|
|
|
const handleSubmit = () => onRename(cstData);
|
2023-08-23 01:36:17 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
useLayoutEffect(() => {
|
2023-11-01 20:14:50 +03:00
|
|
|
if (schema && initial && cstData.cst_type !== initial.cst_type) {
|
2024-01-04 14:35:46 +03:00
|
|
|
updateData({ alias: generateAlias(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-12-28 14:04:44 +03:00
|
|
|
useLayoutEffect(() => {
|
2023-11-01 20:14:50 +03:00
|
|
|
setValidated(
|
2024-01-04 14:35:46 +03:00
|
|
|
!!schema && cstData.alias !== initial.alias && validateNewAlias(cstData.alias, cstData.cst_type, schema)
|
2023-11-01 20:14:50 +03:00
|
|
|
);
|
|
|
|
}, [cstData.cst_type, cstData.alias, initial, schema]);
|
2023-08-22 23:45:59 +03:00
|
|
|
|
|
|
|
return (
|
2023-12-28 14:04:44 +03:00
|
|
|
<Modal
|
|
|
|
header='Переименование конституенты'
|
|
|
|
submitText='Переименовать'
|
|
|
|
submitInvalidTooltip={'Введите незанятое имя, соответствующее типу'}
|
|
|
|
hideWindow={hideWindow}
|
|
|
|
canSubmit={validated}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
className={clsx('w-[30rem]', 'py-6 px-6 flex gap-6 justify-center items-center')}
|
|
|
|
>
|
|
|
|
<SelectSingle
|
2024-03-18 16:21:39 +03:00
|
|
|
id='dlg_cst_type'
|
2023-12-28 14:04:44 +03:00
|
|
|
placeholder='Выберите тип'
|
|
|
|
className='min-w-[16rem] self-center'
|
|
|
|
options={SelectorCstType}
|
|
|
|
value={{
|
|
|
|
value: cstData.cst_type,
|
|
|
|
label: labelCstType(cstData.cst_type)
|
|
|
|
}}
|
|
|
|
onChange={data => updateData({ cst_type: data?.value ?? CstType.BASE })}
|
|
|
|
/>
|
|
|
|
<TextInput
|
2024-03-18 16:21:39 +03:00
|
|
|
id='dlg_cst_alias'
|
2023-12-28 14:04:44 +03:00
|
|
|
dense
|
|
|
|
label='Имя'
|
|
|
|
className='w-[7rem]'
|
|
|
|
value={cstData.alias}
|
|
|
|
onChange={event => updateData({ alias: event.target.value })}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
);
|
2023-08-22 23:45:59 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default DlgRenameCst;
|