Portal/rsconcept/frontend/src/features/library/dialogs/dlg-clone-library-item.tsx

141 lines
4.6 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
2025-02-12 21:36:03 +03:00
import { Controller, useForm } from 'react-hook-form';
2025-02-08 19:07:43 +03:00
import { zodResolver } from '@hookform/resolvers/zod';
2024-06-07 20:17:03 +03:00
import { urls, useConceptNavigation } from '@/app';
2025-02-12 21:36:03 +03:00
2025-03-12 12:04:23 +03:00
import { MiniButton } from '@/components/control';
import { Checkbox, Label, TextArea, TextInput } from '@/components/input';
import { ModalForm } from '@/components/modal';
import { useDialogsStore } from '@/stores/dialogs';
2024-06-07 20:17:03 +03:00
2025-02-20 20:22:05 +03:00
import { AccessPolicy, type ICloneLibraryItemDTO, type ILibraryItem, schemaCloneLibraryItem } from '../backend/types';
2025-03-12 11:54:32 +03:00
import { useCloneItem } from '../backend/use-clone-item';
import { IconItemVisibility } from '../components/icon-item-visibility';
import { PickLocation } from '../components/pick-location';
import { SelectAccessPolicy } from '../components/select-access-policy';
import { cloneTitle } from '../models/library-api';
export interface DlgCloneLibraryItemProps {
2024-06-07 20:17:03 +03:00
base: ILibraryItem;
initialLocation: string;
2025-02-12 15:12:59 +03:00
selected: number[];
2024-06-07 20:17:03 +03:00
totalCount: number;
}
2025-02-19 23:29:45 +03:00
export function DlgCloneLibraryItem() {
const { base, initialLocation, selected, totalCount } = useDialogsStore(
state => state.props as DlgCloneLibraryItemProps
);
2024-06-07 20:17:03 +03:00
const router = useConceptNavigation();
2025-01-23 19:41:31 +03:00
const { cloneItem } = useCloneItem();
2024-06-07 20:17:03 +03:00
2025-02-08 19:07:43 +03:00
const {
register,
handleSubmit,
control,
formState: { errors, isValid }
} = useForm<ICloneLibraryItemDTO>({
resolver: zodResolver(schemaCloneLibraryItem),
2025-02-08 19:07:43 +03:00
defaultValues: {
2025-01-23 19:41:31 +03:00
id: base.id,
2024-06-07 20:17:03 +03:00
item_type: base.item_type,
2025-02-08 19:07:43 +03:00
title: cloneTitle(base),
alias: base.alias,
description: base.description,
2025-02-08 19:07:43 +03:00
visible: true,
2024-06-07 20:17:03 +03:00
read_only: false,
2025-02-08 19:07:43 +03:00
access_policy: AccessPolicy.PUBLIC,
location: initialLocation,
items: []
2025-02-08 19:07:43 +03:00
},
mode: 'onChange',
reValidateMode: 'onChange'
});
function onSubmit(data: ICloneLibraryItemDTO) {
return cloneItem(data).then(newSchema => router.pushAsync({ path: urls.schema(newSchema.id), force: true }));
2024-06-07 20:17:03 +03:00
}
return (
2025-02-06 20:27:56 +03:00
<ModalForm
2024-06-07 20:17:03 +03:00
header='Создание копии концептуальной схемы'
submitText='Создать'
2025-02-08 19:07:43 +03:00
canSubmit={isValid}
onSubmit={event => void handleSubmit(onSubmit)(event)}
2025-03-10 16:01:40 +03:00
className='px-6 py-2 cc-column h-fit w-120'
2024-06-07 20:17:03 +03:00
>
<TextInput
2025-02-08 19:07:43 +03:00
id='dlg_full_name' //
label='Название'
2025-02-08 19:07:43 +03:00
{...register('title')}
error={errors.title}
2024-06-07 20:17:03 +03:00
/>
2025-03-10 16:01:40 +03:00
2024-06-07 20:17:03 +03:00
<div className='flex justify-between gap-3'>
2025-03-10 16:01:40 +03:00
<TextInput id='dlg_alias' label='Сокращение' className='w-64' {...register('alias')} error={errors.alias} />
2024-06-07 20:17:03 +03:00
<div className='flex flex-col gap-2'>
<Label text='Доступ' className='self-center select-none' />
<div className='ml-auto cc-icons'>
2025-02-08 19:07:43 +03:00
<Controller
control={control}
name='access_policy'
render={({ field }) => (
<SelectAccessPolicy
value={field.value} //
onChange={field.onChange}
stretchLeft
/>
)}
2024-06-07 20:17:03 +03:00
/>
2025-02-08 19:07:43 +03:00
<Controller
control={control}
name='visible'
render={({ field }) => (
<MiniButton
title={field.value ? 'Библиотека: отображать' : 'Библиотека: скрывать'}
2025-03-20 11:33:19 +03:00
aria-label='Переключатель отображения библиотеки'
2025-02-26 00:16:22 +03:00
icon={<IconItemVisibility value={field.value} />}
2025-02-08 19:07:43 +03:00
onClick={() => field.onChange(!field.value)}
/>
)}
2024-06-07 20:17:03 +03:00
/>
</div>
</div>
</div>
2025-03-11 22:24:51 +03:00
<Controller
control={control}
name='location'
render={({ field }) => (
2025-03-14 21:06:31 +03:00
<PickLocation
value={field.value} //
rows={2}
onChange={field.onChange}
className={!!errors.location ? '-mb-6' : undefined}
error={errors.location}
/>
2025-03-11 22:24:51 +03:00
)}
/>
2024-06-07 20:17:03 +03:00
<TextArea id='dlg_comment' {...register('description')} label='Описание' rows={4} error={errors.description} />
2025-02-08 19:07:43 +03:00
{selected.length > 0 ? (
<Controller
control={control}
name='items'
render={({ field }) => (
<Checkbox
id='dlg_only_selected'
label={`Только выбранные конституенты [${selected.length} из ${totalCount}]`}
value={field.value.length > 0}
onChange={value => field.onChange(value ? selected : [])}
2025-02-08 19:07:43 +03:00
/>
)}
/>
) : null}
2025-02-06 20:27:56 +03:00
</ModalForm>
2024-06-07 20:17:03 +03:00
);
}