2024-06-07 20:17:03 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
2024-12-12 20:57:45 +03:00
|
|
|
import { useEffect, useState } from 'react';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
import Modal, { ModalProps } from '@/components/ui/Modal';
|
|
|
|
import SelectSingle from '@/components/ui/SelectSingle';
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
|
|
|
import { useRSForm } from '@/context/RSFormContext';
|
|
|
|
import usePartialUpdate from '@/hooks/usePartialUpdate';
|
|
|
|
import { HelpTopic } from '@/models/miscellaneous';
|
|
|
|
import { CstType, ICstRenameData } from '@/models/rsform';
|
|
|
|
import { generateAlias, validateNewAlias } from '@/models/rsformAPI';
|
|
|
|
import { labelCstType } from '@/utils/labels';
|
|
|
|
import { SelectorCstType } from '@/utils/selectors';
|
|
|
|
|
|
|
|
interface DlgRenameCstProps extends Pick<ModalProps, 'hideWindow'> {
|
|
|
|
initial: ICstRenameData;
|
2024-09-05 21:24:57 +03:00
|
|
|
allowChangeType: boolean;
|
2024-06-07 20:17:03 +03:00
|
|
|
onRename: (data: ICstRenameData) => void;
|
|
|
|
}
|
|
|
|
|
2024-09-05 21:24:57 +03:00
|
|
|
function DlgRenameCst({ hideWindow, initial, allowChangeType, onRename }: DlgRenameCstProps) {
|
2024-06-07 20:17:03 +03:00
|
|
|
const { schema } = useRSForm();
|
|
|
|
const [validated, setValidated] = useState(false);
|
|
|
|
const [cstData, updateData] = usePartialUpdate(initial);
|
|
|
|
|
2024-12-12 20:57:45 +03:00
|
|
|
useEffect(() => {
|
2024-06-07 20:17:03 +03:00
|
|
|
if (schema && initial && cstData.cst_type !== initial.cst_type) {
|
|
|
|
updateData({ alias: generateAlias(cstData.cst_type, schema) });
|
|
|
|
}
|
|
|
|
}, [initial, cstData.cst_type, updateData, schema]);
|
|
|
|
|
2024-12-12 20:57:45 +03:00
|
|
|
useEffect(() => {
|
2024-06-07 20:17:03 +03:00
|
|
|
setValidated(
|
|
|
|
!!schema && cstData.alias !== initial.alias && validateNewAlias(cstData.alias, cstData.cst_type, schema)
|
|
|
|
);
|
|
|
|
}, [cstData.cst_type, cstData.alias, initial, schema]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
header='Переименование конституенты'
|
|
|
|
submitText='Переименовать'
|
2024-08-23 22:53:32 +03:00
|
|
|
submitInvalidTooltip='Введите незанятое имя, соответствующее типу'
|
2024-06-07 20:17:03 +03:00
|
|
|
hideWindow={hideWindow}
|
|
|
|
canSubmit={validated}
|
2024-08-15 23:23:10 +03:00
|
|
|
onSubmit={() => onRename(cstData)}
|
2024-09-21 20:03:49 +03:00
|
|
|
className={clsx('w-[30rem]', 'py-6 pr-3 pl-6 flex gap-3 justify-center items-center ')}
|
2024-10-29 12:05:23 +03:00
|
|
|
helpTopic={HelpTopic.CC_CONSTITUENTA}
|
2024-06-07 20:17:03 +03:00
|
|
|
>
|
|
|
|
<SelectSingle
|
|
|
|
id='dlg_cst_type'
|
|
|
|
placeholder='Выберите тип'
|
2024-09-21 20:03:49 +03:00
|
|
|
className='min-w-[16rem]'
|
2024-09-05 21:24:57 +03:00
|
|
|
isDisabled={!allowChangeType}
|
2024-06-07 20:17:03 +03:00
|
|
|
options={SelectorCstType}
|
|
|
|
value={{
|
|
|
|
value: cstData.cst_type,
|
|
|
|
label: labelCstType(cstData.cst_type)
|
|
|
|
}}
|
|
|
|
onChange={data => updateData({ cst_type: data?.value ?? CstType.BASE })}
|
|
|
|
/>
|
2024-06-09 20:40:41 +03:00
|
|
|
|
2024-06-07 20:17:03 +03:00
|
|
|
<TextInput
|
|
|
|
id='dlg_cst_alias'
|
|
|
|
dense
|
|
|
|
label='Имя'
|
2024-08-30 11:03:31 +03:00
|
|
|
className='w-[7rem]'
|
2024-06-07 20:17:03 +03:00
|
|
|
value={cstData.alias}
|
|
|
|
onChange={event => updateData({ alias: event.target.value })}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DlgRenameCst;
|