ConceptPortal-public/rsconcept/frontend/src/features/oss/dialogs/DlgEditOperation/DlgEditOperation.tsx

110 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-07-29 16:56:24 +03:00
'use client';
2025-02-12 00:14:38 +03:00
import { Suspense, useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
2025-02-12 21:36:25 +03:00
import { zodResolver } from '@hookform/resolvers/zod';
import { HelpTopic } from '@/features/help';
2024-07-29 16:56:24 +03:00
2025-02-12 00:14:38 +03:00
import { Loader } from '@/components/Loader';
import { ModalForm } from '@/components/Modal';
import { TabLabel, TabList, TabPanel, Tabs } from '@/components/Tabs';
import { useDialogsStore } from '@/stores/dialogs';
2024-07-29 16:56:24 +03:00
import {
type IOperationPosition,
type IOperationUpdateDTO,
OperationType,
schemaOperationUpdate
} from '../../backend/types';
2025-02-12 00:14:38 +03:00
import { useOperationUpdate } from '../../backend/useOperationUpdate';
import { type IOperation, type IOperationSchema } from '../../models/oss';
2025-02-12 21:36:25 +03:00
2025-02-19 23:30:35 +03:00
import { TabArguments } from './TabArguments';
import { TabOperation } from './TabOperation';
import { TabSynthesis } from './TabSynthesis';
2024-07-29 16:56:24 +03:00
export interface DlgEditOperationProps {
2024-07-29 16:56:24 +03:00
oss: IOperationSchema;
target: IOperation;
2025-02-12 00:14:38 +03:00
positions: IOperationPosition[];
2024-07-29 16:56:24 +03:00
}
export enum TabID {
CARD = 0,
ARGUMENTS = 1,
SUBSTITUTION = 2
}
2025-02-19 23:30:35 +03:00
export function DlgEditOperation() {
2025-02-12 00:14:38 +03:00
const { oss, target, positions } = useDialogsStore(state => state.props as DlgEditOperationProps);
const { operationUpdate } = useOperationUpdate();
2025-02-12 00:14:38 +03:00
const methods = useForm<IOperationUpdateDTO>({
resolver: zodResolver(schemaOperationUpdate),
defaultValues: {
target: target.id,
2025-02-12 00:14:38 +03:00
item_data: {
alias: target.alias,
2025-02-22 17:26:18 +03:00
title: target.title,
2025-02-12 00:14:38 +03:00
comment: target.comment
},
arguments: target.arguments,
substitutions: target.substitutions.map(sub => ({
original: sub.original,
substitution: sub.substitution
})),
positions: positions
},
mode: 'onChange'
2025-02-12 00:14:38 +03:00
});
const [activeTab, setActiveTab] = useState(TabID.CARD);
2024-08-26 17:25:07 +03:00
2025-02-12 00:14:38 +03:00
function onSubmit(data: IOperationUpdateDTO) {
return operationUpdate({ itemID: oss.id, data });
}
2024-07-29 16:56:24 +03:00
return (
<ModalForm
2024-07-29 16:56:24 +03:00
header='Редактирование операции'
submitText='Сохранить'
canSubmit={methods.formState.isValid}
2025-02-12 00:14:38 +03:00
onSubmit={event => void methods.handleSubmit(onSubmit)(event)}
2025-03-09 21:59:21 +03:00
className='w-160 px-6 h-128'
2024-10-29 12:06:43 +03:00
helpTopic={HelpTopic.UI_SUBSTITUTIONS}
hideHelpWhen={() => activeTab !== TabID.SUBSTITUTION}
2024-07-29 16:56:24 +03:00
>
2025-03-11 11:35:51 +03:00
<Tabs selectedTabClassName='clr-selected' className='grid' selectedIndex={activeTab} onSelect={setActiveTab}>
<TabList className='mb-3 mx-auto w-fit flex border divide-x rounded-none bg-prim-200'>
2025-03-09 21:59:21 +03:00
<TabLabel title='Текстовые поля' label='Карточка' className='w-32' />
2024-07-29 16:56:24 +03:00
{target.operation_type === OperationType.SYNTHESIS ? (
2025-03-09 21:59:21 +03:00
<TabLabel title='Выбор аргументов операции' label='Аргументы' className='w-32' />
2024-07-29 16:56:24 +03:00
) : null}
{target.operation_type === OperationType.SYNTHESIS ? (
2025-03-09 21:59:21 +03:00
<TabLabel titleHtml='Таблица отождествлений' label='Отождествления' className='w-32' />
2024-07-29 16:56:24 +03:00
) : null}
</TabList>
2025-02-12 00:14:38 +03:00
<FormProvider {...methods}>
<TabPanel>
2025-02-12 00:14:38 +03:00
<TabOperation />
</TabPanel>
2025-02-12 00:14:38 +03:00
{target.operation_type === OperationType.SYNTHESIS ? (
<TabPanel>
<TabArguments />
</TabPanel>
) : null}
{target.operation_type === OperationType.SYNTHESIS ? (
<TabPanel>
<Suspense fallback={<Loader />}>
<TabSynthesis />
</Suspense>
</TabPanel>
) : null}
</FormProvider>
2024-07-29 16:56:24 +03:00
</Tabs>
</ModalForm>
2024-07-29 16:56:24 +03:00
);
}