FitTrackee/fittrackee_client/src/utils.js

198 lines
5.0 KiB
JavaScript
Raw Normal View History

2018-05-03 21:42:54 +02:00
import togeojson from '@mapbox/togeojson'
2018-07-29 17:59:35 +02:00
import {
addDays, addMonths, addYears, format, parse, startOfWeek, subHours
} from 'date-fns'
import { DateTime } from 'luxon'
2018-05-03 21:42:54 +02:00
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
}`
export const gpxLimit = `${process.env.REACT_APP_GPX_LIMIT_IMPORT}`
2018-06-07 12:42:30 +02:00
export const activityColors = [
'#55a8a3',
'#98C3A9',
'#D0838A',
'#ECC77E',
'#926692',
'#929292',
'#428bca',
]
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 => {
2018-07-29 12:06:36 +02:00
const obj = { id: i, value: val }
i++
return obj
})
2018-01-14 12:48:52 +01:00
}
2018-05-03 21:42:54 +02:00
2018-05-28 12:35:17 +02:00
export const createRequest = params => {
const headers = {}
if (!params.noAuthorization) {
headers.Authorization = `Bearer ${
window.localStorage.getItem('authToken')}`
}
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
}
const request = new Request(params.url, requestParams)
return fetch(request)
2018-05-29 22:19:17 +02:00
.then(response => params.method === 'DELETE'
? response
: response.json())
2018-05-28 12:35:17 +02:00
.catch(error => error)
}
2018-05-04 23:04:44 +02:00
export const getGeoJson = gpxContent => {
let jsonData
2018-05-03 21:42:54 +02:00
if (gpxContent) {
const gpx = new DOMParser().parseFromString(gpxContent, 'text/xml')
jsonData = togeojson.gpx(gpx)
}
return { jsonData }
2018-05-03 21:42:54 +02:00
}
2018-05-10 16:56:45 +02:00
export const getDateWithTZ = (date, tz) => {
if (!date) {
return ''
}
const dt = DateTime.fromISO(format(date)).setZone(tz)
return parse(dt.toFormat('yyyy-MM-dd HH:mm:ss'))
}
export const formatActivityDate = (
dateTime,
dateFormat = null,
timeFormat = null,
) => {
if (!dateFormat) {
dateFormat = 'DD/MM/YYYY'
}
if (!timeFormat) {
timeFormat = 'HH:mm'
2018-05-16 23:52:55 +02:00
}
2018-05-10 16:56:45 +02:00
return {
activity_date: dateTime ? format(dateTime, dateFormat) : null,
activity_time: dateTime ? format(dateTime, timeFormat) : null,
2018-05-10 16:56:45 +02:00
}
}
2018-05-20 13:12:35 +02:00
2018-07-29 12:04:29 +02:00
export const recordsLabels = [
{ record_type: 'AS', label: 'Avg speed' },
{ record_type: 'FD', label: 'Farest distance' },
{ record_type: 'LD', label: 'Longest duration' },
{ record_type: 'MS', label: 'Max speed' },
]
export const formatRecord = (record, tz) => {
2018-05-20 13:12:35 +02:00
let value, recordType = null
switch (record.record_type) {
case 'AS':
case 'MS':
value = `${record.value} km/h`
break
case 'FD':
value = `${record.value} km`
break
default: // 'LD'
value = record.value // eslint-disable-line prefer-destructuring
}
2018-07-29 12:04:29 +02:00
[recordType] = recordsLabels.filter(r => r.record_type === record.record_type)
2018-05-20 13:12:35 +02:00
return {
activity_date: formatActivityDate(
getDateWithTZ(record.activity_date, tz)
).activity_date,
2018-05-20 13:12:35 +02:00
activity_id: record.activity_id,
id: record.id,
2018-07-29 12:04:29 +02:00
record_type: recordType.label,
2018-05-20 13:12:35 +02:00
value: value,
}
}
export const formatDuration = seconds => {
2018-07-29 12:06:36 +02:00
let newDate = new Date(0)
newDate = subHours(newDate.setSeconds(seconds), 1)
return newDate.getTime()
}
export const formatChartData = chartData => {
for (let i = 0; i < chartData.length; i++) {
chartData[i].time = new Date(chartData[i].time).getTime()
chartData[i].duration = formatDuration(chartData[i].duration)
}
return chartData
}
2018-07-29 17:59:35 +02:00
const xAxisFormats = [
{ duration: 'week', dateFormat: 'YYYY-MM-DD', xAxis: 'DD/MM' },
{ duration: 'month', dateFormat: 'YYYY-MM', xAxis: 'MM/YYYY' },
{ duration: 'year', dateFormat: 'YYYY', xAxis: 'YYYY' },
]
const dateIncrement = (duration, day) => {
switch (duration) {
case 'week':
return addDays(day, 7)
case 'year':
return addYears(day, 1)
2018-12-31 12:19:08 +01:00
case 'month':
default:
return addMonths(day, 1)
2018-07-29 17:59:35 +02:00
}
}
export const formatStats = (
stats, sports, startDate, endDate, duration = 'week'
) => {
const nbActivitiesStats = []
const distanceStats = []
const durationStats = []
for (let day = startOfWeek(startDate);
day <= endDate;
2018-07-29 17:59:35 +02:00
day = dateIncrement(duration, day)
) {
2018-07-29 17:59:35 +02:00
const [xAxisFormat] = xAxisFormats.filter(x => x.duration === duration)
const date = format(day, xAxisFormat.dateFormat)
const xAxis = format(day, xAxisFormat.xAxis)
2018-06-07 12:42:30 +02:00
const dataNbActivities = { date: xAxis }
const dataDistance = { date: xAxis }
const dataDuration = { date: xAxis }
if (stats[date]) {
Object.keys(stats[date]).map(sportId => {
const sportLabel = sports.filter(s => s.id === +sportId)[0].label
dataNbActivities[sportLabel] = stats[date][sportId].nb_activities
dataDistance[sportLabel] = stats[date][sportId].total_distance
dataDuration[sportLabel] = stats[date][sportId].total_duration
return null
})
}
nbActivitiesStats.push(dataNbActivities)
distanceStats.push(dataDistance)
durationStats.push(dataDuration)
}
return {
activities: nbActivitiesStats,
distance: distanceStats,
duration: durationStats
}
}