ConceptPortal-public/rsconcept/frontend/src/features/rsform/dialogs/DlgRenameCst.tsx

79 lines
2.5 KiB
TypeScript
Raw Normal View History

'use client';
2025-02-07 20:45:32 +03:00
import { zodResolver } from '@hookform/resolvers/zod';
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
import { TextInput } from '@/components/Input';
import { ModalForm } from '@/components/Modal';
import { HelpTopic } from '@/features/help/models/helpTopic';
import { useDialogsStore } from '@/stores/dialogs';
import { CstRenameSchema, ICstRenameDTO } from '../backend/api';
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
export interface DlgRenameCstProps {
schema: IRSForm;
2025-02-07 20:45:32 +03:00
target: IConstituenta;
2023-08-22 23:45:59 +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>({
resolver: zodResolver(CstRenameSchema),
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' });
2023-08-22 23:45:59 +03:00
2025-02-07 20:45:32 +03:00
// TODO: validate in ZOD
const validated = 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) {
cstRename({ itemID: schema.id, data: data });
}
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 (
<ModalForm
2023-12-28 14:04:44 +03:00
header='Переименование конституенты'
submitText='Переименовать'
2024-08-23 22:53:53 +03:00
submitInvalidTooltip='Введите незанятое имя, соответствующее типу'
2023-12-28 14:04:44 +03:00
canSubmit={validated}
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
>
<SelectCstType
id='dlg_cst_type'
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
/>
</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;