2021-12-20 12:44:07 +05:00
|
|
|
|
import moment from 'moment'
|
|
|
|
|
import { useState, useEffect } from 'react'
|
|
|
|
|
import { DatePicker, Button, Input } from 'antd'
|
2022-01-24 17:32:45 +05:00
|
|
|
|
|
|
|
|
|
import { FileService } from '@api'
|
2022-02-07 14:58:38 +05:00
|
|
|
|
import { hasPermission } from '@utils/permissions'
|
2022-01-24 17:32:45 +05:00
|
|
|
|
import LoaderPortal from '@components/LoaderPortal'
|
|
|
|
|
import { UploadForm } from '@components/UploadForm'
|
|
|
|
|
import { CompanyView, UserView } from '@components/views'
|
|
|
|
|
import { EditableTable, makePaginationObject } from '@components/Table'
|
|
|
|
|
import { invokeWebApiWrapperAsync, downloadFile, formatBytes } from '@components/factory'
|
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-12-20 12:44:07 +05:00
|
|
|
|
const columns = [
|
|
|
|
|
{
|
|
|
|
|
title: 'Документ',
|
|
|
|
|
key: 'document',
|
|
|
|
|
dataIndex: 'name',
|
|
|
|
|
render: (name, row) => (
|
|
|
|
|
<Button type={'link'} onClick={() => downloadFile(row)} download={name}>
|
|
|
|
|
{name}
|
|
|
|
|
</Button>
|
|
|
|
|
),
|
|
|
|
|
}, {
|
|
|
|
|
title: 'Дата загрузки',
|
|
|
|
|
key: 'uploadDate',
|
|
|
|
|
dataIndex: 'uploadDate',
|
|
|
|
|
render: item => moment.utc(item).local().format('DD MMM YYYY, HH:mm:ss'),
|
|
|
|
|
}, {
|
|
|
|
|
title: 'Размер',
|
|
|
|
|
key: 'size',
|
|
|
|
|
dataIndex: 'size',
|
|
|
|
|
render: item => formatBytes(item)
|
|
|
|
|
}, {
|
|
|
|
|
title: 'Автор',
|
|
|
|
|
key: 'author',
|
|
|
|
|
dataIndex: 'author',
|
|
|
|
|
render: item => <UserView user={item}/>
|
|
|
|
|
}, {
|
|
|
|
|
title: 'Компания',
|
|
|
|
|
key: 'company',
|
|
|
|
|
render: (_, record) => <CompanyView company={record?.author?.company}/>
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
2022-01-24 17:32:45 +05:00
|
|
|
|
export const DocumentsTemplate = ({ idCategory, idWell, accept, headerChild, customColumns, beforeTable, onChange}) => {
|
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([])
|
2021-08-31 18:04:04 +05:00
|
|
|
|
const [filterCompanyName, setFilterCompanyName] = useState([])
|
2021-08-17 17:01:19 +05:00
|
|
|
|
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-12-20 12:44:07 +05:00
|
|
|
|
const handleUploadComplete = () => update()
|
2021-08-17 16:46:46 +05:00
|
|
|
|
|
2021-08-20 15:28:13 +05:00
|
|
|
|
const handleFileDelete = async (file) => {
|
|
|
|
|
await FileService.delete(idWell, file.id)
|
2021-08-17 16:46:46 +05:00
|
|
|
|
update()
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 12:44:07 +05:00
|
|
|
|
const hanleCompanySearch = (value, _) => setFilterCompanyName(value)
|
|
|
|
|
const hanleFileNameSearch = (value, _) => setFilterFileName(value)
|
2021-08-17 16:46:46 +05:00
|
|
|
|
|
2021-12-20 12:44:07 +05:00
|
|
|
|
const mergedColumns = [...columns, ...(customColumns ?? [])]
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 17:42:42 +05:00
|
|
|
|
invokeWebApiWrapperAsync(
|
2021-08-17 16:46:46 +05:00
|
|
|
|
async () => {
|
2021-08-17 10:46:28 +05:00
|
|
|
|
const paginatedFiles = await FileService.getFilesInfo(
|
2021-12-20 12:44:07 +05:00
|
|
|
|
idWell,
|
2021-08-17 10:46:28 +05:00
|
|
|
|
idCategory,
|
2021-08-31 18:04:04 +05:00
|
|
|
|
filterCompanyName,
|
2021-08-31 12:30:44 +05:00
|
|
|
|
filterFileName,
|
2021-08-17 10:46:28 +05:00
|
|
|
|
begin,
|
2021-08-31 12:30:44 +05:00
|
|
|
|
end,
|
|
|
|
|
(page - 1) * pageSize,
|
|
|
|
|
pageSize,
|
2021-08-17 16:46:46 +05:00
|
|
|
|
)
|
2021-08-31 18:04:04 +05:00
|
|
|
|
|
2021-12-20 12:44:07 +05:00
|
|
|
|
if (!paginatedFiles) return
|
|
|
|
|
|
|
|
|
|
const filesInfos = paginatedFiles.items ?? []
|
2021-08-31 18:04:04 +05:00
|
|
|
|
setFiles(filesInfos)
|
2021-08-17 10:46:28 +05:00
|
|
|
|
const newPagination = makePaginationObject(paginatedFiles)
|
|
|
|
|
setPagination(newPagination)
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
2021-08-17 16:46:46 +05:00
|
|
|
|
`Не удалось загрузить файлы по скважине "${idWell}"`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 18:04:04 +05:00
|
|
|
|
useEffect(update, [idWell, idCategory, page, filterDataRange, filterCompanyName, filterFileName])
|
2021-12-20 12:44:07 +05:00
|
|
|
|
useEffect(() => onChange?.(files), [files, onChange])
|
2021-08-17 17:01:19 +05:00
|
|
|
|
|
2021-12-20 12:44:07 +05:00
|
|
|
|
const companies = [...new Set(files.map(file => file.company))].filter(company => company)
|
|
|
|
|
const filenames = [...new Set(files.map(file => file.name))].filter(name => name)
|
2021-08-17 16:46:46 +05:00
|
|
|
|
|
|
|
|
|
return (
|
2021-08-17 10:46:28 +05:00
|
|
|
|
<LoaderPortal show={showLoader}>
|
2021-12-20 12:44:07 +05:00
|
|
|
|
<div style={{ margin: 16, display: 'flex' }}>
|
2021-08-17 16:46:46 +05:00
|
|
|
|
<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>
|
2021-12-20 12:44:07 +05:00
|
|
|
|
</div>
|
2021-08-17 16:46:46 +05:00
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<span>Фильтр по компании</span>
|
|
|
|
|
<Search
|
2021-12-20 12:44:07 +05:00
|
|
|
|
list={'listCompanies'}
|
|
|
|
|
placeholder={'Фильтр по компании'}
|
|
|
|
|
onSearch={hanleCompanySearch}
|
|
|
|
|
/>
|
|
|
|
|
<datalist id={'listCompanies'}>
|
|
|
|
|
{companies.map((company) => (
|
|
|
|
|
<option key={company} value={company}>{company}</option>
|
|
|
|
|
))}
|
|
|
|
|
</datalist>
|
|
|
|
|
</div>
|
2021-08-17 17:01:19 +05:00
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<span>Фильтр по имени файла</span>
|
|
|
|
|
<Search
|
2021-12-20 12:44:07 +05:00
|
|
|
|
list={'listFileNames'}
|
|
|
|
|
placeholder={'Фильтр по имени файла'}
|
|
|
|
|
onSearch={hanleFileNameSearch}
|
|
|
|
|
/>
|
|
|
|
|
<datalist id={'listFileNames'}>
|
|
|
|
|
{filenames.map((name) => (
|
|
|
|
|
<option key={name} value={name}>{name}</option>
|
|
|
|
|
))}
|
|
|
|
|
</datalist>
|
2021-08-17 16:46:46 +05:00
|
|
|
|
</div>
|
|
|
|
|
|
2022-02-07 14:58:38 +05:00
|
|
|
|
{hasPermission(`File.edit${idCategory}`) && (
|
|
|
|
|
<div>
|
|
|
|
|
<span>Загрузка</span>
|
|
|
|
|
<UploadForm
|
|
|
|
|
url={uploadUrl}
|
|
|
|
|
accept={accept}
|
|
|
|
|
onUploadStart={() => setShowLoader(true)}
|
|
|
|
|
onUploadComplete={handleUploadComplete}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2021-09-01 13:43:14 +05:00
|
|
|
|
|
|
|
|
|
{headerChild}
|
2021-08-17 16:46:46 +05:00
|
|
|
|
</div>
|
2021-11-11 12:22:21 +05:00
|
|
|
|
{beforeTable}
|
2021-08-17 16:46:46 +05:00
|
|
|
|
<EditableTable
|
2021-12-20 12:44:07 +05:00
|
|
|
|
columns={mergedColumns}
|
2021-08-17 10:46:28 +05:00
|
|
|
|
dataSource={files}
|
2021-12-20 12:44:07 +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),
|
|
|
|
|
}}
|
2022-02-07 14:58:38 +05:00
|
|
|
|
onRowDelete={hasPermission(`File.edit${idCategory}`) && handleFileDelete}
|
2021-08-17 10:46:28 +05:00
|
|
|
|
rowKey={(record) => record.id}
|
|
|
|
|
/>
|
2021-08-17 16:46:46 +05:00
|
|
|
|
</LoaderPortal>
|
|
|
|
|
)
|
|
|
|
|
}
|
2022-01-24 17:32:45 +05:00
|
|
|
|
|
|
|
|
|
export default DocumentsTemplate
|