2019-01-04 10:07:24 +01:00
|
|
|
import { format, parse } from 'date-fns'
|
|
|
|
import { DateTime } from 'luxon'
|
|
|
|
|
2019-07-07 15:34:44 +02:00
|
|
|
export const version = '0.2.1-beta' // version stored in 'utils' for now
|
2019-01-04 10:07:24 +01:00
|
|
|
export const apiUrl = `${process.env.REACT_APP_API_URL}/api/`
|
2019-08-28 15:35:22 +02:00
|
|
|
/* prettier-ignore */
|
2019-01-04 10:07:24 +01:00
|
|
|
export const thunderforestApiKey = `${
|
|
|
|
process.env.REACT_APP_THUNDERFOREST_API_KEY
|
2019-08-28 15:35:22 +02:00
|
|
|
}`
|
2019-01-04 10:07:24 +01:00
|
|
|
export const gpxLimit = `${process.env.REACT_APP_GPX_LIMIT_IMPORT}`
|
2019-08-25 12:50:42 +02:00
|
|
|
export const isRegistrationAllowed =
|
|
|
|
process.env.REACT_APP_ALLOW_REGISTRATION !== 'false'
|
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-08-28 15:35:22 +02:00
|
|
|
.then(response => (params.method === 'DELETE' ? 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
|
|
|
}
|