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

111 lines
3.7 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-03-12 12:04:50 +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
2025-04-06 15:49:43 +03:00
import { type IOperationUpdateDTO, type IOssLayout, OperationType, schemaOperationUpdate } from '../../backend/types';
2025-03-12 11:55:43 +03:00
import { useOperationUpdate } from '../../backend/use-operation-update';
import { type IOperation, type IOperationSchema } from '../../models/oss';
2025-02-12 21:36:25 +03:00
2025-03-12 11:55:43 +03:00
import { TabArguments } from './tab-arguments';
import { TabOperation } from './tab-operation';
import { TabSynthesis } from './tab-synthesis';
2024-07-29 16:56:24 +03:00
export interface DlgEditOperationProps {
2024-07-29 16:56:24 +03:00
oss: IOperationSchema;
target: IOperation;
2025-04-06 15:49:43 +03:00
layout: IOssLayout;
2024-07-29 16:56:24 +03:00
}
2025-03-14 20:44:23 +03:00
export const TabID = {
CARD: 0,
ARGUMENTS: 1,
SUBSTITUTION: 2
} as const;
export type TabID = (typeof TabID)[keyof typeof TabID];
2024-07-29 16:56:24 +03:00
2025-02-19 23:30:35 +03:00
export function DlgEditOperation() {
2025-04-06 15:49:43 +03:00
const { oss, target, layout } = useDialogsStore(state => state.props as DlgEditOperationProps);
2025-02-12 00:14:38 +03:00
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,
description: target.description
2025-02-12 00:14:38 +03:00
},
arguments: target.arguments,
substitutions: target.substitutions.map(sub => ({
original: sub.original,
substitution: sub.substitution
})),
2025-04-06 15:49:43 +03:00
layout: layout
},
mode: 'onChange'
2025-02-12 00:14:38 +03:00
});
2025-03-14 20:44:23 +03:00
const [activeTab, setActiveTab] = useState<TabID>(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-14 20:44:23 +03:00
<Tabs
selectedTabClassName='cc-selected'
2025-03-14 20:44:23 +03:00
className='grid'
selectedIndex={activeTab}
onSelect={index => setActiveTab(index as TabID)}
>
2025-03-11 11:35:51 +03:00
<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
);
}