2021-08-17 16:46:46 +05:00
|
|
|
|
import { useState, useEffect } from "react"
|
|
|
|
|
import { DatePicker, Button, Input } from "antd"
|
|
|
|
|
import moment from "moment"
|
|
|
|
|
import { FileService } from "../../services/api"
|
|
|
|
|
import {
|
|
|
|
|
updateFromWebApiWrapperAsync,
|
|
|
|
|
download,
|
|
|
|
|
makePaginationObject,
|
|
|
|
|
} from "../../components/factory"
|
|
|
|
|
import { EditableTable } from "../../components/EditableTable"
|
|
|
|
|
import UploadForm from "../../components/UploadForm"
|
|
|
|
|
import LoaderPortal from "../../components/LoaderPortal"
|
2021-08-17 10:46:28 +05:00
|
|
|
|
|
|
|
|
|
const pageSize = 12
|
2021-08-17 16:46:46 +05:00
|
|
|
|
const { RangePicker } = DatePicker
|
|
|
|
|
const { Search } = Input
|
2021-08-17 10:46:28 +05:00
|
|
|
|
|
2021-08-17 16:46:46 +05:00
|
|
|
|
export default function DocumentsTemplate({ idCategory, idWell }) {
|
2021-08-17 10:46:28 +05:00
|
|
|
|
const [page, setPage] = useState(1)
|
2021-08-17 17:01:19 +05:00
|
|
|
|
const [filterDataRange, setFilterDataRange] = useState([])
|
|
|
|
|
const [filterCompany, setFilterCompany] = useState([])
|
|
|
|
|
const [filterFileName, setFilterFileName] = useState('')
|
2021-08-17 10:46:28 +05:00
|
|
|
|
const [pagination, setPagination] = useState(null)
|
|
|
|
|
const [files, setFiles] = useState([])
|
|
|
|
|
const [showLoader, setShowLoader] = useState(false)
|
|
|
|
|
|
2021-08-17 13:01:13 +05:00
|
|
|
|
const uploadUrl = `/api/well/${idWell}/files/?idCategory=${idCategory}`
|
2021-08-17 10:46:28 +05:00
|
|
|
|
|
2021-08-17 16:46:46 +05:00
|
|
|
|
const handleFileDownload = async (_, row) => {
|
|
|
|
|
updateFromWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
|
|
|
|
await download(`/api/well/${idWell}/files/${row.id}`)
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
|
|
|
|
`Не удалось скачать файл ${row}`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleUploadComplete = () => {
|
|
|
|
|
update()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleFileDelete = (file) => {
|
|
|
|
|
//FileService.delete(idWell, file.id)
|
|
|
|
|
update()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hanleCompanySearch = (value, _) => {
|
2021-08-17 17:01:19 +05:00
|
|
|
|
setFilterCompany(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hanleFileNameSearch = (value, _) => {
|
|
|
|
|
setFilterFileName(value)
|
2021-08-17 10:46:28 +05:00
|
|
|
|
}
|
2021-08-17 16:46:46 +05:00
|
|
|
|
|
2021-08-17 10:46:28 +05:00
|
|
|
|
const columns = [
|
|
|
|
|
{
|
2021-08-17 16:46:46 +05:00
|
|
|
|
title: "Документ",
|
|
|
|
|
key: "document",
|
|
|
|
|
dataIndex: "name",
|
|
|
|
|
render: (name, row) => (
|
|
|
|
|
<Button type="link" onClick={(ev) => handleFileDownload(ev, row)} download={name}>
|
|
|
|
|
{name}
|
|
|
|
|
</Button>),
|
2021-08-17 10:46:28 +05:00
|
|
|
|
},
|
|
|
|
|
{
|
2021-08-17 16:46:46 +05:00
|
|
|
|
title: "Дата загрузки",
|
|
|
|
|
key: "uploadDate",
|
|
|
|
|
dataIndex: "uploadDate",
|
|
|
|
|
render: (item) =>
|
|
|
|
|
moment.utc(item).local().format("DD MMM YYYY, HH:mm:ss"),
|
2021-08-17 10:46:28 +05:00
|
|
|
|
},
|
|
|
|
|
{
|
2021-08-17 16:46:46 +05:00
|
|
|
|
title: "Ф.И.О.",
|
|
|
|
|
key: "userName",
|
|
|
|
|
dataIndex: "userName",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: "Компания",
|
|
|
|
|
key: "company",
|
|
|
|
|
dataIndex: "company",
|
|
|
|
|
},
|
|
|
|
|
]
|
2021-08-17 10:46:28 +05:00
|
|
|
|
|
2021-08-17 16:46:46 +05:00
|
|
|
|
const addKeysAndUpdateFiles = (items) => {
|
|
|
|
|
const mappedFiles = items?.map((fileInfo) => ({
|
|
|
|
|
key: fileInfo.id,
|
|
|
|
|
begin: fileInfo.date,
|
|
|
|
|
...fileInfo,
|
|
|
|
|
}))
|
|
|
|
|
setFiles(mappedFiles ?? [])
|
2021-08-17 10:46:28 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:46:46 +05:00
|
|
|
|
const update = () => {
|
2021-08-17 10:46:28 +05:00
|
|
|
|
let begin = null
|
|
|
|
|
let end = null
|
2021-08-17 17:01:19 +05:00
|
|
|
|
if (filterDataRange?.length > 1) {
|
|
|
|
|
begin = filterDataRange[0].toISOString()
|
|
|
|
|
end = filterDataRange[1].toISOString()
|
2021-08-17 10:46:28 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateFromWebApiWrapperAsync(
|
2021-08-17 16:46:46 +05:00
|
|
|
|
async () => {
|
2021-08-17 10:46:28 +05:00
|
|
|
|
const paginatedFiles = await FileService.getFilesInfo(
|
2021-08-17 16:46:46 +05:00
|
|
|
|
idWell,
|
2021-08-17 10:46:28 +05:00
|
|
|
|
(page - 1) * pageSize,
|
|
|
|
|
pageSize,
|
|
|
|
|
idCategory,
|
|
|
|
|
begin,
|
2021-08-17 16:46:46 +05:00
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
if (!paginatedFiles) return
|
2021-08-17 10:46:28 +05:00
|
|
|
|
addKeysAndUpdateFiles(paginatedFiles?.items)
|
|
|
|
|
|
|
|
|
|
const newPagination = makePaginationObject(paginatedFiles)
|
|
|
|
|
setPagination(newPagination)
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
2021-08-17 16:46:46 +05:00
|
|
|
|
`Не удалось загрузить файлы по скважине "${idWell}"`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 17:01:19 +05:00
|
|
|
|
useEffect(update, [idWell, idCategory, page, filterDataRange, filterCompany, filterFileName])
|
2021-08-17 16:46:46 +05:00
|
|
|
|
|
|
|
|
|
const companies = [...new Set(files.map(file=>file.company))]
|
|
|
|
|
.filter(company=>company)
|
2021-08-17 10:46:28 +05:00
|
|
|
|
|
2021-08-17 16:46:46 +05:00
|
|
|
|
const DataListCompanies = <datalist id="listCompanies">
|
2021-08-17 17:01:19 +05:00
|
|
|
|
{companies.map((company)=><option key={company} value={company}>{company}</option>)}
|
|
|
|
|
</datalist>
|
|
|
|
|
|
|
|
|
|
const filenames = [...new Set(files.map(file=>file.name))]
|
|
|
|
|
.filter(name=>name)
|
|
|
|
|
|
|
|
|
|
const DataListFileNames = <datalist id="listFileNames">
|
|
|
|
|
{filenames.map((name)=><option key={name} value={name}>{name}</option>)}
|
2021-08-17 16:46:46 +05:00
|
|
|
|
</datalist>
|
|
|
|
|
|
|
|
|
|
return (
|
2021-08-17 10:46:28 +05:00
|
|
|
|
<LoaderPortal show={showLoader}>
|
2021-08-17 16:46:46 +05:00
|
|
|
|
<div style={{margin:16, display:'flex'}}>
|
|
|
|
|
<div>
|
|
|
|
|
<span>Фильтр по дате</span>
|
|
|
|
|
<div>
|
2021-08-17 17:01:19 +05:00
|
|
|
|
<RangePicker showTime onChange={setFilterDataRange} />
|
2021-08-17 16:46:46 +05:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<span>Фильтр по компании</span>
|
|
|
|
|
<Search
|
|
|
|
|
list='listCompanies'
|
|
|
|
|
placeholder="Фильтр по компании"
|
|
|
|
|
onSearch={hanleCompanySearch}/>
|
|
|
|
|
{DataListCompanies}
|
2021-08-17 17:01:19 +05:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<span>Фильтр по имени файла</span>
|
|
|
|
|
<Search
|
|
|
|
|
list='listFileNames'
|
|
|
|
|
placeholder="Фильтр по имени файла"
|
|
|
|
|
onSearch={hanleFileNameSearch}/>
|
|
|
|
|
{DataListFileNames}
|
2021-08-17 16:46:46 +05:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<span>Загрузка</span>
|
|
|
|
|
<UploadForm
|
|
|
|
|
url={uploadUrl}
|
|
|
|
|
onUploadStart={() => setShowLoader(true)}
|
|
|
|
|
onUploadComplete={handleUploadComplete}/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<EditableTable
|
2021-08-17 10:46:28 +05:00
|
|
|
|
columns={columns}
|
|
|
|
|
dataSource={files}
|
2021-08-17 16:46:46 +05:00
|
|
|
|
size={"small"}
|
2021-08-17 10:46:28 +05:00
|
|
|
|
pagination={{
|
|
|
|
|
...pagination,
|
|
|
|
|
showSizeChanger: false,
|
2021-08-17 16:46:46 +05:00
|
|
|
|
onChange: (page) => setPage(page),
|
|
|
|
|
}}
|
|
|
|
|
onRowDelete={handleFileDelete}
|
2021-08-17 10:46:28 +05:00
|
|
|
|
rowKey={(record) => record.id}
|
|
|
|
|
/>
|
2021-08-17 16:46:46 +05:00
|
|
|
|
</LoaderPortal>
|
|
|
|
|
)
|
|
|
|
|
}
|