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

177 lines
5.8 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 clsx from 'clsx';
import { urls, useConceptNavigation } from '@/app';
2025-02-12 21:36:03 +03:00
import { useAuthSuspense } from '@/features/auth';
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-02-12 15:12:59 +03:00
import { SelectAccessPolicy } from '../components/SelectAccessPolicy';
import { SelectLocationContext } from '../components/SelectLocationContext';
import { SelectLocationHead } from '../components/SelectLocationHead';
2025-02-18 19:39:54 +03:00
import { LocationHead } from '../models/library';
2025-02-12 15:12:59 +03:00
import { cloneTitle, combineLocation } 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();
const { user } = useAuthSuspense();
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)}
2024-06-07 20:17:03 +03:00
className={clsx('px-6 py-2', 'cc-column', 'max-h-full w-[30rem]')}
>
<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
/>
<div className='flex justify-between gap-3'>
<TextInput
id='dlg_alias'
label='Сокращение'
2025-02-08 19:07:43 +03:00
className='w-[16rem]'
{...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>
<div className='flex justify-between gap-3'>
<div className='flex flex-col gap-2 w-[7rem] h-min'>
<Label text='Корень' />
2025-02-08 19:07:43 +03:00
<Controller
control={control} //
name='location'
render={({ field }) => (
<SelectLocationHead
value={field.value.substring(0, 2) as LocationHead}
onChange={newValue => field.onChange(combineLocation(newValue, field.value.substring(3)))}
excluded={!user.is_staff ? [LocationHead.LIBRARY] : []}
/>
)}
/>
2024-06-07 20:17:03 +03:00
</div>
2025-02-08 19:07:43 +03:00
<Controller
control={control} //
name='location'
render={({ field }) => (
<SelectLocationContext
value={field.value} //
onChange={field.onChange}
/>
)}
/>
<Controller
control={control} //
name='location'
render={({ field }) => (
<TextArea
id='dlg_location'
label='Путь'
rows={3}
value={field.value.substring(3)}
onChange={event => field.onChange(combineLocation(field.value.substring(0, 2), event.target.value))}
error={errors.location}
/>
)}
2024-06-07 20:17:03 +03:00
/>
</div>
2025-02-08 19:07:43 +03:00
<TextArea id='dlg_comment' {...register('comment')} label='Описание' error={errors.comment} />
{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
);
}