Portal/rsconcept/frontend/src/features/oss/dialogs/DlgCreateOperation/DlgCreateOperation.tsx

133 lines
4.2 KiB
TypeScript
Raw Normal View History

2024-07-21 15:17:36 +03:00
'use client';
2025-02-11 12:34:28 +03:00
import { useState } from 'react';
import { FormProvider, useForm, useWatch } from 'react-hook-form';
2025-02-12 21:36:03 +03:00
import { zodResolver } from '@hookform/resolvers/zod';
import clsx from 'clsx';
import { HelpTopic } from '@/features/help';
2024-07-21 15:17:36 +03:00
import { ModalForm } from '@/components/Modal';
import { TabLabel, TabList, TabPanel, Tabs } from '@/components/Tabs';
import { useDialogsStore } from '@/stores/dialogs';
2024-07-21 15:17:36 +03:00
2025-02-18 19:39:54 +03:00
import { IOperationCreateDTO, IOperationPosition, OperationType, schemaOperationCreate } from '../../backend/types';
2025-02-11 12:34:28 +03:00
import { useOperationCreate } from '../../backend/useOperationCreate';
2025-02-11 20:56:11 +03:00
import { describeOperationType, labelOperationType } from '../../labels';
2025-02-18 19:39:54 +03:00
import { IOperationSchema } from '../../models/oss';
2025-02-11 12:34:28 +03:00
import { calculateInsertPosition } from '../../models/ossAPI';
2025-02-12 21:36:03 +03:00
2024-07-21 15:17:36 +03:00
import TabInputOperation from './TabInputOperation';
import TabSynthesisOperation from './TabSynthesisOperation';
export interface DlgCreateOperationProps {
2024-07-21 15:17:36 +03:00
oss: IOperationSchema;
2025-02-11 12:34:28 +03:00
positions: IOperationPosition[];
2025-02-17 15:11:32 +03:00
initialInputs: number[];
2025-02-11 12:34:28 +03:00
defaultX: number;
defaultY: number;
2025-02-17 15:11:32 +03:00
onCreate?: (newID: number) => void;
2024-07-21 15:17:36 +03:00
}
export enum TabID {
INPUT = 0,
SYNTHESIS = 1
}
function DlgCreateOperation() {
2025-02-11 12:34:28 +03:00
const { operationCreate } = useOperationCreate();
2025-01-23 19:41:31 +03:00
2025-02-11 12:34:28 +03:00
const { oss, positions, initialInputs, onCreate, defaultX, defaultY } = useDialogsStore(
state => state.props as DlgCreateOperationProps
);
2024-07-21 15:17:36 +03:00
2025-02-11 12:34:28 +03:00
const methods = useForm<IOperationCreateDTO>({
resolver: zodResolver(schemaOperationCreate),
defaultValues: {
2024-07-21 15:17:36 +03:00
item_data: {
2025-02-11 12:34:28 +03:00
operation_type: initialInputs.length === 0 ? OperationType.INPUT : OperationType.SYNTHESIS,
result: null,
position_x: defaultX,
position_y: defaultY,
alias: '',
title: '',
comment: ''
2024-07-21 15:17:36 +03:00
},
2025-02-11 12:34:28 +03:00
arguments: initialInputs,
create_schema: false,
positions: positions
},
mode: 'onChange'
});
const alias = useWatch({ control: methods.control, name: 'item_data.alias' });
const [activeTab, setActiveTab] = useState(initialInputs.length === 0 ? TabID.INPUT : TabID.SYNTHESIS);
const isValid = !!alias && !oss.items.some(operation => operation.alias === alias);
function onSubmit(data: IOperationCreateDTO) {
const target = calculateInsertPosition(oss, data.item_data.operation_type, data.arguments, positions, {
x: defaultX,
y: defaultY
2025-01-23 19:41:31 +03:00
});
2025-02-11 12:34:28 +03:00
data.item_data.position_x = target.x;
data.item_data.position_y = target.y;
2025-02-11 20:15:34 +03:00
void operationCreate({ itemID: oss.id, data: data }).then(response => onCreate?.(response.new_operation.id));
2025-02-11 12:34:28 +03:00
}
2024-07-21 15:17:36 +03:00
function handleSelectTab(newTab: TabID, last: TabID) {
if (last === newTab) {
return;
}
if (newTab === TabID.INPUT) {
2025-02-11 12:34:28 +03:00
methods.setValue('item_data.operation_type', OperationType.INPUT);
methods.setValue('item_data.result', null);
methods.setValue('arguments', []);
} else {
2025-02-11 12:34:28 +03:00
methods.setValue('item_data.operation_type', OperationType.SYNTHESIS);
methods.setValue('arguments', initialInputs);
}
setActiveTab(newTab);
}
2024-07-21 15:17:36 +03:00
return (
2025-02-06 20:27:56 +03:00
<ModalForm
2024-07-21 15:17:36 +03:00
header='Создание операции'
submitText='Создать'
canSubmit={isValid}
2025-02-11 12:34:28 +03:00
onSubmit={event => void methods.handleSubmit(onSubmit)(event)}
className='w-[40rem] px-6 h-[32rem]'
2024-10-29 12:05:23 +03:00
helpTopic={HelpTopic.CC_OSS}
2024-07-21 15:17:36 +03:00
>
<Tabs
selectedTabClassName='clr-selected'
className='flex flex-col pt-2'
2024-07-21 15:17:36 +03:00
selectedIndex={activeTab}
onSelect={handleSelectTab}
2024-07-21 15:17:36 +03:00
>
2024-12-18 14:54:45 +03:00
<TabList
className={clsx('self-center absolute top-[2.4rem]', 'flex', 'border divide-x rounded-none', 'bg-prim-200')}
>
2024-07-21 15:17:36 +03:00
<TabLabel
title={describeOperationType(OperationType.INPUT)}
label={labelOperationType(OperationType.INPUT)}
/>
<TabLabel
title={describeOperationType(OperationType.SYNTHESIS)}
label={labelOperationType(OperationType.SYNTHESIS)}
/>
</TabList>
2025-02-11 12:34:28 +03:00
<FormProvider {...methods}>
<TabPanel>
2025-02-12 00:14:18 +03:00
<TabInputOperation />
2025-02-11 12:34:28 +03:00
</TabPanel>
<TabPanel>
2025-02-12 00:14:18 +03:00
<TabSynthesisOperation />
2025-02-11 12:34:28 +03:00
</TabPanel>
</FormProvider>
2024-07-21 15:17:36 +03:00
</Tabs>
2025-02-06 20:27:56 +03:00
</ModalForm>
2024-07-21 15:17:36 +03:00
);
}
export default DlgCreateOperation;