2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2024-12-12 21:36:46 +03:00
|
|
|
import { useEffect, useState } from 'react';
|
2023-08-22 23:45:59 +03:00
|
|
|
|
2025-01-27 15:03:48 +03:00
|
|
|
import { ICstRenameDTO } from '@/backend/rsform/api';
|
2025-01-16 16:31:32 +03:00
|
|
|
import Modal from '@/components/ui/Modal';
|
2024-01-04 19:38:12 +03:00
|
|
|
import SelectSingle from '@/components/ui/SelectSingle';
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
2023-12-13 14:32:57 +03:00
|
|
|
import usePartialUpdate from '@/hooks/usePartialUpdate';
|
2024-04-03 19:31:26 +03:00
|
|
|
import { HelpTopic } from '@/models/miscellaneous';
|
2025-01-27 15:03:48 +03:00
|
|
|
import { CstType, IRSForm } from '@/models/rsform';
|
2024-01-04 14:35:46 +03:00
|
|
|
import { generateAlias, validateNewAlias } from '@/models/rsformAPI';
|
2025-01-16 16:31:32 +03:00
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
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
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
export interface DlgRenameCstProps {
|
|
|
|
schema: IRSForm;
|
2025-01-27 15:03:48 +03:00
|
|
|
initial: ICstRenameDTO;
|
2024-09-05 21:25:09 +03:00
|
|
|
allowChangeType: boolean;
|
2025-01-27 15:03:48 +03:00
|
|
|
onRename: (data: ICstRenameDTO) => void;
|
2023-08-22 23:45:59 +03:00
|
|
|
}
|
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
function DlgRenameCst() {
|
|
|
|
const { schema, initial, allowChangeType, onRename } = useDialogsStore(state => state.props as DlgRenameCstProps);
|
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
|
|
|
|
2024-12-12 21:36:46 +03:00
|
|
|
useEffect(() => {
|
2025-01-28 19:47:24 +03:00
|
|
|
if (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
|
|
|
|
2024-12-12 21:36:46 +03:00
|
|
|
useEffect(() => {
|
2025-01-28 19:47:24 +03:00
|
|
|
setValidated(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
|
|
|
|
2025-02-06 14:10:18 +03:00
|
|
|
function handleSubmit() {
|
|
|
|
onRename(cstData);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-22 23:45:59 +03:00
|
|
|
return (
|
2023-12-28 14:04:44 +03:00
|
|
|
<Modal
|
|
|
|
header='Переименование конституенты'
|
|
|
|
submitText='Переименовать'
|
2024-08-23 22:53:53 +03:00
|
|
|
submitInvalidTooltip='Введите незанятое имя, соответствующее типу'
|
2023-12-28 14:04:44 +03:00
|
|
|
canSubmit={validated}
|
2025-02-06 14:10:18 +03:00
|
|
|
onSubmit={handleSubmit}
|
2024-09-21 20:04:07 +03:00
|
|
|
className={clsx('w-[30rem]', 'py-6 pr-3 pl-6 flex gap-3 justify-center items-center ')}
|
2024-10-29 12:06:43 +03:00
|
|
|
helpTopic={HelpTopic.CC_CONSTITUENTA}
|
2023-12-28 14:04:44 +03:00
|
|
|
>
|
|
|
|
<SelectSingle
|
2024-03-18 16:21:39 +03:00
|
|
|
id='dlg_cst_type'
|
2023-12-28 14:04:44 +03:00
|
|
|
placeholder='Выберите тип'
|
2024-09-21 20:04:07 +03:00
|
|
|
className='min-w-[16rem]'
|
2024-09-05 21:25:09 +03:00
|
|
|
isDisabled={!allowChangeType}
|
2023-12-28 14:04:44 +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:41:33 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
<TextInput
|
2024-03-18 16:21:39 +03:00
|
|
|
id='dlg_cst_alias'
|
2023-12-28 14:04:44 +03:00
|
|
|
dense
|
|
|
|
label='Имя'
|
2024-08-30 11:03:45 +03:00
|
|
|
className='w-[7rem]'
|
2023-12-28 14:04:44 +03:00
|
|
|
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;
|