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

90 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-07-28 01:37:26 +03:00
import { useEffect, useState } from 'react';
import { toast } from 'react-toastify';
2023-11-01 13:47:49 +03:00
import Checkbox from '../components/Common/Checkbox';
import Modal, { ModalProps } from '../components/Common/Modal';
import TextArea from '../components/Common/TextArea';
import TextInput from '../components/Common/TextInput';
import { useLibrary } from '../context/LibraryContext';
import { useConceptNavigation } from '../context/NagivationContext';
import { useRSForm } from '../context/RSFormContext';
import { IRSFormCreateData } from '../models/rsform';
import { cloneTitle } from '../utils/misc';
2023-07-28 01:37:26 +03:00
2023-09-11 17:56:32 +03:00
interface DlgCloneRSFormProps
extends Pick<ModalProps, 'hideWindow'> {}
2023-07-28 01:37:26 +03:00
2023-07-29 03:31:21 +03:00
function DlgCloneRSForm({ hideWindow }: DlgCloneRSFormProps) {
2023-09-05 00:23:53 +03:00
const { navigateTo } = useConceptNavigation();
2023-07-28 01:37:26 +03:00
const [title, setTitle] = useState('');
const [alias, setAlias] = useState('');
const [comment, setComment] = useState('');
const [common, setCommon] = useState(false);
2023-08-25 22:51:20 +03:00
const [canonical, setCanonical] = useState(false);
2023-07-28 01:37:26 +03:00
2023-11-01 13:41:32 +03:00
const { cloneItem } = useLibrary();
const { schema } = useRSForm();
2023-07-28 01:37:26 +03:00
useEffect(() => {
if (schema) {
2023-09-21 14:58:01 +03:00
setTitle(cloneTitle(schema));
2023-08-25 22:51:20 +03:00
setAlias(schema.alias);
setComment(schema.comment);
setCommon(schema.is_common);
setCanonical(false);
2023-07-28 01:37:26 +03:00
}
}, [schema, schema?.title, schema?.alias, schema?.comment, schema?.is_common]);
const handleSubmit = () => {
if (!schema) {
return;
}
2023-07-28 01:37:26 +03:00
const data: IRSFormCreateData = {
2023-08-25 22:51:20 +03:00
item_type: schema.item_type,
2023-07-28 01:37:26 +03:00
title: title,
alias: alias,
comment: comment,
2023-08-25 22:51:20 +03:00
is_common: common,
is_canonical: canonical
2023-07-28 01:37:26 +03:00
};
2023-11-01 13:41:32 +03:00
cloneItem(schema.id, data, newSchema => {
2023-07-28 01:37:26 +03:00
toast.success(`Схема создана: ${newSchema.alias}`);
2023-09-05 00:23:53 +03:00
navigateTo(`/rsforms/${newSchema.id}`);
2023-07-28 01:37:26 +03:00
});
};
return (
<Modal
title='Создание копии концептуальной схемы'
hideWindow={hideWindow}
canSubmit={true}
submitText='Создать'
onSubmit={handleSubmit}
>
<div className='flex flex-col gap-3'>
2023-07-28 01:37:26 +03:00
<TextInput id='title' label='Полное название' type='text'
required
value={title}
2023-08-27 16:35:17 +03:00
onChange={event => setTitle(event.target.value)}
2023-07-28 01:37:26 +03:00
/>
<TextInput id='alias' label='Сокращение' type='text'
required
value={alias}
dimensions='max-w-sm'
2023-08-27 16:35:17 +03:00
onChange={event => setAlias(event.target.value)}
2023-07-28 01:37:26 +03:00
/>
<TextArea id='comment' label='Комментарий'
value={comment}
2023-08-27 16:35:17 +03:00
onChange={event => setComment(event.target.value)}
2023-07-28 01:37:26 +03:00
/>
<Checkbox id='common' label='Общедоступная схема'
value={common}
2023-09-07 16:30:43 +03:00
setValue={value => setCommon(value)}
2023-07-28 01:37:26 +03:00
/>
</div>
2023-07-28 01:37:26 +03:00
</Modal>
2023-07-29 03:31:21 +03:00
);
2023-07-28 01:37:26 +03:00
}
export default DlgCloneRSForm;