2021-08-13 14:46:22 +05:00
|
|
|
|
import { Dispatch, SetStateAction } from "react"
|
2021-08-20 10:49:20 +05:00
|
|
|
|
import { notification } from 'antd';
|
2021-09-30 12:33:41 +05:00
|
|
|
|
import { FileInfoDto } from '../services/api'
|
2021-08-20 10:49:20 +05:00
|
|
|
|
|
|
|
|
|
const notificationTypeDictionary = new Map([
|
|
|
|
|
['error', {notifyInstance: notification.error, caption: 'Ошибка'}],
|
|
|
|
|
['warning', {notifyInstance: notification.warning, caption: 'Предупреждение'}],
|
|
|
|
|
['info', {notifyInstance: notification.info, caption: 'Инфо'}],
|
|
|
|
|
['open', {notifyInstance: notification.info, caption: ''}],
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Вызов оповещений всплывающим окошком.
|
|
|
|
|
* @param body string или ReactNode
|
|
|
|
|
* @param notifyType для параметра типа. Допустимые значение 'error', 'warning', 'info'
|
2021-07-30 16:13:26 +05:00
|
|
|
|
*/
|
2021-08-20 10:49:20 +05:00
|
|
|
|
export const notify = (body: string|any, notifyType:string ='info', other?: any) => {
|
|
|
|
|
if(!body)
|
|
|
|
|
return
|
|
|
|
|
const instance = notificationTypeDictionary.get(notifyType) ??
|
|
|
|
|
notificationTypeDictionary.get('open')
|
|
|
|
|
|
|
|
|
|
instance?.notifyInstance({
|
|
|
|
|
description: body,
|
|
|
|
|
message: instance.caption,
|
|
|
|
|
placement: "bottomRight",
|
|
|
|
|
duration: 10,
|
|
|
|
|
...other
|
|
|
|
|
})
|
2021-08-13 14:46:22 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type asyncFunction = (...args:any) => Promise<any|void>;
|
|
|
|
|
|
2021-08-17 17:42:42 +05:00
|
|
|
|
export const invokeWebApiWrapperAsync = async (funcAsync: asyncFunction, setShowLoader: Dispatch<SetStateAction<boolean>>, errorNotifyText: string) => {
|
2021-08-13 14:46:22 +05:00
|
|
|
|
if(setShowLoader)
|
|
|
|
|
setShowLoader(true)
|
|
|
|
|
try{
|
|
|
|
|
await funcAsync()
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
if(process.env.NODE_ENV === 'development')
|
2021-09-01 10:31:56 +05:00
|
|
|
|
console.error(ex)
|
2021-08-13 14:46:22 +05:00
|
|
|
|
if(errorNotifyText)
|
|
|
|
|
notify(errorNotifyText, 'error')
|
|
|
|
|
} finally{
|
|
|
|
|
if(setShowLoader)
|
|
|
|
|
setShowLoader(false)
|
|
|
|
|
}
|
2021-08-17 10:46:10 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const download = async (url:string, fileName?:string) => {
|
|
|
|
|
const response = await fetch(url, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: 'Bearer ' + localStorage['token']
|
|
|
|
|
},
|
|
|
|
|
method: 'Get'
|
|
|
|
|
})
|
2021-09-01 10:31:56 +05:00
|
|
|
|
if(response.status !== 200)
|
|
|
|
|
throw response
|
2021-08-17 10:46:10 +05:00
|
|
|
|
const requestFileName = decodeURI(fileName
|
|
|
|
|
??response.headers
|
|
|
|
|
.get('content-disposition')
|
|
|
|
|
?.split(';')
|
|
|
|
|
.splice(-1)[0]
|
|
|
|
|
.split("'")
|
|
|
|
|
.splice(-1)[0]
|
|
|
|
|
?? url.replace('\\','/')
|
|
|
|
|
.split('/')
|
|
|
|
|
.splice(-1)[0]
|
|
|
|
|
?? 'file')
|
|
|
|
|
const blob = await response.blob()
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
|
reader.onload = function (e) {
|
|
|
|
|
let a = document.createElement('a');
|
|
|
|
|
a.href = (e.target?.result?.toString() ?? '');
|
|
|
|
|
a.download = requestFileName;
|
|
|
|
|
document.body.appendChild(a); // we need to append the element to the dom -> otherwise it will not work in firefox
|
|
|
|
|
a.click();
|
|
|
|
|
a.remove();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const upload = async (url:string, formData: FormData) => {
|
|
|
|
|
await fetch(url, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: 'Bearer ' + localStorage['token']
|
|
|
|
|
},
|
|
|
|
|
method: 'Post',
|
|
|
|
|
body: formData,
|
|
|
|
|
})
|
2021-08-31 12:30:44 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 12:33:41 +05:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:30:44 +05:00
|
|
|
|
export const formatBytes = (bytes:number) => {
|
|
|
|
|
if(bytes < 1024)
|
|
|
|
|
return `${bytes.toFixed(0)}b`
|
|
|
|
|
if(bytes < 1024*1024)
|
|
|
|
|
return `${(bytes/1024).toFixed(2)}kb`
|
|
|
|
|
else
|
|
|
|
|
return `${(bytes/1024/1024).toFixed(2)}Mb`
|
2021-09-23 14:18:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const formatTimespan = (seconds:number) => {
|
|
|
|
|
const days = Math.floor(seconds / 86400)
|
|
|
|
|
seconds = seconds % 86400
|
|
|
|
|
const hours = Math.floor(seconds / 3600)
|
|
|
|
|
seconds = seconds % 3600
|
|
|
|
|
const minutes = Math.floor(seconds / 60)
|
|
|
|
|
seconds = seconds % 60
|
|
|
|
|
let formatedTimespan = ''
|
|
|
|
|
if(days > 0)
|
|
|
|
|
formatedTimespan += days + ' '
|
|
|
|
|
|
|
|
|
|
formatedTimespan += hours.toString().padStart(2,'0') + ':' +
|
|
|
|
|
minutes.toString().padStart(2,'0') + ':' +
|
|
|
|
|
seconds.toString().padStart(2,'0')
|
|
|
|
|
return formatedTimespan
|
2021-08-13 14:46:22 +05:00
|
|
|
|
}
|