2023-09-08 13:46:45 +03:00
|
|
|
import { useEffect, useRef, useState } from 'react';
|
2023-09-05 00:23:53 +03:00
|
|
|
import { useLocation } from 'react-router-dom';
|
2023-07-25 20:27:29 +03:00
|
|
|
import { toast } from 'react-toastify';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
import BackendError from '../components/BackendError';
|
2023-09-02 01:11:27 +03:00
|
|
|
import Button from '../components/Common/Button';
|
2023-07-25 20:27:29 +03:00
|
|
|
import Checkbox from '../components/Common/Checkbox';
|
2023-07-15 17:46:19 +03:00
|
|
|
import Form from '../components/Common/Form';
|
2023-09-08 13:46:45 +03:00
|
|
|
import Label from '../components/Common/Label';
|
|
|
|
import MiniButton from '../components/Common/MiniButton';
|
2023-07-15 17:46:19 +03:00
|
|
|
import SubmitButton from '../components/Common/SubmitButton';
|
2023-07-25 20:27:29 +03:00
|
|
|
import TextArea from '../components/Common/TextArea';
|
|
|
|
import TextInput from '../components/Common/TextInput';
|
2023-09-08 13:46:45 +03:00
|
|
|
import { UploadIcon } from '../components/Icons';
|
2023-07-15 17:46:19 +03:00
|
|
|
import RequireAuth from '../components/RequireAuth';
|
2023-08-08 23:34:25 +03:00
|
|
|
import { useLibrary } from '../context/LibraryContext';
|
2023-09-05 00:23:53 +03:00
|
|
|
import { useConceptNavigation } from '../context/NagivationContext';
|
2023-09-08 13:46:45 +03:00
|
|
|
import { EXTEOR_TRS_FILE } from '../utils/constants';
|
2023-08-25 22:51:20 +03:00
|
|
|
import { IRSFormCreateData, LibraryItemType } from '../utils/models';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-07-28 00:03:37 +03:00
|
|
|
function CreateRSFormPage() {
|
2023-09-02 01:11:27 +03:00
|
|
|
const location = useLocation();
|
2023-09-05 00:23:53 +03:00
|
|
|
const { navigateTo, navigateHistory } = useConceptNavigation();
|
2023-08-09 17:19:12 +03:00
|
|
|
const { createSchema, error, setError, processing } = useLibrary();
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
|
|
const [title, setTitle] = useState('');
|
|
|
|
const [alias, setAlias] = useState('');
|
|
|
|
const [comment, setComment] = useState('');
|
|
|
|
const [common, setCommon] = useState(false);
|
2023-09-08 13:46:45 +03:00
|
|
|
|
|
|
|
const [fileName, setFileName] = useState('');
|
|
|
|
const [file, setFile] = useState<File | undefined>();
|
|
|
|
const inputRef = useRef<HTMLInputElement | null>(null);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-07-31 23:47:18 +03:00
|
|
|
useEffect(() => {
|
|
|
|
setError(undefined);
|
|
|
|
}, [title, alias, setError]);
|
|
|
|
|
2023-09-02 01:11:27 +03:00
|
|
|
function handleCancel() {
|
2023-09-03 18:26:50 +03:00
|
|
|
if (location.key !== 'default') {
|
2023-09-05 00:23:53 +03:00
|
|
|
navigateHistory(-1);
|
2023-09-02 01:11:27 +03:00
|
|
|
} else {
|
2023-09-05 00:23:53 +03:00
|
|
|
navigateTo('/library');
|
2023-09-02 01:11:27 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-31 23:47:18 +03:00
|
|
|
|
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
2023-07-15 17:46:19 +03:00
|
|
|
event.preventDefault();
|
2023-08-09 17:19:12 +03:00
|
|
|
if (processing) {
|
2023-07-25 20:27:29 +03:00
|
|
|
return;
|
2023-07-15 17:46:19 +03:00
|
|
|
}
|
2023-07-25 20:27:29 +03:00
|
|
|
const data: IRSFormCreateData = {
|
2023-08-25 22:51:20 +03:00
|
|
|
item_type: LibraryItemType.RSFORM,
|
2023-07-26 23:11:00 +03:00
|
|
|
title: title,
|
|
|
|
alias: alias,
|
|
|
|
comment: comment,
|
|
|
|
is_common: common,
|
2023-08-25 22:51:20 +03:00
|
|
|
is_canonical: false,
|
2023-07-26 23:11:00 +03:00
|
|
|
file: file,
|
|
|
|
fileName: file?.name
|
2023-07-25 20:27:29 +03:00
|
|
|
};
|
2023-08-09 17:19:12 +03:00
|
|
|
createSchema(data, (newSchema) => {
|
|
|
|
toast.success('Схема успешно создана');
|
2023-09-05 00:23:53 +03:00
|
|
|
navigateTo(`/rsforms/${newSchema.id}`);
|
2023-08-09 17:19:12 +03:00
|
|
|
});
|
2023-07-31 23:47:18 +03:00
|
|
|
}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-09-08 13:46:45 +03:00
|
|
|
function handleFileChange(event: React.ChangeEvent<HTMLInputElement>) {
|
|
|
|
if (event.target.files && event.target.files.length > 0) {
|
|
|
|
setFileName(event.target.files[0].name);
|
|
|
|
setFile(event.target.files[0]);
|
|
|
|
} else {
|
|
|
|
setFileName('');
|
|
|
|
setFile(undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
return (
|
2023-07-25 20:27:29 +03:00
|
|
|
<RequireAuth>
|
2023-07-31 23:47:18 +03:00
|
|
|
<Form title='Создание концептуальной схемы'
|
|
|
|
onSubmit={handleSubmit}
|
2023-08-16 18:32:37 +03:00
|
|
|
widthClass='max-w-lg w-full mt-4'
|
2023-07-31 23:47:18 +03:00
|
|
|
>
|
2023-09-08 13:46:45 +03:00
|
|
|
<div className='relative w-full'>
|
|
|
|
<div className='absolute top-[-2.4rem] right-[-1rem] flex'>
|
|
|
|
<input
|
|
|
|
type='file'
|
|
|
|
ref={inputRef}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
accept={EXTEOR_TRS_FILE}
|
|
|
|
onChange={handleFileChange}
|
|
|
|
/>
|
|
|
|
<MiniButton
|
|
|
|
tooltip='Загрузить из Экстеор'
|
|
|
|
icon={<UploadIcon size={5} color='text-primary'/>}
|
|
|
|
onClick={() => inputRef.current?.click()}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{ fileName && <Label text={`Загружен файл: ${fileName}`} />}
|
2023-07-31 23:47:18 +03:00
|
|
|
<TextInput id='title' label='Полное название' type='text'
|
|
|
|
required={!file}
|
|
|
|
placeholder={file && 'Загрузить из файла'}
|
|
|
|
value={title}
|
|
|
|
onChange={event => setTitle(event.target.value)}
|
|
|
|
/>
|
|
|
|
<TextInput id='alias' label='Сокращение' type='text'
|
2023-08-16 18:32:37 +03:00
|
|
|
singleRow
|
2023-07-31 23:47:18 +03:00
|
|
|
required={!file}
|
|
|
|
value={alias}
|
|
|
|
placeholder={file && 'Загрузить из файла'}
|
|
|
|
onChange={event => setAlias(event.target.value)}
|
|
|
|
/>
|
|
|
|
<TextArea id='comment' label='Комментарий'
|
|
|
|
value={comment}
|
|
|
|
placeholder={file && 'Загрузить из файла'}
|
|
|
|
onChange={event => setComment(event.target.value)}
|
|
|
|
/>
|
|
|
|
<Checkbox id='common' label='Общедоступная схема'
|
|
|
|
value={common}
|
2023-09-07 16:30:43 +03:00
|
|
|
setValue={value => setCommon(value ?? false)}
|
2023-07-31 23:47:18 +03:00
|
|
|
/>
|
2023-09-02 01:11:27 +03:00
|
|
|
<div className='flex items-center justify-center gap-4 py-2 mt-4'>
|
2023-07-31 23:47:18 +03:00
|
|
|
<SubmitButton
|
|
|
|
text='Создать схему'
|
2023-09-02 01:11:27 +03:00
|
|
|
loading={processing}
|
|
|
|
widthClass='min-w-[10rem]'
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
text='Отмена'
|
|
|
|
onClick={() => handleCancel()}
|
|
|
|
widthClass='min-w-[10rem]'
|
2023-07-31 23:47:18 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{ error && <BackendError error={error} />}
|
|
|
|
</Form>
|
2023-07-15 17:46:19 +03:00
|
|
|
</RequireAuth>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:03:37 +03:00
|
|
|
export default CreateRSFormPage;
|