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',
},
strokeWidth: 3,
format: percent => `${parseFloat(percent.toFixed(2))}%`,
},
onChange({ file, fileList }) {
if (file.status !== 'uploading') {
setSelectedFiles(fileList)
}
} }
const closeModal = () => {
setIsModalVisible(false)
} }
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}
name="DocumentsUploadForm"
onFinish={(values) => onFinish(values, form)}
onFinishFailed={onFinishFailed}
style={{width: '300px'}}
>
<Form.Item
name="fileInput"
rules={[{ required: true, message: 'Выберите файл' }]}
>
<Upload {...submitFileFormProps}>
<Button icon={<UploadOutlined />}>Загрузить файл</Button>
</Upload>
</Form.Item>
<Form.Item>
<Button <Button
type="primary" icon={<UploadOutlined />}
htmlType="submit" onClick={openModal}
disabled={selectedFiles.length === 0} >Загрузить файл</Button>
<Modal
title="Загрузить файл"
centered
visible={isModalVisible}
onCancel={closeModal}
width={500}
footer={null}
> >
Отправить <div>
</Button> <DocumentCreationForm
</Form.Item> selectedFileCategory={selectedFileCategory}
</Form> closeModalHandler = {() => {
setIsModalVisible(false)
setTableUpdating(true)
}}>
</DocumentCreationForm>
</div>
</Modal>
<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>
)
}