Portal/rsconcept/frontend/src/features/oss/dialogs/dlg-edit-operation/dlg-edit-operation.tsx

121 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-07-29 16:55:48 +03:00
'use client';
2025-02-12 00:14:18 +03:00
import { Suspense, useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
2025-02-12 21:36:03 +03:00
import { zodResolver } from '@hookform/resolvers/zod';
import { HelpTopic } from '@/features/help';
2024-07-29 16:55:48 +03:00
2025-03-12 12:04:23 +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:55:48 +03:00
2025-04-28 13:57:39 +03:00
import { type IUpdateOperationDTO, OperationType, schemaUpdateOperation } from '../../backend/types';
import { useUpdateOperation } from '../../backend/use-update-operation';
2025-08-04 15:59:16 +03:00
import { type IOperationInput, type IOperationSynthesis } from '../../models/oss';
2025-04-28 13:57:39 +03:00
import { type LayoutManager } from '../../models/oss-layout-api';
2025-02-12 21:36:03 +03:00
2025-03-12 11:54:32 +03:00
import { TabArguments } from './tab-arguments';
import { TabOperation } from './tab-operation';
import { TabSubstitutions } from './tab-substitutions';
2024-07-29 16:55:48 +03:00
export interface DlgEditOperationProps {
2025-04-28 13:57:39 +03:00
manager: LayoutManager;
2025-08-04 15:59:16 +03:00
target: IOperationInput | IOperationSynthesis;
2024-07-29 16:55:48 +03:00
}
2025-03-14 20:43:30 +03:00
export const TabID = {
CARD: 0,
ARGUMENTS: 1,
SUBSTITUTIONS: 2
2025-03-14 20:43:30 +03:00
} as const;
export type TabID = (typeof TabID)[keyof typeof TabID];
2024-07-29 16:55:48 +03:00
2025-02-19 23:29:45 +03:00
export function DlgEditOperation() {
2025-04-28 13:57:39 +03:00
const { manager, target } = useDialogsStore(state => state.props as DlgEditOperationProps);
2025-04-21 20:35:40 +03:00
const { updateOperation } = useUpdateOperation();
const methods = useForm<IUpdateOperationDTO>({
resolver: zodResolver(schemaUpdateOperation),
2025-02-12 00:14:18 +03:00
defaultValues: {
target: target.id,
2025-02-12 00:14:18 +03:00
item_data: {
alias: target.alias,
2025-02-22 17:25:40 +03:00
title: target.title,
2025-04-20 18:06:18 +03:00
description: target.description,
parent: target.parent
2025-02-12 00:14:18 +03:00
},
2025-08-04 15:59:16 +03:00
arguments: target.operation_type === OperationType.SYNTHESIS ? target.arguments : [],
substitutions:
target.operation_type === OperationType.SYNTHESIS
? target.substitutions.map(sub => ({
original: sub.original,
substitution: sub.substitution
}))
: [],
2025-04-28 13:57:39 +03:00
layout: manager.layout
},
mode: 'onChange'
2025-02-12 00:14:18 +03:00
});
2025-03-14 20:43:30 +03:00
const [activeTab, setActiveTab] = useState<TabID>(TabID.CARD);
2024-08-26 17:24:46 +03:00
function onSubmit(data: IUpdateOperationDTO) {
2025-05-14 10:56:40 +03:00
if (data.item_data.parent !== target.parent) {
manager.onChangeParent(target.nodeID, data.item_data.parent === null ? null : `b${data.item_data.parent}`);
data.layout = manager.layout;
2025-05-14 10:56:40 +03:00
}
2025-04-28 13:57:39 +03:00
return updateOperation({ itemID: manager.oss.id, data });
}
2024-07-29 16:55:48 +03:00
return (
2025-02-06 20:27:56 +03:00
<ModalForm
2024-07-29 16:55:48 +03:00
header='Редактирование операции'
submitText='Сохранить'
canSubmit={methods.formState.isValid}
2025-02-12 00:14:18 +03:00
onSubmit={event => void methods.handleSubmit(onSubmit)(event)}
2025-03-09 21:57:21 +03:00
className='w-160 px-6 h-128'
2024-10-29 12:05:23 +03:00
helpTopic={HelpTopic.UI_SUBSTITUTIONS}
hideHelpWhen={() => activeTab !== TabID.SUBSTITUTIONS}
2024-07-29 16:55:48 +03:00
>
2025-06-18 16:14:18 +03:00
<Tabs className='grid' selectedIndex={activeTab} onSelect={index => setActiveTab(index as TabID)}>
<TabList className='mb-3 mx-auto w-fit flex border divide-x rounded-none'>
2025-04-21 20:35:40 +03:00
<TabLabel
title='Текстовые поля' //
2025-07-02 12:37:47 +03:00
label='Паспорт'
2025-04-21 20:35:40 +03:00
className='w-32'
/>
<TabLabel
title='Выбор аргументов операции'
label='Аргументы'
className='w-32'
disabled={target.operation_type !== OperationType.SYNTHESIS}
/>
<TabLabel
titleHtml='Таблица отождествлений'
label='Отождествления'
className='w-32'
disabled={target.operation_type !== OperationType.SYNTHESIS}
/>
2024-07-29 16:55:48 +03:00
</TabList>
2025-02-12 00:14:18 +03:00
<FormProvider {...methods}>
<TabPanel>
2025-02-12 00:14:18 +03:00
<TabOperation />
</TabPanel>
2025-02-12 00:14:18 +03:00
2025-07-28 21:38:00 +03:00
<TabPanel>{target.operation_type === OperationType.SYNTHESIS ? <TabArguments /> : null}</TabPanel>
<TabPanel>
{target.operation_type === OperationType.SYNTHESIS ? (
2025-02-12 00:14:18 +03:00
<Suspense fallback={<Loader />}>
<TabSubstitutions />
2025-02-12 00:14:18 +03:00
</Suspense>
2025-07-28 21:38:00 +03:00
) : null}
</TabPanel>
2025-02-12 00:14:18 +03:00
</FormProvider>
2024-07-29 16:55:48 +03:00
</Tabs>
2025-02-06 20:27:56 +03:00
</ModalForm>
2024-07-29 16:55:48 +03:00
);
}