2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2023-12-18 19:42:27 +03:00
|
|
|
import clsx from 'clsx';
|
2024-06-26 19:00:29 +03:00
|
|
|
import { useCallback, useMemo, useState } from 'react';
|
2023-12-04 14:19:54 +03:00
|
|
|
import { toast } from 'react-toastify';
|
|
|
|
|
2024-04-01 21:45:10 +03:00
|
|
|
import { urls } from '@/app/urls';
|
2024-06-02 23:41:46 +03:00
|
|
|
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';
|
2024-06-02 23:41:46 +03:00
|
|
|
import SelectLocationHead from '@/components/select/SelectLocationHead';
|
2024-01-04 19:38:12 +03:00
|
|
|
import Checkbox from '@/components/ui/Checkbox';
|
2024-06-02 23:41:46 +03:00
|
|
|
import Label from '@/components/ui/Label';
|
|
|
|
import MiniButton from '@/components/ui/MiniButton';
|
2024-01-04 19:38:12 +03:00
|
|
|
import Modal, { ModalProps } from '@/components/ui/Modal';
|
|
|
|
import TextArea from '@/components/ui/TextArea';
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
2024-06-02 23:41:46 +03:00
|
|
|
import { useAuth } from '@/context/AuthContext';
|
2023-12-13 14:32:57 +03:00
|
|
|
import { useLibrary } from '@/context/LibraryContext';
|
2023-12-26 14:23:51 +03:00
|
|
|
import { useConceptNavigation } from '@/context/NavigationContext';
|
2024-06-02 23:41:46 +03:00
|
|
|
import { AccessPolicy, ILibraryItem, LocationHead } from '@/models/library';
|
|
|
|
import { cloneTitle, combineLocation, validateLocation } from '@/models/libraryAPI';
|
2024-04-04 14:34:25 +03:00
|
|
|
import { ConstituentaID, IRSFormCloneData } from '@/models/rsform';
|
2024-06-06 23:11:53 +03:00
|
|
|
import { information } from '@/utils/labels';
|
2023-12-04 14:19:54 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
interface DlgCloneLibraryItemProps extends Pick<ModalProps, 'hideWindow'> {
|
|
|
|
base: ILibraryItem;
|
2024-06-02 23:41:46 +03:00
|
|
|
initialLocation: string;
|
2024-04-04 14:34:25 +03:00
|
|
|
selected: ConstituentaID[];
|
|
|
|
totalCount: number;
|
2023-12-04 14:19:54 +03:00
|
|
|
}
|
|
|
|
|
2024-06-02 23:41:46 +03:00
|
|
|
function DlgCloneLibraryItem({ hideWindow, base, initialLocation, selected, totalCount }: DlgCloneLibraryItemProps) {
|
2023-12-13 14:32:57 +03:00
|
|
|
const router = useConceptNavigation();
|
2024-06-02 23:41:46 +03:00
|
|
|
const { user } = useAuth();
|
2024-04-04 14:34:25 +03:00
|
|
|
const [title, setTitle] = useState(cloneTitle(base));
|
|
|
|
const [alias, setAlias] = useState(base.alias);
|
|
|
|
const [comment, setComment] = useState(base.comment);
|
2024-06-02 23:41:46 +03:00
|
|
|
const [visible, setVisible] = useState(true);
|
|
|
|
const [policy, setPolicy] = useState(AccessPolicy.PUBLIC);
|
|
|
|
|
2024-04-04 14:34:25 +03:00
|
|
|
const [onlySelected, setOnlySelected] = useState(false);
|
2023-12-04 14:19:54 +03:00
|
|
|
|
2024-06-02 23:41:46 +03:00
|
|
|
const [head, setHead] = useState(initialLocation.substring(0, 2) as LocationHead);
|
|
|
|
const [body, setBody] = useState(initialLocation.substring(3));
|
|
|
|
const location = useMemo(() => combineLocation(head, body), [head, body]);
|
|
|
|
|
2024-06-26 19:00:29 +03:00
|
|
|
const { cloneItem, folders } = useLibrary();
|
2023-12-04 14:19:54 +03:00
|
|
|
|
2024-06-02 23:41:46 +03:00
|
|
|
const canSubmit = useMemo(() => title !== '' && alias !== '' && validateLocation(location), [title, alias, location]);
|
2023-12-04 14:19:54 +03:00
|
|
|
|
2024-06-26 19:00:29 +03:00
|
|
|
const handleSelectLocation = useCallback((newValue: string) => {
|
|
|
|
setHead(newValue.substring(0, 2) as LocationHead);
|
|
|
|
setBody(newValue.length > 3 ? newValue.substring(3) : '');
|
|
|
|
}, []);
|
|
|
|
|
2023-12-04 14:19:54 +03:00
|
|
|
function handleSubmit() {
|
2024-04-04 14:34:25 +03:00
|
|
|
const data: IRSFormCloneData = {
|
2023-12-04 14:19:54 +03:00
|
|
|
item_type: base.item_type,
|
|
|
|
title: title,
|
|
|
|
alias: alias,
|
|
|
|
comment: comment,
|
2024-06-02 23:41:46 +03:00
|
|
|
read_only: false,
|
|
|
|
visible: visible,
|
|
|
|
access_policy: policy,
|
|
|
|
location: location
|
2023-12-04 14:19:54 +03:00
|
|
|
};
|
2024-04-04 14:34:25 +03:00
|
|
|
if (onlySelected) {
|
|
|
|
data.items = selected;
|
|
|
|
}
|
2023-12-04 14:19:54 +03:00
|
|
|
cloneItem(base.id, data, newSchema => {
|
2024-06-06 23:11:53 +03:00
|
|
|
toast.success(information.cloneComplete(newSchema.alias));
|
2024-04-01 21:45:10 +03:00
|
|
|
router.push(urls.schema(newSchema.id));
|
2023-12-04 14:19:54 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-12-28 14:04:44 +03:00
|
|
|
<Modal
|
|
|
|
header='Создание копии концептуальной схемы'
|
|
|
|
hideWindow={hideWindow}
|
|
|
|
canSubmit={canSubmit}
|
|
|
|
submitText='Создать'
|
|
|
|
onSubmit={handleSubmit}
|
2024-06-02 23:41:46 +03:00
|
|
|
className={clsx('px-6 py-2', 'cc-column', 'max-h-full w-[30rem]')}
|
2023-12-28 14:04:44 +03:00
|
|
|
>
|
|
|
|
<TextInput
|
2024-03-18 16:21:39 +03:00
|
|
|
id='dlg_full_name'
|
|
|
|
label='Полное название'
|
|
|
|
value={title}
|
|
|
|
onChange={event => setTitle(event.target.value)}
|
|
|
|
/>
|
2024-06-02 23:41:46 +03:00
|
|
|
<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>
|
2024-06-26 19:00:29 +03:00
|
|
|
<SelectLocationContext folderTree={folders} value={location} onChange={handleSelectLocation} />
|
2024-06-02 23:41:46 +03:00
|
|
|
<TextArea
|
|
|
|
id='dlg_cst_body'
|
|
|
|
label='Путь'
|
|
|
|
rows={3}
|
|
|
|
value={body}
|
|
|
|
onChange={event => setBody(event.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2024-04-04 14:34:25 +03:00
|
|
|
<TextArea id='dlg_comment' label='Описание' value={comment} onChange={event => setComment(event.target.value)} />
|
2024-06-02 23:41:46 +03:00
|
|
|
|
2024-04-04 14:34:25 +03:00
|
|
|
<Checkbox
|
|
|
|
id='dlg_only_selected'
|
|
|
|
label={`Только выбранные конституенты [${selected.length} из ${totalCount}]`}
|
|
|
|
value={onlySelected}
|
|
|
|
setValue={value => setOnlySelected(value)}
|
2024-03-18 16:21:39 +03:00
|
|
|
/>
|
2023-12-28 14:04:44 +03:00
|
|
|
</Modal>
|
|
|
|
);
|
2023-12-04 14:19:54 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default DlgCloneLibraryItem;
|