FitTrackee/fittrackee_client/src/utils/activities.js

103 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-01-05 12:57:19 +01:00
import { format, subHours } from 'date-fns'
2019-01-04 10:07:24 +01:00
import togeojson from '@mapbox/togeojson'
import { getDateWithTZ } from './index'
export const activityColors = [
'#55a8a3',
'#98C3A9',
'#D0838A',
'#ECC77E',
'#926692',
'#929292',
'#428bca',
]
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 getGeoJson = gpxContent => {
let jsonData
if (gpxContent) {
const gpx = new DOMParser().parseFromString(gpxContent, 'text/xml')
jsonData = togeojson.gpx(gpx)
}
return { jsonData }
}
export const formatActivityDate = (
dateTime,
dateFormat = null,
2019-08-28 15:35:22 +02:00
timeFormat = null
2019-01-04 10:07:24 +01:00
) => {
if (!dateFormat) {
dateFormat = 'yyyy/MM/dd'
2019-01-04 10:07:24 +01:00
}
if (!timeFormat) {
timeFormat = 'HH:mm'
}
return {
activity_date: dateTime ? format(dateTime, dateFormat) : null,
activity_time: dateTime ? format(dateTime, timeFormat) : null,
}
}
2019-01-05 12:57:19 +01:00
export const formatActivityDuration = seconds => {
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 = formatActivityDuration(chartData[i].duration)
}
return chartData
}
2019-01-04 10:07:24 +01:00
export const formatRecord = (record, tz) => {
let value
switch (record.record_type) {
case 'AS':
case 'MS':
value = `${record.value} km/h`
break
case 'FD':
value = `${record.value} km`
break
2019-08-28 15:35:22 +02:00
default:
// 'LD'
2019-01-04 10:07:24 +01:00
value = record.value // eslint-disable-line prefer-destructuring
}
const [recordType] = recordsLabels.filter(
r => r.record_type === record.record_type
)
return {
2019-08-28 15:35:22 +02:00
activity_date: formatActivityDate(getDateWithTZ(record.activity_date, tz))
.activity_date,
2019-01-04 10:07:24 +01:00
activity_id: record.activity_id,
id: record.id,
record_type: recordType.label,
value: value,
}
}
2019-09-16 10:26:02 +02:00
const sortSports = (a, b) => {
const sportALabel = a.label.toLowerCase()
const sportBLabel = b.label.toLowerCase()
return sportALabel > sportBLabel ? 1 : sportALabel < sportBLabel ? -1 : 0
}
export const translateSports = (sports, t) =>
sports
.map(sport => ({
...sport,
label: t(`sports:${sport.label}`),
}))
.sort(sortSports)