Refactor extract downloadFileMethod into factory

This commit is contained in:
Фролов 2021-09-30 12:33:41 +05:00
parent f0e16032e0
commit 74bc512fa3
4 changed files with 16 additions and 35 deletions

View File

@ -1,5 +1,6 @@
import { Dispatch, SetStateAction } from "react"
import { notification } from 'antd';
import { FileInfoDto } from '../services/api'
const notificationTypeDictionary = new Map([
['error', {notifyInstance: notification.error, caption: 'Ошибка'}],
@ -89,6 +90,15 @@ export const upload = async (url:string, formData: FormData) => {
})
}
export const downloadFile = async (fileInfo: FileInfoDto) => {
try {
await download(`/api/well/${fileInfo.idWell}/files/${fileInfo.id}`)
} catch (error) {
notify(`Не удалось скачать файл ${fileInfo.name} по скважине (${fileInfo.idWell})`, 'error')
console.log(error)
}
}
export const formatBytes = (bytes:number) => {
if(bytes < 1024)
return `${bytes.toFixed(0)}b`

View File

@ -4,7 +4,7 @@ import moment from "moment"
import { FileService } from "../../services/api"
import {
invokeWebApiWrapperAsync,
download,
downloadFile,
formatBytes,
} from "../../components/factory"
import { EditableTable, makePaginationObject } from "../../components/Table"
@ -28,16 +28,6 @@ export default function DocumentsTemplate({ idCategory, idWell, accept, headerCh
const uploadUrl = `/api/well/${idWell}/files/?idCategory=${idCategory}`
const handleFileDownload = async (_, row) => {
invokeWebApiWrapperAsync(
async () => {
await download(`/api/well/${idWell}/files/${row.id}`)
},
setShowLoader,
`Не удалось скачать файл ${row}`
)
}
const handleUploadComplete = () => {
update()
}
@ -61,7 +51,7 @@ export default function DocumentsTemplate({ idCategory, idWell, accept, headerCh
key: "document",
dataIndex: "name",
render: (name, row) => (
<Button type="link" onClick={(ev) => handleFileDownload(ev, row)} download={name}>
<Button type="link" onClick={() => downloadFile(row)} download={name}>
{name}
</Button>),
},

View File

@ -1,14 +1,5 @@
import { Progress, Button } from "antd"
import { download, notify } from "../../components/factory"
export const getReportFile = async (fileInfo) => {
try {
await download(`/api/well/${fileInfo.idWell}/files/${fileInfo.id}`)
} catch (error) {
notify(`Не удалось скачать отчет ${fileInfo.name} по скважине (${fileInfo.idWell})`, 'error')
console.log(error)
}
}
import { downloadFile } from "../../components/factory"
export const ReportCreationNotify = ({progressData}) => {
progressData = progressData ?? {progress: 0.0, operation: 'Создание отчета'}
@ -17,7 +8,7 @@ export const ReportCreationNotify = ({progressData}) => {
if (progressData.file)
downloadButton = <Button
type="link"
onClick={_ => {getReportFile(progressData.file)}}
onClick={_ => {downloadFile(progressData.file)}}
download={progressData.file.name}>
{progressData.file.name}
</Button>

View File

@ -3,7 +3,7 @@ import { Table, makeDateSorter, makeNumericSorter, formatDate} from "../../compo
import { Button, Tooltip } from "antd"
import { FilePdfOutlined, FileTextOutlined} from '@ant-design/icons'
import { ReportService } from "../../services/api"
import { invokeWebApiWrapperAsync, download, formatTimespan} from "../../components/factory"
import { invokeWebApiWrapperAsync, downloadFile, formatTimespan} from "../../components/factory"
import LoaderPortal from "../../components/LoaderPortal"
import moment from "moment"
@ -12,16 +12,6 @@ const imgPaths = {
".las": <FileTextOutlined />,
}
const handleFileDownload = async (idWell, idFile) => {
invokeWebApiWrapperAsync(
async () => {
await download(`/api/well/${idWell}/files/${idFile}`)
},
null,
`Не удалось скачать файл ${idFile}`
)
}
const columns = [
{
title: "Название",
@ -31,7 +21,7 @@ const columns = [
<Button
type="link"
icon={imgPaths[report.format]}
onClick={(_) => handleFileDownload(report.idWell, report.id)}
onClick={(_) => downloadFile(report.file)}
download={name}
>
{name}