2021-12-27 15:18:20 +05:00
|
|
|
|
import { notification } from 'antd'
|
2022-01-24 17:32:45 +05:00
|
|
|
|
import { Dispatch, ReactNode, SetStateAction } from 'react'
|
|
|
|
|
|
2022-02-07 17:44:46 +05:00
|
|
|
|
import { ApiError, FileInfoDto } from '@api'
|
2022-01-24 17:32:45 +05:00
|
|
|
|
import { getUserToken } from '@utils/storage'
|
2021-08-20 10:49:20 +05:00
|
|
|
|
|
|
|
|
|
const notificationTypeDictionary = new Map([
|
2021-12-27 15:18:20 +05:00
|
|
|
|
['error' , { notifyInstance: notification.error , caption: 'Ошибка' }],
|
|
|
|
|
['warning', { notifyInstance: notification.warning, caption: 'Предупреждение' }],
|
|
|
|
|
['info' , { notifyInstance: notification.info , caption: 'Инфо' }],
|
|
|
|
|
['open' , { notifyInstance: notification.info , caption: '' }],
|
2021-08-20 10:49:20 +05:00
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Вызов оповещений всплывающим окошком.
|
|
|
|
|
* @param body string или ReactNode
|
|
|
|
|
* @param notifyType для параметра типа. Допустимые значение 'error', 'warning', 'info'
|
2021-07-30 16:13:26 +05:00
|
|
|
|
*/
|
2022-01-24 17:32:45 +05:00
|
|
|
|
export const notify = (body: ReactNode, notifyType: string = 'info', other?: any) => {
|
2021-12-27 15:18:20 +05:00
|
|
|
|
if (!body) return
|
|
|
|
|
|
|
|
|
|
const instance = notificationTypeDictionary.get(notifyType) ??
|
2021-08-20 10:49:20 +05:00
|
|
|
|
notificationTypeDictionary.get('open')
|
|
|
|
|
|
|
|
|
|
instance?.notifyInstance({
|
|
|
|
|
description: body,
|
|
|
|
|
message: instance.caption,
|
2021-12-27 15:18:20 +05:00
|
|
|
|
placement: 'bottomRight',
|
2021-08-20 10:49:20 +05:00
|
|
|
|
duration: 10,
|
|
|
|
|
...other
|
|
|
|
|
})
|
2021-08-13 14:46:22 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 15:18:20 +05:00
|
|
|
|
type asyncFunction = (...args: any) => Promise<any|void>
|
2021-08-13 14:46:22 +05:00
|
|
|
|
|
2021-12-07 19:37:13 +05:00
|
|
|
|
export const invokeWebApiWrapperAsync = async (
|
|
|
|
|
funcAsync: asyncFunction,
|
2021-12-27 15:18:20 +05:00
|
|
|
|
setShowLoader?: Dispatch<SetStateAction<boolean>>,
|
2022-02-22 15:28:05 +05:00
|
|
|
|
errorNotifyText?: ReactNode | ((ex: unknown) => ReactNode),
|
2022-02-07 17:44:46 +05:00
|
|
|
|
actionName?: string,
|
2021-12-07 19:37:13 +05:00
|
|
|
|
) => {
|
2021-12-27 15:18:20 +05:00
|
|
|
|
setShowLoader?.(true)
|
2021-08-13 14:46:22 +05:00
|
|
|
|
try{
|
|
|
|
|
await funcAsync()
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
if(process.env.NODE_ENV === 'development')
|
2021-09-01 10:31:56 +05:00
|
|
|
|
console.error(ex)
|
2022-02-07 17:44:46 +05:00
|
|
|
|
if (ex instanceof ApiError && ex.status === 403) {
|
|
|
|
|
if (actionName)
|
|
|
|
|
notify(`Недостаточно прав для выполнения действия "${actionName}"`, 'error')
|
|
|
|
|
else
|
|
|
|
|
notify('Недостаточно прав для выполнения действия', 'error')
|
|
|
|
|
} else if(errorNotifyText) {
|
2021-12-07 19:37:13 +05:00
|
|
|
|
if (typeof errorNotifyText === 'function')
|
|
|
|
|
notify(errorNotifyText(ex), 'error')
|
|
|
|
|
else notify(errorNotifyText, 'error')
|
|
|
|
|
}
|
2021-12-27 15:18:20 +05:00
|
|
|
|
} finally {
|
|
|
|
|
setShowLoader?.(false)
|
2021-08-13 14:46:22 +05:00
|
|
|
|
}
|
2021-08-17 10:46:10 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 15:18:20 +05:00
|
|
|
|
export const download = async (url: string, fileName?: string) => {
|
2021-08-17 10:46:10 +05:00
|
|
|
|
const response = await fetch(url, {
|
|
|
|
|
headers: {
|
2021-12-29 17:48:10 +05:00
|
|
|
|
Authorization: `Bearer ${getUserToken()}`
|
2021-08-17 10:46:10 +05:00
|
|
|
|
},
|
|
|
|
|
method: 'Get'
|
|
|
|
|
})
|
2021-09-01 10:31:56 +05:00
|
|
|
|
if(response.status !== 200)
|
|
|
|
|
throw response
|
2021-12-27 15:18:20 +05:00
|
|
|
|
const requestFileName = decodeURI(
|
|
|
|
|
fileName
|
|
|
|
|
?? response.headers.get('content-disposition')?.split(';').pop()?.split(`'`).pop()
|
|
|
|
|
?? url.replace('\\','/').split('/').pop()
|
|
|
|
|
?? 'file'
|
|
|
|
|
)
|
2021-08-17 10:46:10 +05:00
|
|
|
|
const blob = await response.blob()
|
2021-12-27 15:18:20 +05:00
|
|
|
|
const reader = new FileReader()
|
|
|
|
|
reader.readAsDataURL(blob)
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
|
const 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()
|
|
|
|
|
}
|
2021-08-17 10:46:10 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 15:18:20 +05:00
|
|
|
|
export const upload = async (url: string, formData: FormData) => {
|
|
|
|
|
const response = await fetch(url, {
|
2021-08-17 10:46:10 +05:00
|
|
|
|
headers: {
|
2021-12-29 17:48:10 +05:00
|
|
|
|
Authorization: `Bearer ${getUserToken()}`
|
2021-08-17 10:46:10 +05:00
|
|
|
|
},
|
|
|
|
|
method: 'Post',
|
|
|
|
|
body: formData,
|
|
|
|
|
})
|
2021-10-11 13:41:54 +05:00
|
|
|
|
return response
|
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-12-27 15:18:20 +05:00
|
|
|
|
export const formatBytes = (bytes: number) => {
|
2021-08-31 12:30:44 +05:00
|
|
|
|
if(bytes < 1024)
|
|
|
|
|
return `${bytes.toFixed(0)}b`
|
2021-12-27 15:18:20 +05:00
|
|
|
|
if(bytes < 1024 * 1024)
|
|
|
|
|
return `${(bytes / 1024).toFixed(2)}kb`
|
2021-08-31 12:30:44 +05:00
|
|
|
|
else
|
2021-12-27 15:18:20 +05:00
|
|
|
|
return `${(bytes / 1024 / 1024).toFixed(2)}Mb`
|
2021-09-23 14:18:46 +05:00
|
|
|
|
}
|