Portal/rsconcept/frontend/src/features/oss/dialogs/dlg-create-operation/tab-input-operation.tsx

117 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-07-26 00:33:22 +03:00
'use client';
2025-02-11 12:34:28 +03:00
import { Controller, useFormContext, useWatch } from 'react-hook-form';
2024-07-26 00:33:22 +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';
2025-02-12 21:36:03 +03:00
2025-03-12 12:04:23 +03:00
import { MiniButton } from '@/components/control';
import { IconReset } from '@/components/icons';
import { Checkbox, Label, TextArea, TextInput } from '@/components/input';
2025-02-12 00:14:18 +03:00
import { useDialogsStore } from '@/stores/dialogs';
2024-07-21 15:17:36 +03:00
2025-02-20 20:22:05 +03:00
import { type IOperationCreateDTO } from '../../backend/types';
2025-03-12 11:54:32 +03:00
import { sortItemsForOSS } from '../../models/oss-api';
2025-02-12 21:36:03 +03:00
2025-03-12 11:54:32 +03:00
import { type DlgCreateOperationProps } from './dlg-create-operation';
2025-02-11 12:34:28 +03:00
2025-02-19 23:29:45 +03:00
export function TabInputOperation() {
2025-02-12 00:14:18 +03:00
const { oss } = useDialogsStore(state => state.props as DlgCreateOperationProps);
2025-01-23 19:41:31 +03:00
const { items: libraryItems } = useLibrary();
const sortedItems = sortItemsForOSS(oss, libraryItems);
2025-02-11 12:34:28 +03:00
const {
register,
control,
setValue,
formState: { errors }
} = useFormContext<IOperationCreateDTO>();
const createSchema = useWatch({ control, name: 'create_schema' });
function baseFilter(item: ILibraryItem) {
return !oss.schemas.includes(item.id);
}
2024-07-26 00:33:22 +03:00
2025-02-11 12:34:28 +03:00
function handleChangeCreateSchema(value: boolean) {
if (value) {
setValue('item_data.result', null);
}
setValue('create_schema', value);
}
2025-02-12 15:12:59 +03:00
function handleSetInput(inputID: number) {
const schema = libraryItems.find(item => item.id === inputID);
2025-02-11 12:34:28 +03:00
if (!schema) {
return;
2024-07-26 17:30:37 +03:00
}
2025-02-12 15:12:59 +03:00
setValue('item_data.result', inputID);
2025-02-11 12:34:28 +03:00
setValue('create_schema', false);
setValue('item_data.alias', schema.alias);
setValue('item_data.title', schema.title);
setValue('item_data.description', schema.description, { shouldValidate: true });
2025-02-11 12:34:28 +03:00
}
2024-07-26 17:30:37 +03:00
2024-07-21 15:17:36 +03:00
return (
2024-12-12 13:17:24 +03:00
<div className='cc-fade-in cc-column'>
2024-07-21 15:17:36 +03:00
<TextInput
2025-02-11 12:34:28 +03:00
id='operation_title' //
2024-07-21 15:17:36 +03:00
label='Полное название'
2025-02-11 12:34:28 +03:00
{...register('item_data.title')}
error={errors.item_data?.title}
2024-07-21 15:17:36 +03:00
/>
<div className='flex gap-6'>
<TextInput
2025-02-11 12:34:28 +03:00
id='operation_alias' //
label='Сокращение'
2025-03-10 16:01:40 +03:00
className='w-64'
2025-02-11 12:34:28 +03:00
{...register('item_data.alias')}
error={errors.item_data?.alias}
/>
2024-07-21 15:17:36 +03:00
<TextArea
2025-02-11 12:34:28 +03:00
id='operation_comment' //
2024-07-21 15:17:36 +03:00
label='Описание'
noResize
rows={3}
{...register('item_data.description')}
2024-07-24 22:23:05 +03:00
/>
</div>
2024-07-26 17:30:37 +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-11 12:34:28 +03:00
onClick={() => setValue('item_data.result', null)}
2024-07-26 17:30:37 +03:00
/>
</div>
<Checkbox
2025-02-11 12:34:28 +03:00
value={createSchema} //
onChange={handleChangeCreateSchema}
2024-07-26 17:30:37 +03:00
label='Создать новую схему'
2024-07-21 15:17:36 +03:00
/>
</div>
2024-07-26 17:30:37 +03:00
{!createSchema ? (
2025-02-11 12:34:28 +03:00
<Controller
control={control}
name='item_data.result'
render={({ field }) => (
<PickSchema
items={sortedItems}
value={field.value}
itemType={LibraryItemType.RSFORM}
onChange={handleSetInput}
rows={8}
baseFilter={baseFilter}
/>
)}
2024-07-26 17:30:37 +03:00
/>
) : null}
2024-12-12 13:17:24 +03:00
</div>
2024-07-21 15:17:36 +03:00
);
}