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

143 lines
4.5 KiB
TypeScript
Raw Normal View History

'use client';
import { useEffect, useRef, useState } from 'react';
2023-07-25 20:27:29 +03:00
import { toast } from 'react-toastify';
2023-07-15 17:46:19 +03:00
import Button from '@/components/Common/Button';
import Checkbox from '@/components/Common/Checkbox';
import Label from '@/components/Common/Label';
import MiniButton from '@/components/Common/MiniButton';
import Overlay from '@/components/Common/Overlay';
import SubmitButton from '@/components/Common/SubmitButton';
import TextArea from '@/components/Common/TextArea';
import TextInput from '@/components/Common/TextInput';
import { DownloadIcon } from '@/components/Icons';
import InfoError from '@/components/InfoError';
import RequireAuth from '@/components/RequireAuth';
import { useLibrary } from '@/context/LibraryContext';
import { useConceptNavigation } from '@/context/NagivationContext';
import { LibraryItemType } from '@/models/library';
import { IRSFormCreateData } from '@/models/rsform';
2023-12-13 15:24:10 +03:00
import { EXTEOR_TRS_FILE, limits, patterns } from '@/utils/constants';
2023-07-15 17:46:19 +03:00
2023-07-28 00:03:37 +03:00
function CreateRSFormPage() {
const router = useConceptNavigation();
2023-11-01 13:41:32 +03:00
const { createItem, 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 [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() {
if (router.canBack()) {
router.back();
2023-09-02 01:11:27 +03:00
} else {
router.push('/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,
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-11-01 13:41:32 +03:00
createItem(data, (newSchema) => {
2023-08-09 17:19:12 +03:00
toast.success('Схема успешно создана');
router.push(`/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
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 (
<RequireAuth>
2023-12-08 00:33:47 +03:00
<form
className='flex flex-col w-full max-w-lg gap-3 px-6 py-3'
onSubmit={handleSubmit}
>
2023-12-08 00:33:47 +03:00
<h1>Создание концептуальной схемы</h1>
<Overlay position='top-[-2.4rem] right-[-1rem]'>
<input ref={inputRef} type='file'
style={{ display: 'none' }}
accept={EXTEOR_TRS_FILE}
onChange={handleFileChange}
/>
<MiniButton
tooltip='Загрузить из Экстеор'
2023-10-18 19:16:47 +03:00
icon={<DownloadIcon size={5} color='text-primary'/>}
onClick={() => inputRef.current?.click()}
/>
</Overlay>
{fileName ? <Label text={`Загружен файл: ${fileName}`} /> : null}
2023-12-13 15:24:10 +03:00
<TextInput required={!file}
label='Полное название'
placeholder={file && 'Загрузить из файла'}
value={title}
onChange={event => setTitle(event.target.value)}
/>
2023-12-13 15:24:10 +03:00
<TextInput required={!file}
label='Сокращение'
placeholder={file && 'Загрузить из файла'}
2023-12-13 15:24:10 +03:00
dimensions='w-[14rem]'
pattern={patterns.alias}
tooltip={`не более ${limits.alias_len} символов`}
value={alias}
onChange={event => setAlias(event.target.value)}
/>
<TextArea
label='Комментарий'
placeholder={file && 'Загрузить из файла'}
value={comment}
onChange={event => setComment(event.target.value)}
/>
<Checkbox
label='Общедоступная схема'
value={common}
setValue={value => setCommon(value ?? false)}
/>
2023-12-08 00:33:47 +03:00
<div className='flex items-center justify-around py-2'>
<SubmitButton
text='Создать схему'
loading={processing}
dimensions='min-w-[10rem]'
2023-07-31 23:47:18 +03:00
/>
<Button
text='Отмена'
dimensions='min-w-[10rem]'
onClick={() => handleCancel()}
2023-07-31 23:47:18 +03:00
/>
</div>
{error ? <InfoError error={error} /> : null}
2023-12-08 00:33:47 +03:00
</form>
</RequireAuth>);
2023-07-15 17:46:19 +03:00
}
export default CreateRSFormPage;