2024-06-02 23:41:46 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2025-02-04 23:34:02 +03:00
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2024-06-02 23:41:46 +03:00
|
|
|
|
import clsx from 'clsx';
|
2025-02-04 23:34:02 +03:00
|
|
|
|
import { useRef } from 'react';
|
|
|
|
|
import { Controller, useForm, useWatch } from 'react-hook-form';
|
2024-06-02 23:41:46 +03:00
|
|
|
|
|
2025-01-27 15:50:37 +03:00
|
|
|
|
import { useConceptNavigation } from '@/app/Navigation/NavigationContext';
|
2024-06-02 23:41:46 +03:00
|
|
|
|
import { urls } from '@/app/urls';
|
2025-01-29 16:18:41 +03:00
|
|
|
|
import { useAuthSuspense } from '@/backend/auth/useAuth';
|
2025-02-04 23:34:02 +03:00
|
|
|
|
import { CreateLibraryItemSchema, ICreateLibraryItemDTO } from '@/backend/library/api';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
import { useCreateItem } from '@/backend/library/useCreateItem';
|
2024-06-02 23:41:46 +03:00
|
|
|
|
import { VisibilityIcon } from '@/components/DomainIcons';
|
|
|
|
|
import { IconDownload } from '@/components/Icons';
|
|
|
|
|
import InfoError from '@/components/info/InfoError';
|
|
|
|
|
import SelectAccessPolicy from '@/components/select/SelectAccessPolicy';
|
2024-06-03 17:38:30 +03:00
|
|
|
|
import SelectItemType from '@/components/select/SelectItemType';
|
2024-06-26 19:00:29 +03:00
|
|
|
|
import SelectLocationContext from '@/components/select/SelectLocationContext';
|
2024-06-02 23:41:46 +03:00
|
|
|
|
import SelectLocationHead from '@/components/select/SelectLocationHead';
|
|
|
|
|
import Button from '@/components/ui/Button';
|
|
|
|
|
import Label from '@/components/ui/Label';
|
|
|
|
|
import MiniButton from '@/components/ui/MiniButton';
|
|
|
|
|
import Overlay from '@/components/ui/Overlay';
|
|
|
|
|
import SubmitButton from '@/components/ui/SubmitButton';
|
|
|
|
|
import TextArea from '@/components/ui/TextArea';
|
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
|
|
|
|
import { AccessPolicy, LibraryItemType, LocationHead } from '@/models/library';
|
2025-02-04 23:34:02 +03:00
|
|
|
|
import { combineLocation } from '@/models/libraryAPI';
|
2025-01-15 16:06:42 +03:00
|
|
|
|
import { useLibrarySearchStore } from '@/stores/librarySearch';
|
2024-09-15 21:19:17 +03:00
|
|
|
|
import { EXTEOR_TRS_FILE } from '@/utils/constants';
|
2024-06-02 23:41:46 +03:00
|
|
|
|
|
|
|
|
|
function FormCreateItem() {
|
|
|
|
|
const router = useConceptNavigation();
|
2025-01-29 16:18:41 +03:00
|
|
|
|
const { user } = useAuthSuspense();
|
2025-02-04 23:34:02 +03:00
|
|
|
|
const { createItem, isPending, error: serverError, reset: clearServerError } = useCreateItem();
|
2024-06-02 23:41:46 +03:00
|
|
|
|
|
2025-01-15 16:06:42 +03:00
|
|
|
|
const searchLocation = useLibrarySearchStore(state => state.location);
|
|
|
|
|
const setSearchLocation = useLibrarySearchStore(state => state.setLocation);
|
|
|
|
|
|
2025-02-04 23:34:02 +03:00
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
clearErrors,
|
|
|
|
|
setValue,
|
|
|
|
|
control,
|
|
|
|
|
formState: { errors }
|
|
|
|
|
} = useForm<ICreateLibraryItemDTO>({
|
|
|
|
|
resolver: zodResolver(CreateLibraryItemSchema),
|
|
|
|
|
defaultValues: {
|
|
|
|
|
item_type: LibraryItemType.RSFORM,
|
|
|
|
|
access_policy: AccessPolicy.PUBLIC,
|
|
|
|
|
visible: true,
|
|
|
|
|
read_only: false,
|
|
|
|
|
location: !!searchLocation ? searchLocation : LocationHead.USER
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const itemType = useWatch({ control, name: 'item_type' });
|
|
|
|
|
const file = useWatch({ control, name: 'file' });
|
2024-06-02 23:41:46 +03:00
|
|
|
|
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
|
|
|
|
2025-02-04 23:34:02 +03:00
|
|
|
|
function resetErrors() {
|
|
|
|
|
clearServerError();
|
|
|
|
|
clearErrors();
|
|
|
|
|
}
|
2024-06-02 23:41:46 +03:00
|
|
|
|
|
|
|
|
|
function handleCancel() {
|
|
|
|
|
if (router.canBack()) {
|
|
|
|
|
router.back();
|
|
|
|
|
} else {
|
|
|
|
|
router.push(urls.library);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleFileChange(event: React.ChangeEvent<HTMLInputElement>) {
|
|
|
|
|
if (event.target.files && event.target.files.length > 0) {
|
2025-02-04 23:34:02 +03:00
|
|
|
|
setValue('file', event.target.files[0]);
|
|
|
|
|
setValue('fileName', event.target.files[0].name);
|
2024-06-02 23:41:46 +03:00
|
|
|
|
} else {
|
2025-02-04 23:34:02 +03:00
|
|
|
|
setValue('file', undefined);
|
|
|
|
|
setValue('fileName', '');
|
2024-06-02 23:41:46 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-04 23:34:02 +03:00
|
|
|
|
function handleItemTypeChange(value: LibraryItemType) {
|
|
|
|
|
if (value !== LibraryItemType.RSFORM) {
|
|
|
|
|
setValue('file', undefined);
|
|
|
|
|
setValue('fileName', '');
|
2024-08-01 20:11:32 +03:00
|
|
|
|
}
|
2025-02-04 23:34:02 +03:00
|
|
|
|
setValue('item_type', value);
|
|
|
|
|
}
|
2024-08-01 20:11:32 +03:00
|
|
|
|
|
2025-02-04 23:34:02 +03:00
|
|
|
|
function onSubmit(data: ICreateLibraryItemDTO) {
|
|
|
|
|
createItem(data, newItem => {
|
|
|
|
|
setSearchLocation(data.location);
|
|
|
|
|
if (newItem.item_type == LibraryItemType.RSFORM) {
|
|
|
|
|
router.push(urls.schema(newItem.id));
|
|
|
|
|
} else {
|
|
|
|
|
router.push(urls.oss(newItem.id));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-09-02 18:38:36 +03:00
|
|
|
|
|
2024-06-02 23:41:46 +03:00
|
|
|
|
return (
|
2024-12-12 13:19:12 +03:00
|
|
|
|
<form
|
|
|
|
|
className={clsx('cc-fade-in cc-column', 'min-w-[30rem] max-w-[30rem] mx-auto', 'px-6 py-3')}
|
2025-02-04 23:34:02 +03:00
|
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
|
|
|
|
onChange={resetErrors}
|
2024-12-12 13:19:12 +03:00
|
|
|
|
>
|
2024-09-07 16:26:41 +03:00
|
|
|
|
<h1 className='select-none'>
|
2024-09-02 18:38:36 +03:00
|
|
|
|
{itemType == LibraryItemType.RSFORM ? (
|
|
|
|
|
<Overlay position='top-0 right-[0.5rem]'>
|
2025-02-04 23:34:02 +03:00
|
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name='file'
|
|
|
|
|
render={() => (
|
|
|
|
|
<input
|
|
|
|
|
id='schema_file'
|
|
|
|
|
ref={inputRef}
|
|
|
|
|
type='file'
|
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
|
accept={EXTEOR_TRS_FILE}
|
|
|
|
|
onChange={handleFileChange}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-09-02 18:38:36 +03:00
|
|
|
|
/>
|
|
|
|
|
<MiniButton
|
|
|
|
|
title='Загрузить из Экстеор'
|
|
|
|
|
icon={<IconDownload size='1.25rem' className='icon-primary' />}
|
|
|
|
|
onClick={() => inputRef.current?.click()}
|
|
|
|
|
/>
|
|
|
|
|
</Overlay>
|
|
|
|
|
) : null}
|
2024-06-05 13:00:28 +03:00
|
|
|
|
Создание схемы
|
|
|
|
|
</h1>
|
|
|
|
|
|
2025-02-04 23:34:02 +03:00
|
|
|
|
{file ? <Label className='text-wrap' text={`Загружен файл: ${file.name}`} /> : null}
|
2024-06-05 13:00:28 +03:00
|
|
|
|
|
|
|
|
|
<TextInput
|
|
|
|
|
id='schema_title'
|
2025-02-04 23:34:02 +03:00
|
|
|
|
{...register('title')}
|
2024-06-05 13:00:28 +03:00
|
|
|
|
label='Полное название'
|
|
|
|
|
placeholder={file && 'Загрузить из файла'}
|
2025-02-04 23:34:02 +03:00
|
|
|
|
error={errors.title}
|
2024-06-05 13:00:28 +03:00
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div className='flex justify-between gap-3'>
|
2024-06-02 23:41:46 +03:00
|
|
|
|
<TextInput
|
2024-06-05 13:00:28 +03:00
|
|
|
|
id='schema_alias'
|
2025-02-04 23:34:02 +03:00
|
|
|
|
{...register('alias')}
|
2024-06-05 13:00:28 +03:00
|
|
|
|
label='Сокращение'
|
2024-06-02 23:41:46 +03:00
|
|
|
|
placeholder={file && 'Загрузить из файла'}
|
2024-08-22 23:24:10 +03:00
|
|
|
|
className='w-[16rem]'
|
2025-02-04 23:34:02 +03:00
|
|
|
|
error={errors.alias}
|
2024-06-02 23:41:46 +03:00
|
|
|
|
/>
|
2024-09-02 18:42:19 +03:00
|
|
|
|
<div className='flex flex-col items-center gap-2'>
|
|
|
|
|
<Label text='Тип схемы' className='self-center select-none' />
|
2025-02-04 23:34:02 +03:00
|
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name='item_type'
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<SelectItemType
|
|
|
|
|
value={field.value} //
|
|
|
|
|
onChange={handleItemTypeChange}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2024-09-02 18:42:19 +03:00
|
|
|
|
</div>
|
2024-06-05 13:00:28 +03:00
|
|
|
|
|
|
|
|
|
<div className='flex flex-col gap-2'>
|
|
|
|
|
<Label text='Доступ' className='self-center select-none' />
|
|
|
|
|
<div className='ml-auto cc-icons'>
|
2025-02-04 23:34:02 +03:00
|
|
|
|
<Controller
|
|
|
|
|
control={control} //
|
|
|
|
|
name='access_policy'
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<SelectAccessPolicy
|
|
|
|
|
value={field.value} //
|
|
|
|
|
onChange={field.onChange}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name='visible'
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<MiniButton
|
|
|
|
|
title={field.value ? 'Библиотека: отображать' : 'Библиотека: скрывать'}
|
|
|
|
|
icon={<VisibilityIcon value={field.value} />}
|
|
|
|
|
onClick={() => field.onChange(!field.value)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-06-02 23:41:46 +03:00
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-06-05 13:00:28 +03:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<TextArea
|
|
|
|
|
id='schema_comment'
|
2025-02-04 23:34:02 +03:00
|
|
|
|
{...register('comment')}
|
2024-06-05 13:00:28 +03:00
|
|
|
|
label='Описание'
|
|
|
|
|
placeholder={file && 'Загрузить из файла'}
|
2025-02-04 23:34:02 +03:00
|
|
|
|
error={errors.comment}
|
2024-06-05 13:00:28 +03:00
|
|
|
|
/>
|
|
|
|
|
|
2024-08-24 11:21:02 +03:00
|
|
|
|
<div className='flex justify-between gap-3 flex-grow'>
|
|
|
|
|
<div className='flex flex-col gap-2 min-w-[7rem] h-min'>
|
2024-06-05 13:00:28 +03:00
|
|
|
|
<Label text='Корень' />
|
2025-02-04 23:34:02 +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-02 23:41:46 +03:00
|
|
|
|
</div>
|
2025-02-04 23:34:02 +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_cst_body'
|
|
|
|
|
label='Путь'
|
|
|
|
|
rows={4}
|
|
|
|
|
value={field.value.substring(3)}
|
|
|
|
|
onChange={event => field.onChange(combineLocation(field.value.substring(0, 2), event.target.value))}
|
|
|
|
|
error={errors.location}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-06-05 13:00:28 +03:00
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className='flex justify-around gap-6 py-3'>
|
2025-02-04 23:34:02 +03:00
|
|
|
|
<SubmitButton text='Создать схему' loading={isPending} className='min-w-[10rem]' />
|
2024-06-05 13:00:28 +03:00
|
|
|
|
<Button text='Отмена' className='min-w-[10rem]' onClick={() => handleCancel()} />
|
|
|
|
|
</div>
|
2025-02-04 23:34:02 +03:00
|
|
|
|
{serverError ? <InfoError error={serverError} /> : null}
|
2024-06-05 13:00:28 +03:00
|
|
|
|
</form>
|
2024-06-02 23:41:46 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FormCreateItem;
|