diff --git a/src/components/Documents.jsx b/src/components/Documents.jsx index db405b0..c758f90 100644 --- a/src/components/Documents.jsx +++ b/src/components/Documents.jsx @@ -1,11 +1,12 @@ -import {Table, DatePicker, Form, Button, Upload, ConfigProvider} from 'antd'; -import { UploadOutlined } from '@ant-design/icons'; -import MenuDocuments from "./MenuDocuments"; -import {useState, useEffect} from "react"; +import {Table, DatePicker, Form, Button, Upload, ConfigProvider} from 'antd' +import { UploadOutlined } from '@ant-design/icons' +import MenuDocuments from "./MenuDocuments" +import { FileService } from '../services/api' +import {useState, useEffect} from "react" import {useParams} from 'react-router-dom' import notify from './notify' import LoaderPortal from './LoaderPortal' -import locale from "antd/lib/locale/ru_RU"; +import locale from "antd/lib/locale/ru_RU" import moment from 'moment' const pageSize = 26 @@ -25,26 +26,26 @@ const columns = [ }, { title: 'Ф.И.О.', - key: 'name', - dataIndex: 'name', + key: 'userName', + dataIndex: 'userName', } ]; -export default function Documents() { +export default function Documents({selectedFileCategory}) { let {id} = useParams() const [page, setPage] = useState(1) const [range, setRange] = useState([]) const [pagination, setPagination] = useState(null) const [files, setFiles] = useState([]) + const [selectedFiles, setSelectedFiles] = useState([]) const [loader, setLoader] = useState(false) const submitFileFormProps = { - //action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76', onChange({ file, fileList }) { if (file.status !== 'uploading') { - console.log(file, fileList); + setSelectedFiles(fileList) } } } @@ -54,15 +55,33 @@ export default function Documents() { } const onFinish = (values) => { - console.log('Success:', values); + var fileList = values.fileInput.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 + }) + .then(async (response) => { + // refresh component to upload new files в зависимоть useEffect какую-то переменную, чтоб дергался снова запрос на всю инфу о файлах + }); + } } const onFinishFailed = (errorInfo) => { - console.log('Failed:', errorInfo); + notify(`Не удалось отправить файлы по скважине "${id}".`, 'error') } - const fileList = [] - useEffect(() => { const GetDocuments = async () => { setLoader(true) @@ -74,26 +93,29 @@ export default function Documents() { begin = range[0].toISOString() end = range[1].toISOString() } - // await AnalyticsService.getOperationsByWell( - // `${id}`, - // (page-1) * pageSize, - // pageSize, - // begin, - // end).then((paginatedFiles) => { - // setFiles(paginatedFiles?.items.map(f => { - // return { - // key: f.id, - // begin: f.date, - // ...f - // } - // })) + console.log(id) + await FileService.getFilesInfo( + `${id}`, + (page - 1) * pageSize, + pageSize, + selectedFileCategory, + begin, + end + ).then((paginatedFiles) => { + setFiles(paginatedFiles?.items.map(f => { + return { + key: f.id, + begin: f.date, + ...f + } + })) - // setPagination({ - // total: paginatedFiles?.count, - // current: Math.floor(paginatedFiles?.skip / pageSize), - // }) - // } - //) + setPagination({ + total: paginatedFiles?.count, + current: Math.floor(paginatedFiles?.skip / pageSize), + }) + } + ) } catch (ex) { notify(`Не удалось загрузить файлы по скважине "${id}"`, 'error') console.log(ex) @@ -101,7 +123,7 @@ export default function Documents() { setLoader(false) } GetDocuments() - }, [id, range]) + }, [id, selectedFileCategory, range]) return (
@@ -127,15 +149,6 @@ export default function Documents() { name="fileInput" rules={[{ required: true, message: 'Выберите файл' }]} > - {/* { - setFile(e.target.value) - files.push(e.target.value) - localStorage.setItem(e.target.value, file) - setFiles(files) - console.log(e.target.value) - } - }/> */} @@ -144,7 +157,7 @@ export default function Documents() {