Portal/rsconcept/frontend/src/features/library/dialogs/DlgCloneLibraryItem.tsx

134 lines
4.3 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
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-02-12 15:12:59 +03:00
import { useCloneItem } from '../backend/useCloneItem';
2025-02-26 00:16:22 +03:00
import { IconItemVisibility } from '../components/IconItemVisibility';
2025-03-11 22:24:51 +03:00
import { PickLocation } from '../components/PickLocation';
2025-02-12 15:12:59 +03:00
import { SelectAccessPolicy } from '../components/SelectAccessPolicy';
2025-03-11 22:24:51 +03:00
import { cloneTitle } from '../models/libraryAPI';
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,
comment: base.comment,
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' //
2024-06-07 20:17:03 +03:00
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-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 }) => (
<PickLocation value={field.value} rows={2} onChange={field.onChange} error={errors.location} />
)}
/>
2024-06-07 20:17:03 +03:00
2025-03-07 16:21:18 +03:00
<TextArea id='dlg_comment' {...register('comment')} label='Описание' rows={4} error={errors.comment} />
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
);
}