2023-07-28 01:37:26 +03:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { useNavigate } from 'react-router-dom';
|
|
|
|
import { toast } from 'react-toastify';
|
|
|
|
|
|
|
|
import Checkbox from '../../components/Common/Checkbox';
|
|
|
|
import Modal from '../../components/Common/Modal';
|
|
|
|
import TextArea from '../../components/Common/TextArea';
|
|
|
|
import TextInput from '../../components/Common/TextInput';
|
2023-08-11 19:28:12 +03:00
|
|
|
import { useLibrary } from '../../context/LibraryContext';
|
2023-07-28 01:37:26 +03:00
|
|
|
import { useRSForm } from '../../context/RSFormContext';
|
|
|
|
import { IRSFormCreateData } from '../../utils/models';
|
|
|
|
import { getCloneTitle } from '../../utils/staticUI';
|
|
|
|
|
|
|
|
interface DlgCloneRSFormProps {
|
|
|
|
hideWindow: () => void
|
|
|
|
}
|
|
|
|
|
2023-07-29 03:31:21 +03:00
|
|
|
function DlgCloneRSForm({ hideWindow }: DlgCloneRSFormProps) {
|
2023-07-28 01:37:26 +03:00
|
|
|
const navigate = useNavigate();
|
|
|
|
const [title, setTitle] = useState('');
|
|
|
|
const [alias, setAlias] = useState('');
|
|
|
|
const [comment, setComment] = useState('');
|
|
|
|
const [common, setCommon] = useState(false);
|
|
|
|
|
2023-08-11 19:28:12 +03:00
|
|
|
const { cloneSchema } = useLibrary();
|
|
|
|
const { schema } = useRSForm();
|
2023-07-28 01:37:26 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (schema) {
|
|
|
|
setTitle(getCloneTitle(schema))
|
|
|
|
setAlias(schema.alias)
|
|
|
|
setComment(schema.comment)
|
|
|
|
setCommon(schema.is_common)
|
|
|
|
}
|
|
|
|
}, [schema, schema?.title, schema?.alias, schema?.comment, schema?.is_common]);
|
|
|
|
|
|
|
|
const handleSubmit = () => {
|
2023-08-11 19:28:12 +03:00
|
|
|
if (!schema) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-28 01:37:26 +03:00
|
|
|
const data: IRSFormCreateData = {
|
|
|
|
title: title,
|
|
|
|
alias: alias,
|
|
|
|
comment: comment,
|
|
|
|
is_common: common
|
|
|
|
};
|
2023-08-11 19:28:12 +03:00
|
|
|
cloneSchema(schema.id, data, newSchema => {
|
2023-07-28 01:37:26 +03:00
|
|
|
toast.success(`Схема создана: ${newSchema.alias}`);
|
|
|
|
navigate(`/rsforms/${newSchema.id}`);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title='Создание копии концептуальной схемы'
|
|
|
|
hideWindow={hideWindow}
|
|
|
|
canSubmit={true}
|
|
|
|
submitText='Создать'
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
>
|
|
|
|
<TextInput id='title' label='Полное название' type='text'
|
|
|
|
required
|
|
|
|
value={title}
|
|
|
|
onChange={event => { setTitle(event.target.value); }}
|
|
|
|
/>
|
|
|
|
<TextInput id='alias' label='Сокращение' type='text'
|
|
|
|
required
|
|
|
|
value={alias}
|
|
|
|
widthClass='max-w-sm'
|
|
|
|
onChange={event => { setAlias(event.target.value); }}
|
|
|
|
/>
|
|
|
|
<TextArea id='comment' label='Комментарий'
|
|
|
|
value={comment}
|
|
|
|
onChange={event => { setComment(event.target.value); }}
|
|
|
|
/>
|
|
|
|
<Checkbox id='common' label='Общедоступная схема'
|
|
|
|
value={common}
|
|
|
|
onChange={event => { setCommon(event.target.checked); }}
|
|
|
|
/>
|
|
|
|
</Modal>
|
2023-07-29 03:31:21 +03:00
|
|
|
);
|
2023-07-28 01:37:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default DlgCloneRSForm;
|