2019-01-04 10:07:24 +01:00
|
|
|
import { format, parse } from 'date-fns'
|
|
|
|
import { DateTime } from 'luxon'
|
|
|
|
|
2019-08-31 14:11:00 +02:00
|
|
|
const suffixes = ['bytes', 'KB', 'MB', 'GB', 'TB']
|
2020-05-01 20:18:19 +02:00
|
|
|
export const getFileSize = (fileSize, asText = true) => {
|
2019-08-31 14:11:00 +02:00
|
|
|
const i = Math.floor(Math.log(fileSize) / Math.log(1024))
|
2020-05-01 20:18:19 +02:00
|
|
|
if (!fileSize) {
|
|
|
|
return asText ? '0 bytes' : { size: 0, suffix: 'bytes' }
|
|
|
|
}
|
|
|
|
const size = (fileSize / Math.pow(1024, i)).toFixed(1)
|
|
|
|
const suffix = suffixes[i]
|
|
|
|
return asText ? `${size}${suffix}` : { size, suffix }
|
2019-08-31 14:11:00 +02:00
|
|
|
}
|
|
|
|
|
2019-11-13 20:15:50 +01:00
|
|
|
export const getFileSizeInMB = fileSize => {
|
|
|
|
const value = fileSize / 1048576
|
|
|
|
return (!fileSize && 0) || +value.toFixed(2)
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:42:49 +02:00
|
|
|
export const version = '0.4.0-beta' // version stored in 'utils' for now
|
2020-09-16 13:48:15 +02:00
|
|
|
export const apiUrl =
|
|
|
|
process.env.NODE_ENV === 'production'
|
|
|
|
? '/api/'
|
|
|
|
: `${process.env.REACT_APP_API_URL}/api/`
|
2019-01-04 10:07:24 +01:00
|
|
|
|
2020-05-02 18:00:17 +02:00
|
|
|
export const userFilters = [
|
|
|
|
{ key: 'activities_count', label: 'activities count' },
|
|
|
|
{ key: 'admin', label: 'admin rights' },
|
|
|
|
{ key: 'created_at', label: 'registration date' },
|
|
|
|
{ key: 'username', label: 'user name' },
|
|
|
|
]
|
|
|
|
|
|
|
|
export const sortOrders = [
|
|
|
|
{ key: 'asc', label: 'ascending' },
|
|
|
|
{ key: 'desc', label: 'descending' },
|
|
|
|
]
|
|
|
|
|
2019-01-04 10:07:24 +01:00
|
|
|
export const isLoggedIn = () => !!window.localStorage.authToken
|
|
|
|
|
|
|
|
export const generateIds = arr => {
|
|
|
|
let i = 0
|
|
|
|
return arr.map(val => {
|
|
|
|
const obj = { id: i, value: val }
|
|
|
|
i++
|
|
|
|
return obj
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-07 09:53:12 +01:00
|
|
|
export const createApiRequest = params => {
|
2019-01-04 10:07:24 +01:00
|
|
|
const headers = {}
|
|
|
|
if (!params.noAuthorization) {
|
2019-08-28 15:35:22 +02:00
|
|
|
headers.Authorization = `Bearer ${window.localStorage.getItem('authToken')}`
|
2019-01-04 10:07:24 +01:00
|
|
|
}
|
|
|
|
if (params.type) {
|
|
|
|
headers['Content-Type'] = params.type
|
|
|
|
}
|
|
|
|
const requestParams = {
|
|
|
|
method: params.method,
|
|
|
|
headers: headers,
|
|
|
|
}
|
|
|
|
if (params.type === 'application/json' && params.body) {
|
|
|
|
requestParams.body = JSON.stringify(params.body)
|
|
|
|
} else if (params.body) {
|
|
|
|
requestParams.body = params.body
|
|
|
|
}
|
2019-02-07 09:53:12 +01:00
|
|
|
const request = new Request(`${apiUrl}${params.url}`, requestParams)
|
2019-01-04 10:07:24 +01:00
|
|
|
return fetch(request)
|
2019-11-13 20:15:50 +01:00
|
|
|
.then(response =>
|
|
|
|
params.method === 'DELETE' || response.status === 413
|
|
|
|
? response
|
|
|
|
: response.json()
|
|
|
|
)
|
2019-02-07 09:53:12 +01:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error)
|
|
|
|
return new Error('An error occurred. Please contact the administrator.')
|
|
|
|
})
|
2019-01-04 10:07:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getDateWithTZ = (date, tz) => {
|
|
|
|
if (!date) {
|
|
|
|
return ''
|
|
|
|
}
|
2019-08-28 14:38:15 +02:00
|
|
|
const dt = DateTime.fromISO(
|
2019-08-28 15:35:22 +02:00
|
|
|
format(new Date(date), "yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
|
|
|
|
).setZone(tz)
|
2019-08-28 14:38:15 +02:00
|
|
|
return parse(
|
2019-08-28 15:35:22 +02:00
|
|
|
dt.toFormat('yyyy-MM-dd HH:mm:ss'),
|
|
|
|
'yyyy-MM-dd HH:mm:ss',
|
|
|
|
new Date()
|
|
|
|
)
|
2019-01-04 10:07:24 +01:00
|
|
|
}
|
2019-09-18 13:58:20 +02:00
|
|
|
|
|
|
|
export const capitalize = target =>
|
|
|
|
target.charAt(0).toUpperCase() + target.slice(1)
|
2020-05-02 18:00:17 +02:00
|
|
|
|
|
|
|
export const rangePagination = pages =>
|
|
|
|
Array.from({ length: pages }, (_, i) => i + 1)
|
|
|
|
|
|
|
|
const sortValues = (a, b) => {
|
|
|
|
const valueALabel = a.label.toLowerCase()
|
|
|
|
const valueBLabel = b.label.toLowerCase()
|
|
|
|
return valueALabel > valueBLabel ? 1 : valueALabel < valueBLabel ? -1 : 0
|
|
|
|
}
|
|
|
|
|
|
|
|
export const translateValues = (t, values, key = 'common') =>
|
|
|
|
values
|
|
|
|
.map(value => ({
|
|
|
|
...value,
|
|
|
|
label: t(`${key}:${value.label}`),
|
|
|
|
}))
|
|
|
|
.sort(sortValues)
|
|
|
|
|
|
|
|
export const formatUrl = (pathname, query) => {
|
|
|
|
let url = pathname
|
|
|
|
if (query.id || (pathname === 'users' && query.username)) {
|
|
|
|
url = `${url}/${query.username ? query.username : query.id}`
|
|
|
|
} else if (Object.keys(query).length > 0) {
|
|
|
|
url += '?'
|
|
|
|
Object.keys(query)
|
|
|
|
.filter(key => query[key])
|
|
|
|
.map(
|
|
|
|
(key, index) => (url += `${index === 0 ? '' : '&'}${key}=${query[key]}`)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|