ConceptPortal-public/rsconcept/frontend/src/dialogs/DlgCloneLibraryItem.tsx

145 lines
5.0 KiB
TypeScript
Raw Normal View History

'use client';
import clsx from 'clsx';
import { useState } from 'react';
2023-12-04 14:19:54 +03:00
import { useConceptNavigation } from '@/app/Navigation/NavigationContext';
import { urls } from '@/app/urls';
import { useAuthSuspense } from '@/backend/auth/useAuth';
2025-02-06 14:10:18 +03:00
import { ICloneLibraryItemDTO } from '@/backend/library/api';
import { useCloneItem } from '@/backend/library/useCloneItem';
import { VisibilityIcon } from '@/components/DomainIcons';
import SelectAccessPolicy from '@/components/select/SelectAccessPolicy';
2024-06-26 19:00:29 +03:00
import SelectLocationContext from '@/components/select/SelectLocationContext';
import SelectLocationHead from '@/components/select/SelectLocationHead';
2025-02-07 10:54:47 +03:00
import { MiniButton } from '@/components/ui/Control';
import { Checkbox, Label, TextArea, TextInput } from '@/components/ui/Input';
import { ModalForm } from '@/components/ui/Modal';
import { AccessPolicy, ILibraryItem, LocationHead } from '@/models/library';
import { cloneTitle, combineLocation, validateLocation } from '@/models/libraryAPI';
import { ConstituentaID } from '@/models/rsform';
import { useDialogsStore } from '@/stores/dialogs';
2023-12-04 14:19:54 +03:00
export interface DlgCloneLibraryItemProps {
2023-12-28 14:04:44 +03:00
base: ILibraryItem;
initialLocation: string;
selected: ConstituentaID[];
totalCount: number;
2023-12-04 14:19:54 +03:00
}
function DlgCloneLibraryItem() {
const { base, initialLocation, selected, totalCount } = useDialogsStore(
state => state.props as DlgCloneLibraryItemProps
);
const router = useConceptNavigation();
const { user } = useAuthSuspense();
const [title, setTitle] = useState(cloneTitle(base));
const [alias, setAlias] = useState(base.alias);
const [comment, setComment] = useState(base.comment);
const [visible, setVisible] = useState(true);
const [policy, setPolicy] = useState(AccessPolicy.PUBLIC);
const [onlySelected, setOnlySelected] = useState(false);
2023-12-04 14:19:54 +03:00
const [head, setHead] = useState(initialLocation.substring(0, 2) as LocationHead);
const [body, setBody] = useState(initialLocation.substring(3));
const location = combineLocation(head, body);
const { cloneItem } = useCloneItem();
2023-12-04 14:19:54 +03:00
const canSubmit = title !== '' && alias !== '' && validateLocation(location);
2023-12-04 14:19:54 +03:00
function handleSelectLocation(newValue: string) {
2024-06-26 19:00:29 +03:00
setHead(newValue.substring(0, 2) as LocationHead);
setBody(newValue.length > 3 ? newValue.substring(3) : '');
}
2024-06-26 19:00:29 +03:00
2023-12-04 14:19:54 +03:00
function handleSubmit() {
2025-02-06 14:10:18 +03:00
const data: ICloneLibraryItemDTO = {
id: base.id,
2023-12-04 14:19:54 +03:00
item_type: base.item_type,
title: title,
alias: alias,
comment: comment,
read_only: false,
visible: visible,
access_policy: policy,
location: location
2023-12-04 14:19:54 +03:00
};
if (onlySelected) {
data.items = selected;
}
cloneItem(data, newSchema => router.push(urls.schema(newSchema.id)));
2025-02-06 14:10:18 +03:00
return true;
2023-12-04 14:19:54 +03:00
}
return (
<ModalForm
2023-12-28 14:04:44 +03:00
header='Создание копии концептуальной схемы'
canSubmit={canSubmit}
submitText='Создать'
onSubmit={handleSubmit}
className={clsx('px-6 py-2', 'cc-column', 'max-h-full w-[30rem]')}
2023-12-28 14:04:44 +03:00
>
<TextInput
id='dlg_full_name'
label='Полное название'
value={title}
onChange={event => setTitle(event.target.value)}
/>
<div className='flex justify-between gap-3'>
<TextInput
id='dlg_alias'
label='Сокращение'
value={alias}
className='w-[15rem]'
onChange={event => setAlias(event.target.value)}
/>
<div className='flex flex-col gap-2'>
<Label text='Доступ' className='self-center select-none' />
<div className='ml-auto cc-icons'>
<SelectAccessPolicy
stretchLeft // prettier: split-lines
value={policy}
onChange={newPolicy => setPolicy(newPolicy)}
/>
<MiniButton
title={visible ? 'Библиотека: отображать' : 'Библиотека: скрывать'}
icon={<VisibilityIcon value={visible} />}
onClick={() => setVisible(prev => !prev)}
/>
</div>
</div>
</div>
<div className='flex justify-between gap-3'>
<div className='flex flex-col gap-2 w-[7rem] h-min'>
<Label text='Корень' />
<SelectLocationHead value={head} onChange={setHead} excluded={!user.is_staff ? [LocationHead.LIBRARY] : []} />
</div>
<SelectLocationContext value={location} onChange={handleSelectLocation} />
<TextArea
id='dlg_cst_body'
label='Путь'
rows={3}
value={body}
onChange={event => setBody(event.target.value)}
/>
</div>
<TextArea id='dlg_comment' label='Описание' value={comment} onChange={event => setComment(event.target.value)} />
<Checkbox
id='dlg_only_selected'
label={`Только выбранные конституенты [${selected.length} из ${totalCount}]`}
value={onlySelected}
onChange={value => setOnlySelected(value)}
/>
</ModalForm>
2023-12-28 14:04:44 +03:00
);
2023-12-04 14:19:54 +03:00
}
2023-12-28 14:04:44 +03:00
export default DlgCloneLibraryItem;