2021-07-27 13:39:20 +05:00
|
|
|
|
import {Table, DatePicker, Button, Modal, ConfigProvider} from 'antd'
|
2021-07-26 11:12:20 +05:00
|
|
|
|
import { UploadOutlined } from '@ant-design/icons'
|
2021-07-27 13:39:20 +05:00
|
|
|
|
import DocumentCreationForm from './modalWindows/DocumentCreationForm'
|
2021-07-26 11:12:20 +05:00
|
|
|
|
import { FileService } from '../services/api'
|
|
|
|
|
import {useState, useEffect} from "react"
|
2021-07-22 14:53:30 +05:00
|
|
|
|
import {useParams} from 'react-router-dom'
|
|
|
|
|
import notify from './notify'
|
|
|
|
|
import LoaderPortal from './LoaderPortal'
|
2021-07-26 11:12:20 +05:00
|
|
|
|
import locale from "antd/lib/locale/ru_RU"
|
2021-07-22 14:53:30 +05:00
|
|
|
|
import moment from 'moment'
|
|
|
|
|
|
2021-07-26 15:21:35 +05:00
|
|
|
|
const pageSize = 12
|
2021-07-22 14:53:30 +05:00
|
|
|
|
const {RangePicker} = DatePicker;
|
|
|
|
|
|
|
|
|
|
|
2021-07-26 11:12:20 +05:00
|
|
|
|
export default function Documents({selectedFileCategory}) {
|
2021-07-22 14:53:30 +05:00
|
|
|
|
let {id} = useParams()
|
|
|
|
|
|
|
|
|
|
const [page, setPage] = useState(1)
|
|
|
|
|
const [range, setRange] = useState([])
|
|
|
|
|
const [pagination, setPagination] = useState(null)
|
|
|
|
|
const [files, setFiles] = useState([])
|
2021-07-27 13:39:20 +05:00
|
|
|
|
const [isModalVisible, setIsModalVisible] = useState(false)
|
2021-07-26 15:21:35 +05:00
|
|
|
|
const [isTableUpdating, setTableUpdating] = useState(false)
|
2021-07-22 14:53:30 +05:00
|
|
|
|
|
|
|
|
|
const [loader, setLoader] = useState(false)
|
2021-07-26 15:21:35 +05:00
|
|
|
|
|
|
|
|
|
const handleFileNameCLick = async (event, row) => {
|
|
|
|
|
const element = event.target
|
|
|
|
|
|
|
|
|
|
if(!element.href.length) {
|
|
|
|
|
try {
|
|
|
|
|
setLoader(true)
|
|
|
|
|
|
|
|
|
|
await fetch(`/api/files/${id}/${row.id}`, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: 'Bearer ' + localStorage['token']
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(async (response) => {
|
|
|
|
|
const blob = await response.blob();
|
|
|
|
|
|
|
|
|
|
let reader = new FileReader();
|
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
|
reader.onload = function (e) {
|
|
|
|
|
element.href = e.target.result
|
|
|
|
|
element.click()
|
|
|
|
|
};
|
|
|
|
|
setLoader(false)
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
notify(`Не удалось скачать файл ${row}`, 'error')
|
|
|
|
|
console.log(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columns = [
|
|
|
|
|
{
|
|
|
|
|
title: 'Документ',
|
|
|
|
|
key: 'document',
|
|
|
|
|
dataIndex: 'name',
|
|
|
|
|
render: (name, row) =>
|
|
|
|
|
<a onClick={ev => handleFileNameCLick(ev, row)} download={name}>{name}</a>
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Дата загрузки',
|
|
|
|
|
key: 'uploadDate',
|
|
|
|
|
dataIndex: 'uploadDate',
|
|
|
|
|
render: (item) => moment.utc(item).local().format('DD MMM YYYY, HH:mm:ss')
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Ф.И.О.',
|
|
|
|
|
key: 'userName',
|
|
|
|
|
dataIndex: 'userName',
|
|
|
|
|
}
|
|
|
|
|
];
|
2021-07-22 14:53:30 +05:00
|
|
|
|
|
2021-07-27 13:39:20 +05:00
|
|
|
|
const openModal = () => {
|
|
|
|
|
setIsModalVisible(true)
|
2021-07-22 14:53:30 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 13:39:20 +05:00
|
|
|
|
const closeModal = () => {
|
|
|
|
|
setIsModalVisible(false)
|
2021-07-22 14:53:30 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 13:39:20 +05:00
|
|
|
|
const onChangeRange = (range) => {
|
|
|
|
|
setRange(range)
|
2021-07-22 14:53:30 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const GetDocuments = async () => {
|
|
|
|
|
setLoader(true)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
let begin = null
|
|
|
|
|
let end = null
|
|
|
|
|
if (range?.length > 1) {
|
|
|
|
|
begin = range[0].toISOString()
|
|
|
|
|
end = range[1].toISOString()
|
|
|
|
|
}
|
2021-07-26 15:21:35 +05:00
|
|
|
|
|
2021-07-26 11:12:20 +05:00
|
|
|
|
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),
|
|
|
|
|
})
|
2021-07-26 15:21:35 +05:00
|
|
|
|
|
|
|
|
|
setTableUpdating(false)
|
|
|
|
|
setLoader(false)
|
2021-07-26 11:12:20 +05:00
|
|
|
|
}
|
|
|
|
|
)
|
2021-07-22 14:53:30 +05:00
|
|
|
|
} catch (ex) {
|
|
|
|
|
notify(`Не удалось загрузить файлы по скважине "${id}"`, 'error')
|
|
|
|
|
console.log(ex)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
GetDocuments()
|
2021-07-27 13:39:20 +05:00
|
|
|
|
}, [id, range, page, selectedFileCategory, isTableUpdating])
|
2021-07-22 14:53:30 +05:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2021-07-26 15:21:35 +05:00
|
|
|
|
<div className='filter-group'>
|
|
|
|
|
<h3 className='filter-group-heading'>Фильтр документов:</h3>
|
|
|
|
|
<ConfigProvider locale={locale}>
|
|
|
|
|
<RangePicker
|
|
|
|
|
showTime
|
|
|
|
|
onChange={onChangeRange}
|
|
|
|
|
/>
|
|
|
|
|
</ConfigProvider>
|
|
|
|
|
</div>
|
|
|
|
|
<LoaderPortal show={loader}></LoaderPortal>
|
|
|
|
|
<div> </div>
|
2021-07-27 13:39:20 +05:00
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
icon={<UploadOutlined />}
|
|
|
|
|
onClick={openModal}
|
|
|
|
|
>Загрузить файл</Button>
|
|
|
|
|
<Modal
|
|
|
|
|
title="Загрузить файл"
|
|
|
|
|
centered
|
|
|
|
|
visible={isModalVisible}
|
|
|
|
|
onCancel={closeModal}
|
|
|
|
|
width={500}
|
|
|
|
|
footer={null}
|
2021-07-26 15:21:35 +05:00
|
|
|
|
>
|
2021-07-27 13:39:20 +05:00
|
|
|
|
<div>
|
|
|
|
|
<DocumentCreationForm
|
|
|
|
|
selectedFileCategory={selectedFileCategory}
|
|
|
|
|
closeModalHandler = {() => {
|
|
|
|
|
setIsModalVisible(false)
|
|
|
|
|
setTableUpdating(true)
|
|
|
|
|
}}>
|
|
|
|
|
</DocumentCreationForm>
|
|
|
|
|
</div>
|
|
|
|
|
</Modal>
|
2021-07-22 14:53:30 +05:00
|
|
|
|
<div> </div>
|
|
|
|
|
<Table
|
|
|
|
|
columns={columns}
|
|
|
|
|
dataSource={files}
|
|
|
|
|
size={'small'}
|
|
|
|
|
pagination={{
|
|
|
|
|
pageSize: pageSize,
|
|
|
|
|
showSizeChanger: false,
|
|
|
|
|
total: pagination?.total,
|
|
|
|
|
current: page,
|
|
|
|
|
onChange: (page) => setPage(page)
|
|
|
|
|
}}
|
|
|
|
|
rowKey={(record) => record.id}
|
|
|
|
|
/>
|
|
|
|
|
</div>);
|
|
|
|
|
}
|