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 { Dispatch, SetStateAction } from "react"
import { notification } from 'antd'; import { notification } from 'antd';
import { FileInfoDto } from '../services/api'
const notificationTypeDictionary = new Map([ const notificationTypeDictionary = new Map([
['error', {notifyInstance: notification.error, caption: 'Ошибка'}], ['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) => { export const formatBytes = (bytes:number) => {
if(bytes < 1024) if(bytes < 1024)
return `${bytes.toFixed(0)}b` return `${bytes.toFixed(0)}b`

View File

@ -4,7 +4,7 @@ import moment from "moment"
import { FileService } from "../../services/api" import { FileService } from "../../services/api"
import { import {
invokeWebApiWrapperAsync, invokeWebApiWrapperAsync,
download, downloadFile,
formatBytes, formatBytes,
} from "../../components/factory" } from "../../components/factory"
import { EditableTable, makePaginationObject } from "../../components/Table" 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 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 = () => { const handleUploadComplete = () => {
update() update()
} }
@ -61,7 +51,7 @@ export default function DocumentsTemplate({ idCategory, idWell, accept, headerCh
key: "document", key: "document",
dataIndex: "name", dataIndex: "name",
render: (name, row) => ( render: (name, row) => (
<Button type="link" onClick={(ev) => handleFileDownload(ev, row)} download={name}> <Button type="link" onClick={() => downloadFile(row)} download={name}>
{name} {name}
</Button>), </Button>),
}, },

View File

@ -1,14 +1,5 @@
import { Progress, Button } from "antd" import { Progress, Button } from "antd"
import { download, notify } from "../../components/factory" import { downloadFile } 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)
}
}
export const ReportCreationNotify = ({progressData}) => { export const ReportCreationNotify = ({progressData}) => {
progressData = progressData ?? {progress: 0.0, operation: 'Создание отчета'} progressData = progressData ?? {progress: 0.0, operation: 'Создание отчета'}
@ -17,7 +8,7 @@ export const ReportCreationNotify = ({progressData}) => {
if (progressData.file) if (progressData.file)
downloadButton = <Button downloadButton = <Button
type="link" type="link"
onClick={_ => {getReportFile(progressData.file)}} onClick={_ => {downloadFile(progressData.file)}}
download={progressData.file.name}> download={progressData.file.name}>
{progressData.file.name} {progressData.file.name}
</Button> </Button>

View File

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