Portal/rsconcept/frontend/src/features/rsform/dialogs/DlgRenameCst.tsx

78 lines
2.4 KiB
TypeScript
Raw Normal View History

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