CF2-38: Форма отправки документа вынесена в модальное окно

This commit is contained in:
KharchenkoVV 2021-07-27 13:39:20 +05:00
parent c8b89ba75a
commit b8f8c8be99
2 changed files with 140 additions and 73 deletions

View File

@ -1,6 +1,7 @@
import {Table, DatePicker, Form, Button, Upload, ConfigProvider} from 'antd' import {Table, DatePicker, Button, Modal, ConfigProvider} from 'antd'
import { UploadOutlined } from '@ant-design/icons' import { UploadOutlined } from '@ant-design/icons'
import MenuDocuments from "./MenuDocuments" import MenuDocuments from "./MenuDocuments"
import DocumentCreationForm from './modalWindows/DocumentCreationForm'
import { FileService } from '../services/api' import { FileService } from '../services/api'
import {useState, useEffect} from "react" import {useState, useEffect} from "react"
import {useParams} from 'react-router-dom' import {useParams} from 'react-router-dom'
@ -20,11 +21,10 @@ export default function Documents({selectedFileCategory}) {
const [range, setRange] = useState([]) const [range, setRange] = useState([])
const [pagination, setPagination] = useState(null) const [pagination, setPagination] = useState(null)
const [files, setFiles] = useState([]) const [files, setFiles] = useState([])
const [selectedFiles, setSelectedFiles] = useState([]) const [isModalVisible, setIsModalVisible] = useState(false)
const [isTableUpdating, setTableUpdating] = useState(false) const [isTableUpdating, setTableUpdating] = useState(false)
const [loader, setLoader] = useState(false) const [loader, setLoader] = useState(false)
const [form] = Form.useForm();
const handleFileNameCLick = async (event, row) => { const handleFileNameCLick = async (event, row) => {
const element = event.target const element = event.target
@ -77,57 +77,18 @@ export default function Documents({selectedFileCategory}) {
} }
]; ];
const submitFileFormProps = { const openModal = () => {
progress: { setIsModalVisible(true)
strokeColor: { }
'0%': '#108ee9',
'100%': '#87d068', const closeModal = () => {
}, setIsModalVisible(false)
strokeWidth: 3,
format: percent => `${parseFloat(percent.toFixed(2))}%`,
},
onChange({ file, fileList }) {
if (file.status !== 'uploading') {
setSelectedFiles(fileList)
}
}
} }
const onChangeRange = (range) => { const onChangeRange = (range) => {
setRange(range) setRange(range)
} }
const onFinish = (values, form) => {
var fileList = values.fileInput.fileList
if (fileList.length > 0)
{
setLoader(true)
const formData = new FormData();
fileList.forEach(val => {
formData.append('files', val.originFileObj)
})
fetch(`/api/files/${id}/files?idCategory=${selectedFileCategory}&idUser=${localStorage['userId']}`, {
headers: {
Authorization: 'Bearer ' + localStorage['token']
},
method: 'POST',
body: formData
})
.then(async (response) => {
setLoader(false)
form.resetFields()
setTableUpdating(true)
});
}
}
const onFinishFailed = (errorInfo) => {
notify(`Не удалось отправить файлы по скважине "${id}".`, 'error')
}
useEffect(() => { useEffect(() => {
const GetDocuments = async () => { const GetDocuments = async () => {
setLoader(true) setLoader(true)
@ -171,7 +132,7 @@ export default function Documents({selectedFileCategory}) {
} }
} }
GetDocuments() GetDocuments()
}, [id, range,page, selectedFileCategory, isTableUpdating]) }, [id, range, page, selectedFileCategory, isTableUpdating])
return ( return (
<div> <div>
@ -187,31 +148,29 @@ export default function Documents({selectedFileCategory}) {
</div> </div>
<LoaderPortal show={loader}></LoaderPortal> <LoaderPortal show={loader}></LoaderPortal>
<div>&nbsp;</div> <div>&nbsp;</div>
<Form
form={form} <Button
name="DocumentsUploadForm" icon={<UploadOutlined />}
onFinish={(values) => onFinish(values, form)} onClick={openModal}
onFinishFailed={onFinishFailed} >Загрузить файл</Button>
style={{width: '300px'}} <Modal
title="Загрузить файл"
centered
visible={isModalVisible}
onCancel={closeModal}
width={500}
footer={null}
> >
<Form.Item <div>
name="fileInput" <DocumentCreationForm
rules={[{ required: true, message: 'Выберите файл' }]} selectedFileCategory={selectedFileCategory}
> closeModalHandler = {() => {
<Upload {...submitFileFormProps}> setIsModalVisible(false)
<Button icon={<UploadOutlined />}>Загрузить файл</Button> setTableUpdating(true)
</Upload> }}>
</Form.Item> </DocumentCreationForm>
<Form.Item> </div>
<Button </Modal>
type="primary"
htmlType="submit"
disabled={selectedFiles.length === 0}
>
Отправить
</Button>
</Form.Item>
</Form>
<div>&nbsp;</div> <div>&nbsp;</div>
<Table <Table
columns={columns} columns={columns}

View File

@ -0,0 +1,108 @@
import { Form, Upload, Button } from 'antd'
import { InboxOutlined } from '@ant-design/icons'
import {useState} from "react"
import {useParams} from 'react-router-dom'
const { Dragger } = Upload;
export default function DocumentCreationForm({selectedFileCategory, closeModalHandler}){
let {id} = useParams()
const [form] = Form.useForm();
const [selectedFiles, setSelectedFiles] = useState([])
const onFinish = (form) => {
form
.validateFields()
.then(values => {
var fileList = values.documentFile.fileList
if (fileList.length > 0) {
const formData = new FormData();
fileList.forEach(val => {
formData.append('files', val.originFileObj)
})
fetch(`/api/files/${id}/files?idCategory=${selectedFileCategory}&idUser=${localStorage['userId']}`, {
headers: {
Authorization: 'Bearer ' + localStorage['token']
},
method: 'POST',
body: formData
})
form.resetFields();
closeModalHandler()
}
}).catch(info => {
console.log('Validate Failed:', info);
});
}
const submitFileFormProps = {
progress: {
strokeColor: {
'0%': '#108ee9',
'100%': '#87d068',
},
strokeWidth: 3,
format: percent => `${parseFloat(percent.toFixed(2))}%`,
},
multiple: true,
onChange({ file, fileList }) {
if (file.status !== 'uploading') {
setSelectedFiles(fileList)
}
}
}
return (
<Form
form={form}
layout="vertical"
name="ImprovementCreationForm"
onFinish={(values) => onFinish(form)}
>
<Form.Item
name="documentFile"
rules={[{ required: true, message: 'Выберите файл' }]}
>
<Dragger
name="documentFileInput"
{...submitFileFormProps}
listType="picture"
>
<p className="ant-upload-drag-icon">
<InboxOutlined />
</p>
<p className="ant-upload-text">Кликните или перенесите сюда файлы</p>
<p className="ant-upload-hint">
Возможна загрузка как одного файла, так и нескольких.
</p>
</Dragger>
</Form.Item>
<Form.Item style={{marginBottom: 0}}>
<div style={{display: 'flex', justifyContent: 'flex-end'}}>
<Button
key="cancel"
onClick={closeModalHandler}>
Отмена
</Button>
<Button
key="submit"
type="primary"
htmlType="submit"
disabled={selectedFiles.length === 0}
style={{marginLeft: '10px'}}
>
Отправить
</Button>
</div>
</Form.Item>
</Form>
)
}