Portal/rsconcept/frontend/src/features/oss/dialogs/dlg-change-input-schema.tsx

88 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-07-28 21:29:46 +03:00
'use client';
2025-02-12 21:36:03 +03:00
import { Controller, useForm } from 'react-hook-form';
2025-02-10 13:27:55 +03:00
import { zodResolver } from '@hookform/resolvers/zod';
2025-02-12 21:36:03 +03:00
2025-02-26 00:16:22 +03:00
import { type ILibraryItem, LibraryItemType } from '@/features/library';
2025-03-12 11:54:32 +03:00
import { useLibrary } from '@/features/library/backend/use-library';
2025-02-26 00:16:22 +03:00
import { PickSchema } from '@/features/library/components';
2024-07-28 21:29:46 +03:00
2025-03-12 12:04:23 +03:00
import { MiniButton } from '@/components/control';
import { IconReset } from '@/components/icons';
import { Label } from '@/components/input';
import { ModalForm } from '@/components/modal';
import { useDialogsStore } from '@/stores/dialogs';
2024-07-28 21:29:46 +03:00
2025-02-20 20:22:05 +03:00
import { type IInputUpdateDTO, type IOperationPosition, schemaInputUpdate } from '../backend/types';
2025-03-12 11:54:32 +03:00
import { useInputUpdate } from '../backend/use-input-update';
2025-02-20 20:22:05 +03:00
import { type IOperation, type IOperationSchema } from '../models/oss';
2025-03-12 11:54:32 +03:00
import { sortItemsForOSS } from '../models/oss-api';
export interface DlgChangeInputSchemaProps {
2024-07-28 21:29:46 +03:00
oss: IOperationSchema;
target: IOperation;
2025-02-10 13:27:55 +03:00
positions: IOperationPosition[];
2024-07-28 21:29:46 +03:00
}
2025-02-19 23:29:45 +03:00
export function DlgChangeInputSchema() {
2025-02-10 13:27:55 +03:00
const { oss, target, positions } = useDialogsStore(state => state.props as DlgChangeInputSchemaProps);
const { inputUpdate } = useInputUpdate();
const { setValue, handleSubmit, control } = useForm<IInputUpdateDTO>({
resolver: zodResolver(schemaInputUpdate),
2025-02-10 13:27:55 +03:00
defaultValues: {
target: target.id,
positions: positions,
input: target.result
}
});
2025-01-23 19:41:31 +03:00
const { items } = useLibrary();
const sortedItems = sortItemsForOSS(oss, items);
2024-07-28 21:29:46 +03:00
function baseFilter(item: ILibraryItem) {
2025-02-10 13:27:55 +03:00
return !oss.schemas.includes(item.id) || item.id === target.result;
}
2024-07-28 21:29:46 +03:00
2025-02-10 13:27:55 +03:00
function onSubmit(data: IInputUpdateDTO) {
2025-02-11 20:15:34 +03:00
return inputUpdate({ itemID: oss.id, data: data });
2025-02-06 14:09:20 +03:00
}
2024-07-28 21:29:46 +03:00
return (
2025-02-06 20:27:56 +03:00
<ModalForm
2024-07-28 21:29:46 +03:00
overflowVisible
header='Выбор концептуальной схемы'
submitText='Подтвердить выбор'
2025-02-10 13:27:55 +03:00
onSubmit={event => void handleSubmit(onSubmit)(event)}
2025-03-10 16:01:40 +03:00
className='w-140 pb-3 px-6 cc-column'
2024-07-28 21:29:46 +03:00
>
<div className='flex justify-between gap-3 items-center'>
<div className='flex gap-3'>
<Label text='Загружаемая концептуальная схема' />
<MiniButton
title='Сбросить выбор схемы'
noHover
noPadding
icon={<IconReset size='1.25rem' className='icon-primary' />}
2025-02-10 13:27:55 +03:00
onClick={() => setValue('input', null)}
2024-07-28 21:29:46 +03:00
/>
</div>
</div>
2025-02-10 13:27:55 +03:00
<Controller
name='input'
control={control}
render={({ field }) => (
<PickSchema
items={sortedItems}
itemType={LibraryItemType.RSFORM}
value={field.value}
onChange={field.onChange}
rows={14}
baseFilter={baseFilter}
/>
)}
2024-07-28 21:29:46 +03:00
/>
2025-02-06 20:27:56 +03:00
</ModalForm>
2024-07-28 21:29:46 +03:00
);
}