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

122 lines
3.8 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
import { type IOssLayout, type IUpdateOperationDTO, OperationType, schemaUpdateOperation } from '../../backend/types';
import { useUpdateOperation } from '../../backend/use-update-operation';
2025-02-20 20:22:05 +03:00
import { type IOperation, type IOperationSchema } from '../../models/oss';
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 { TabSynthesis } from './tab-synthesis';
2024-07-29 16:55:48 +03:00
export interface DlgEditOperationProps {
2024-07-29 16:55:48 +03:00
oss: IOperationSchema;
target: IOperation;
2025-04-06 15:47:40 +03:00
layout: IOssLayout;
2024-07-29 16:55:48 +03:00
}
2025-03-14 20:43:30 +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:55:48 +03:00
2025-02-19 23:29:45 +03:00
export function DlgEditOperation() {
2025-04-06 15:47:40 +03:00
const { oss, target, layout } = 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
},
arguments: target.arguments,
substitutions: target.substitutions.map(sub => ({
original: sub.original,
substitution: sub.substitution
})),
2025-04-06 15:47:40 +03:00
layout: 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-04-21 20:35:40 +03:00
return updateOperation({ itemID: 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.SUBSTITUTION}
2024-07-29 16:55:48 +03:00
>
2025-03-14 20:43:30 +03:00
<Tabs
2025-04-12 21:47:46 +03:00
selectedTabClassName='cc-selected'
2025-03-14 20:43:30 +03:00
className='grid'
selectedIndex={activeTab}
onSelect={index => setActiveTab(index as TabID)}
>
2025-04-17 14:37:47 +03:00
<TabList className='mb-3 mx-auto w-fit flex border divide-x rounded-none bg-secondary'>
2025-04-21 20:35:40 +03:00
<TabLabel
title='Текстовые поля' //
label='Карточка'
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
{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:55:48 +03:00
</Tabs>
2025-02-06 20:27:56 +03:00
</ModalForm>
2024-07-29 16:55:48 +03:00
);
}