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

84 lines
2.7 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
2025-02-07 20:45:32 +03:00
import { CstRenameSchema, ICstRenameDTO } from '@/backend/rsform/api';
import { useCstRename } from '@/backend/rsform/useCstRename';
2025-02-07 10:54:47 +03:00
import { SelectSingle, TextInput } from '@/components/ui/Input';
import { ModalForm } from '@/components/ui/Modal';
2024-04-03 19:31:26 +03:00
import { HelpTopic } from '@/models/miscellaneous';
2025-02-07 20:45:32 +03:00
import { CstType, IConstituenta, IRSForm } from '@/models/rsform';
2024-01-04 14:35:46 +03:00
import { generateAlias, validateNewAlias } from '@/models/rsformAPI';
import { useDialogsStore } from '@/stores/dialogs';
import { labelCstType } from '@/utils/labels';
import { SelectorCstType } from '@/utils/selectors';
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
>
<SelectSingle
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]'
2025-02-07 20:45:32 +03:00
isDisabled={target.is_inherited}
2023-12-28 14:04:44 +03:00
options={SelectorCstType}
value={{
2025-02-07 20:45:32 +03:00
value: cst_type,
label: labelCstType(cst_type)
2023-12-28 14:04:44 +03:00
}}
2025-02-07 20:45:32 +03:00
onChange={data => handleChangeType(data?.value ?? CstType.BASE)}
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;