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

162 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-07-21 15:17:36 +03:00
'use client';
import clsx from 'clsx';
import { useEffect, useState } from 'react';
2024-07-21 15:17:36 +03:00
import { ModalForm } from '@/components/Modal';
import { TabLabel, TabList, TabPanel, Tabs } from '@/components/Tabs';
import { HelpTopic } from '@/features/help/models/helpTopic';
import { useLibrary } from '@/features/library/backend/useLibrary';
import { LibraryItemID } from '@/features/library/models/library';
import { useDialogsStore } from '@/stores/dialogs';
2024-07-21 15:17:36 +03:00
import { describeOperationType, labelOperationType } from '@/utils/labels';
import { IOperationCreateDTO } from '../../backend/api';
import { IOperationSchema, OperationID, OperationType } from '../../models/oss';
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-01-23 19:41:31 +03:00
onCreate: (data: IOperationCreateDTO) => void;
initialInputs: OperationID[];
2024-07-21 15:17:36 +03:00
}
export enum TabID {
INPUT = 0,
SYNTHESIS = 1
}
function DlgCreateOperation() {
2025-01-23 19:41:31 +03:00
const { items: libraryItems } = useLibrary();
const { oss, onCreate, initialInputs } = useDialogsStore(state => state.props as DlgCreateOperationProps);
const [activeTab, setActiveTab] = useState(initialInputs.length > 0 ? TabID.SYNTHESIS : TabID.INPUT);
2024-07-21 15:17:36 +03:00
const [alias, setAlias] = useState('');
const [title, setTitle] = useState('');
const [comment, setComment] = useState('');
const [inputs, setInputs] = useState<OperationID[]>(initialInputs);
2024-07-21 15:17:36 +03:00
const [attachedID, setAttachedID] = useState<LibraryItemID | undefined>(undefined);
2024-07-26 17:30:37 +03:00
const [createSchema, setCreateSchema] = useState(false);
2024-07-21 15:17:36 +03:00
const isValid = (() => {
if (alias === '') {
return false;
}
2024-09-16 19:38:24 +03:00
if (activeTab === TabID.SYNTHESIS && inputs.length === 0) {
return false;
}
if (activeTab === TabID.INPUT && !attachedID) {
if (oss.items.some(operation => operation.alias === alias)) {
return false;
}
}
return true;
})();
2024-07-21 15:17:36 +03:00
useEffect(() => {
2024-07-21 15:17:36 +03:00
if (attachedID) {
2025-01-23 19:41:31 +03:00
const schema = libraryItems.find(value => value.id === attachedID);
2024-07-21 15:17:36 +03:00
if (schema) {
setAlias(schema.alias);
setTitle(schema.title);
setComment(schema.comment);
}
}
2025-01-23 19:41:31 +03:00
}, [attachedID, libraryItems]);
2024-07-21 15:17:36 +03:00
const handleSubmit = () => {
2025-01-23 19:41:31 +03:00
onCreate({
2024-07-21 15:17:36 +03:00
item_data: {
2024-07-29 16:55:48 +03:00
position_x: 0,
position_y: 0,
2024-07-21 15:17:36 +03:00
alias: alias,
title: title,
comment: comment,
operation_type: activeTab === TabID.INPUT ? OperationType.INPUT : OperationType.SYNTHESIS,
result: activeTab === TabID.INPUT ? attachedID ?? null : null
},
2024-07-29 16:55:48 +03:00
positions: [],
2024-07-26 17:30:37 +03:00
arguments: activeTab === TabID.INPUT ? undefined : inputs.length > 0 ? inputs : undefined,
create_schema: createSchema
2025-01-23 19:41:31 +03:00
});
2025-02-06 14:09:20 +03:00
return true;
2024-07-21 15:17:36 +03:00
};
function handleSelectTab(newTab: TabID, last: TabID) {
if (last === newTab) {
return;
}
if (newTab === TabID.INPUT) {
setAttachedID(undefined);
} else {
setInputs(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}
onSubmit={handleSubmit}
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>
<TabPanel>
<TabInputOperation
oss={oss}
alias={alias}
onChangeAlias={setAlias}
comment={comment}
onChangeComment={setComment}
title={title}
onChangeTitle={setTitle}
attachedID={attachedID}
onChangeAttachedID={setAttachedID}
createSchema={createSchema}
onChangeCreateSchema={setCreateSchema}
/>
</TabPanel>
<TabPanel>
<TabSynthesisOperation
oss={oss}
alias={alias}
onChangeAlias={setAlias}
comment={comment}
onChangeComment={setComment}
title={title}
onChangeTitle={setTitle}
inputs={inputs}
setInputs={setInputs}
/>
</TabPanel>
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;