FitTrackee/mpwo_client/src/utils.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-05-03 21:42:54 +02:00
import togeojson from '@mapbox/togeojson'
2018-05-10 16:56:45 +02:00
import { format, parse } from 'date-fns'
2018-05-03 21:42:54 +02:00
import bbox from 'geojson-bbox'
2018-04-29 17:02:08 +02:00
export const apiUrl = `${process.env.REACT_APP_API_URL}/api/`
2018-05-03 14:07:31 +02:00
export const thunderforestApiKey = `${
process.env.REACT_APP_THUNDERFOREST_API_KEY
}`
2018-04-29 17:02:08 +02:00
2017-12-25 20:11:10 +01:00
export const isLoggedIn = () => !!window.localStorage.authToken
2018-01-14 12:48:52 +01:00
2018-05-04 23:04:44 +02:00
export const generateIds = arr => {
2018-01-14 12:48:52 +01:00
let i = 0
2018-01-14 17:19:02 +01:00
return arr.map(val => {
const obj = { id: i, value: val }
2018-01-14 12:48:52 +01:00
i++
return obj
})
}
2018-05-03 21:42:54 +02:00
2018-05-04 23:04:44 +02:00
export const getGeoJson = gpxContent => {
2018-05-03 21:42:54 +02:00
let jsonData, bboxCorners
let bounds = [[0, 0], [0, 0]]
if (gpxContent) {
const gpx = new DOMParser().parseFromString(gpxContent, 'text/xml')
jsonData = togeojson.gpx(gpx)
bboxCorners = bbox(jsonData)
bounds = [
[bboxCorners[1], bboxCorners[0]],
[bboxCorners[3], bboxCorners[2]]
]
}
return { jsonData, bounds }
}
2018-05-10 16:56:45 +02:00
export const formatActivityDate = activityDateTime => {
const dateTime = parse(activityDateTime)
return {
activity_date: format(dateTime, 'YYYY-MM-DD'),
activity_time: activityDateTime.match(/[0-2][0-9]:[0-5][0-9]/)[0]
}
}