2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2025-02-07 20:45:32 +03:00
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2025-02-07 20:45:32 +03:00
|
|
|
import { useForm, useWatch } from 'react-hook-form';
|
2023-08-22 23:45:59 +03:00
|
|
|
|
2025-02-10 01:32:55 +03:00
|
|
|
import { TextInput } from '@/components/Input';
|
|
|
|
import { ModalForm } from '@/components/Modal';
|
2025-02-12 13:42:21 +03:00
|
|
|
import { HelpTopic } from '@/features/help';
|
2025-01-16 16:31:32 +03:00
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
2025-02-10 01:32:55 +03:00
|
|
|
|
2025-02-10 14:11:28 +03:00
|
|
|
import { ICstRenameDTO, schemaCstRename } from '../backend/api';
|
2025-02-10 01:32:55 +03:00
|
|
|
import { useCstRename } from '../backend/useCstRename';
|
|
|
|
import { SelectCstType } from '../components/SelectCstType';
|
|
|
|
import { CstType, IConstituenta, IRSForm } from '../models/rsform';
|
|
|
|
import { generateAlias, validateNewAlias } from '../models/rsformAPI';
|
2023-08-22 23:45:59 +03:00
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
export interface DlgRenameCstProps {
|
|
|
|
schema: IRSForm;
|
2025-02-07 20:45:32 +03:00
|
|
|
target: IConstituenta;
|
2023-08-22 23:45:59 +03:00
|
|
|
}
|
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
function DlgRenameCst() {
|
2025-02-07 20:45:32 +03:00
|
|
|
const { schema, target } = useDialogsStore(state => state.props as DlgRenameCstProps);
|
|
|
|
const { cstRename } = useCstRename();
|
2023-12-28 14:04:44 +03:00
|
|
|
|
2025-02-07 20:45:32 +03:00
|
|
|
const { register, setValue, handleSubmit, control } = useForm<ICstRenameDTO>({
|
2025-02-10 14:11:28 +03:00
|
|
|
resolver: zodResolver(schemaCstRename),
|
2025-02-07 20:45:32 +03:00
|
|
|
defaultValues: {
|
|
|
|
target: target.id,
|
|
|
|
alias: target.alias,
|
|
|
|
cst_type: target.cst_type
|
2023-08-22 23:45:59 +03:00
|
|
|
}
|
2025-02-07 20:45:32 +03:00
|
|
|
});
|
|
|
|
const alias = useWatch({ control, name: 'alias' });
|
|
|
|
const cst_type = useWatch({ control, name: 'cst_type' });
|
2025-02-10 13:29:28 +03:00
|
|
|
const isValid = alias !== target.alias && validateNewAlias(alias, cst_type, schema);
|
2023-08-22 23:45:59 +03:00
|
|
|
|
2025-02-07 20:45:32 +03:00
|
|
|
function onSubmit(data: ICstRenameDTO) {
|
2025-02-11 20:56:24 +03:00
|
|
|
return cstRename({ itemID: schema.id, data: data });
|
2025-02-07 20:45:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleChangeType(newType: CstType) {
|
|
|
|
setValue('alias', generateAlias(newType, schema));
|
|
|
|
setValue('cst_type', newType);
|
2025-02-06 14:10:18 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 23:45:59 +03:00
|
|
|
return (
|
2025-02-06 20:28:23 +03:00
|
|
|
<ModalForm
|
2023-12-28 14:04:44 +03:00
|
|
|
header='Переименование конституенты'
|
|
|
|
submitText='Переименовать'
|
2024-08-23 22:53:53 +03:00
|
|
|
submitInvalidTooltip='Введите незанятое имя, соответствующее типу'
|
2025-02-10 13:29:28 +03:00
|
|
|
canSubmit={isValid}
|
2025-02-07 20:45:32 +03:00
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
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
|
|
|
>
|
2025-02-10 01:32:55 +03:00
|
|
|
<SelectCstType
|
2024-03-18 16:21:39 +03:00
|
|
|
id='dlg_cst_type'
|
2025-02-10 01:32:55 +03:00
|
|
|
className='w-[16rem]'
|
|
|
|
value={cst_type}
|
|
|
|
onChange={handleChangeType}
|
|
|
|
disabled={target.is_inherited}
|
2023-12-28 14:04:44 +03:00
|
|
|
/>
|
|
|
|
<TextInput
|
2025-02-07 20:45:32 +03:00
|
|
|
id='dlg_cst_alias' //
|
|
|
|
{...register('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
|
|
|
/>
|
2025-02-06 20:28:23 +03:00
|
|
|
</ModalForm>
|
2023-12-28 14:04:44 +03:00
|
|
|
);
|
2023-08-22 23:45:59 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default DlgRenameCst;
|