ConceptPortal-public/rsconcept/frontend/src/pages/CreateRSFormPage.tsx

105 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-07-15 17:46:19 +03:00
import { useEffect, useState } from 'react';
2023-07-25 20:27:29 +03:00
import { useNavigate } from 'react-router-dom';
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';
import Checkbox from '../components/Common/Checkbox';
import FileInput from '../components/Common/FileInput';
2023-07-15 17:46:19 +03:00
import Form from '../components/Common/Form';
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-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-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-07-15 17:46:19 +03:00
const navigate = useNavigate();
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);
const [file, setFile] = useState<File | undefined>()
2023-07-31 23:47:18 +03:00
useEffect(() => {
setError(undefined);
}, [title, alias, setError]);
function handleFile(event: React.ChangeEvent<HTMLInputElement>) {
2023-07-15 17:46:19 +03:00
if (event.target.files && event.target.files.length > 0) {
setFile(event.target.files[0]);
} else {
2023-07-31 23:47:18 +03:00
setFile(undefined);
2023-07-15 17:46:19 +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,
title: title,
alias: alias,
comment: comment,
is_common: common,
2023-08-25 22:51:20 +03:00
is_canonical: false,
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('Схема успешно создана');
navigate(`/rsforms/${newSchema.id}`);
});
2023-07-31 23:47:18 +03:00
}
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}
widthClass='max-w-lg w-full mt-4'
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'
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}
onChange={event => setCommon(event.target.checked)}
/>
<FileInput id='trs' label='Загрузить *.trs'
acceptType='.trs'
onChange={handleFile}
/>
2023-07-25 20:27:29 +03:00
2023-07-31 23:47:18 +03:00
<div className='flex items-center justify-center py-2 mt-4'>
<SubmitButton
text='Создать схему'
2023-08-09 17:19:12 +03:00
loading={processing}
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;