2025-04-22 13:59:14 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import { Controller, useForm } from 'react-hook-form';
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
|
|
|
|
|
import { TextArea, TextInput } from '@/components/input';
|
|
|
|
import { ModalForm } from '@/components/modal';
|
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
|
|
|
|
2025-04-28 13:59:38 +03:00
|
|
|
import { type IUpdateBlockDTO, schemaUpdateBlock } from '../backend/types';
|
2025-04-22 13:59:14 +03:00
|
|
|
import { useUpdateBlock } from '../backend/use-update-block';
|
|
|
|
import { SelectParent } from '../components/select-parent';
|
2025-04-28 13:59:38 +03:00
|
|
|
import { type IBlock } from '../models/oss';
|
|
|
|
import { type LayoutManager } from '../models/oss-layout-api';
|
2025-04-22 13:59:14 +03:00
|
|
|
|
|
|
|
export interface DlgEditBlockProps {
|
2025-04-28 13:59:38 +03:00
|
|
|
manager: LayoutManager;
|
2025-04-22 13:59:14 +03:00
|
|
|
target: IBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function DlgEditBlock() {
|
2025-04-28 13:59:38 +03:00
|
|
|
const { manager, target } = useDialogsStore(state => state.props as DlgEditBlockProps);
|
2025-04-22 13:59:14 +03:00
|
|
|
const { updateBlock } = useUpdateBlock();
|
|
|
|
|
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
register,
|
|
|
|
formState: { errors, isValid }
|
|
|
|
} = useForm<IUpdateBlockDTO>({
|
|
|
|
resolver: zodResolver(schemaUpdateBlock),
|
|
|
|
defaultValues: {
|
|
|
|
target: target.id,
|
|
|
|
item_data: {
|
|
|
|
title: target.title,
|
|
|
|
description: target.description,
|
|
|
|
parent: target.parent
|
|
|
|
},
|
2025-04-28 13:59:38 +03:00
|
|
|
layout: manager.layout
|
2025-04-22 13:59:14 +03:00
|
|
|
},
|
|
|
|
mode: 'onChange'
|
|
|
|
});
|
|
|
|
|
|
|
|
function onSubmit(data: IUpdateBlockDTO) {
|
2025-05-14 10:56:50 +03:00
|
|
|
if (data.item_data.parent !== target.parent) {
|
2025-06-12 19:44:49 +03:00
|
|
|
manager.onChangeParent(target.nodeID, data.item_data.parent === null ? null : `b${data.item_data.parent}`);
|
2025-06-05 15:26:35 +03:00
|
|
|
data.layout = manager.layout;
|
2025-05-14 10:56:50 +03:00
|
|
|
}
|
2025-04-28 13:59:38 +03:00
|
|
|
return updateBlock({ itemID: manager.oss.id, data });
|
2025-04-22 13:59:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ModalForm
|
|
|
|
header='Редактирование блока'
|
|
|
|
submitText='Сохранить'
|
|
|
|
canSubmit={isValid}
|
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
2025-04-24 13:16:50 +03:00
|
|
|
className='w-160 px-6 pb-2 h-fit cc-column'
|
2025-04-22 13:59:14 +03:00
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
id='operation_title' //
|
|
|
|
label='Название'
|
|
|
|
{...register('item_data.title')}
|
|
|
|
error={errors.item_data?.title}
|
|
|
|
/>
|
|
|
|
<Controller
|
|
|
|
name='item_data.parent'
|
|
|
|
control={control}
|
2025-07-17 19:17:23 +03:00
|
|
|
render={({ field }) => {
|
|
|
|
const descendantNodeIDs = manager.oss.hierarchy.expandAllOutputs([target.nodeID]);
|
|
|
|
descendantNodeIDs.push(target.nodeID);
|
|
|
|
return (
|
|
|
|
<SelectParent
|
|
|
|
items={manager.oss.blocks.filter(block => !descendantNodeIDs.includes(block.nodeID))}
|
|
|
|
value={field.value ? manager.oss.blockByID.get(field.value) ?? null : null}
|
|
|
|
placeholder='Родительский блок'
|
|
|
|
onChange={value => field.onChange(value ? value.id : null)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
2025-04-22 13:59:14 +03:00
|
|
|
/>
|
|
|
|
|
|
|
|
<TextArea
|
|
|
|
id='operation_comment' //
|
|
|
|
label='Описание'
|
|
|
|
noResize
|
|
|
|
rows={5}
|
|
|
|
{...register('item_data.description')}
|
|
|
|
/>
|
|
|
|
</ModalForm>
|
|
|
|
);
|
|
|
|
}
|