2024-03-01 18:19:33 +03:00
|
|
|
'use client';
|
|
|
|
|
2025-02-12 02:12:08 +03:00
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2024-03-01 18:19:33 +03:00
|
|
|
import clsx from 'clsx';
|
2025-02-12 02:12:08 +03:00
|
|
|
import { Controller, useForm } from 'react-hook-form';
|
2024-03-01 18:19:33 +03:00
|
|
|
|
2025-02-12 02:12:08 +03:00
|
|
|
import { ErrorField } from '@/components/Input';
|
2025-02-10 01:32:55 +03:00
|
|
|
import { ModalForm } from '@/components/Modal';
|
|
|
|
import { HelpTopic } from '@/features/help/models/helpTopic';
|
2025-01-16 16:31:32 +03:00
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
2024-03-01 18:19:33 +03:00
|
|
|
|
2025-02-12 02:12:08 +03:00
|
|
|
import { ICstSubstitutionsDTO, schemaCstSubstitutions } from '../backend/api';
|
|
|
|
import { useCstSubstitute } from '../backend/useCstSubstitute';
|
|
|
|
import PickSubstitutions from '../components/PickSubstitutions';
|
2025-02-10 01:32:55 +03:00
|
|
|
import { IRSForm } from '../models/rsform';
|
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
export interface DlgSubstituteCstProps {
|
2024-07-30 16:00:09 +03:00
|
|
|
schema: IRSForm;
|
2025-02-12 02:12:08 +03:00
|
|
|
onSubstitute: (data: ICstSubstitutionsDTO) => void;
|
2024-03-01 18:19:33 +03:00
|
|
|
}
|
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
function DlgSubstituteCst() {
|
|
|
|
const { onSubstitute, schema } = useDialogsStore(state => state.props as DlgSubstituteCstProps);
|
2025-02-12 02:12:08 +03:00
|
|
|
const { cstSubstitute } = useCstSubstitute();
|
2024-03-01 18:19:33 +03:00
|
|
|
|
2025-02-12 02:12:08 +03:00
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
formState: { errors, isValid }
|
|
|
|
} = useForm<ICstSubstitutionsDTO>({
|
|
|
|
resolver: zodResolver(schemaCstSubstitutions),
|
|
|
|
defaultValues: {
|
|
|
|
substitutions: []
|
|
|
|
},
|
|
|
|
mode: 'onChange'
|
|
|
|
});
|
|
|
|
|
|
|
|
function onSubmit(data: ICstSubstitutionsDTO) {
|
|
|
|
return cstSubstitute({ itemID: schema.id, data: data }).then(() => onSubstitute(data));
|
2024-03-01 18:19:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2025-02-06 20:28:23 +03:00
|
|
|
<ModalForm
|
2024-03-27 22:54:24 +03:00
|
|
|
header='Отождествление'
|
2024-03-01 18:19:33 +03:00
|
|
|
submitText='Отождествить'
|
2024-08-23 22:53:53 +03:00
|
|
|
submitInvalidTooltip='Выберите две различные конституенты'
|
2025-02-12 02:12:08 +03:00
|
|
|
canSubmit={isValid}
|
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
2024-03-27 22:54:24 +03:00
|
|
|
className={clsx('w-[40rem]', 'px-6 pb-3')}
|
2024-10-29 12:06:43 +03:00
|
|
|
helpTopic={HelpTopic.UI_SUBSTITUTIONS}
|
2024-03-01 18:19:33 +03:00
|
|
|
>
|
2025-02-12 02:12:08 +03:00
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name='substitutions'
|
|
|
|
render={({ field }) => (
|
|
|
|
<PickSubstitutions
|
|
|
|
allowSelfSubstitution
|
|
|
|
value={field.value}
|
|
|
|
onChange={field.onChange}
|
|
|
|
rows={6}
|
|
|
|
schemas={[schema]}
|
|
|
|
/>
|
|
|
|
)}
|
2024-03-01 18:19:33 +03:00
|
|
|
/>
|
2025-02-12 02:12:08 +03:00
|
|
|
<ErrorField className='mt-2' error={errors.substitutions} />
|
2025-02-06 20:28:23 +03:00
|
|
|
</ModalForm>
|
2024-03-01 18:19:33 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DlgSubstituteCst;
|