Client - rename existing front
This commit is contained in:
@ -1,44 +0,0 @@
|
||||
const routesWithoutAuthentication = [
|
||||
'/login',
|
||||
'/register',
|
||||
'/password-reset',
|
||||
'/password-reset/request',
|
||||
'/password-reset/sent',
|
||||
'/updated-password',
|
||||
]
|
||||
|
||||
const updatePath = (toPath, newPath) => {
|
||||
if (typeof toPath === 'string' || toPath instanceof String) {
|
||||
toPath = newPath
|
||||
} else {
|
||||
toPath.pathname = newPath
|
||||
}
|
||||
return toPath
|
||||
}
|
||||
|
||||
const pathInterceptor = toPath => {
|
||||
if (
|
||||
!window.localStorage.authToken &&
|
||||
!routesWithoutAuthentication.includes(toPath.pathname)
|
||||
) {
|
||||
toPath = updatePath(toPath, '/login')
|
||||
}
|
||||
if (
|
||||
window.localStorage.authToken &&
|
||||
routesWithoutAuthentication.includes(toPath.pathname)
|
||||
) {
|
||||
toPath = updatePath(toPath, '/')
|
||||
}
|
||||
return toPath
|
||||
}
|
||||
|
||||
export const historyEnhancer = originalHistory => {
|
||||
originalHistory.location = pathInterceptor(originalHistory.location)
|
||||
return {
|
||||
...originalHistory,
|
||||
push: (path, ...args) =>
|
||||
originalHistory.push(pathInterceptor(path), ...args),
|
||||
replace: (path, ...args) =>
|
||||
originalHistory.replace(pathInterceptor(path), ...args),
|
||||
}
|
||||
}
|
@ -1,126 +0,0 @@
|
||||
import { format, parse } from 'date-fns'
|
||||
import { DateTime } from 'luxon'
|
||||
|
||||
const suffixes = ['bytes', 'KB', 'MB', 'GB', 'TB']
|
||||
export const getFileSize = (fileSize, asText = true) => {
|
||||
const i = Math.floor(Math.log(fileSize) / Math.log(1024))
|
||||
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 }
|
||||
}
|
||||
|
||||
export const getFileSizeInMB = fileSize => {
|
||||
const value = fileSize / 1048576
|
||||
return (!fileSize && 0) || +value.toFixed(2)
|
||||
}
|
||||
|
||||
export const version = '0.4.9' // version stored in 'utils' for now
|
||||
export const apiUrl =
|
||||
process.env.NODE_ENV === 'production'
|
||||
? '/api/'
|
||||
: `${process.env.REACT_APP_API_URL}/api/`
|
||||
|
||||
export const userFilters = [
|
||||
{ key: 'workouts_count', label: 'workouts 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' },
|
||||
]
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
export const createApiRequest = 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(`${apiUrl}${params.url}`, requestParams)
|
||||
return fetch(request)
|
||||
.then(response =>
|
||||
params.method === 'DELETE' || response.status === 413
|
||||
? response
|
||||
: response.json()
|
||||
)
|
||||
.catch(error => {
|
||||
console.error(error)
|
||||
return new Error('An error occurred. Please contact the administrator.')
|
||||
})
|
||||
}
|
||||
|
||||
export const getDateWithTZ = (date, tz) => {
|
||||
if (!date) {
|
||||
return ''
|
||||
}
|
||||
const dt = DateTime.fromISO(
|
||||
format(new Date(date), "yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
|
||||
).setZone(tz)
|
||||
return parse(
|
||||
dt.toFormat('yyyy-MM-dd HH:mm:ss'),
|
||||
'yyyy-MM-dd HH:mm:ss',
|
||||
new Date()
|
||||
)
|
||||
}
|
||||
|
||||
export const capitalize = target =>
|
||||
target.charAt(0).toUpperCase() + target.slice(1)
|
||||
|
||||
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
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
import {
|
||||
addDays,
|
||||
addMonths,
|
||||
addYears,
|
||||
format,
|
||||
startOfMonth,
|
||||
startOfWeek,
|
||||
startOfYear,
|
||||
} from 'date-fns'
|
||||
|
||||
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' },
|
||||
]
|
||||
|
||||
export const formatDuration = (totalSeconds, formatWithDay = false) => {
|
||||
let days = '0'
|
||||
if (formatWithDay) {
|
||||
days = String(Math.floor(totalSeconds / 86400))
|
||||
totalSeconds %= 86400
|
||||
}
|
||||
const hours = String(Math.floor(totalSeconds / 3600)).padStart(2, '0')
|
||||
totalSeconds %= 3600
|
||||
const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, '0')
|
||||
const seconds = String(totalSeconds % 60).padStart(2, '0')
|
||||
if (formatWithDay) {
|
||||
return `${days === '0' ? '' : `${days}d:`}${
|
||||
hours === '00' ? '' : `${hours}h:`
|
||||
}${minutes}m:${seconds}s`
|
||||
}
|
||||
return `${hours === '00' ? '' : `${hours}:`}${minutes}:${seconds}`
|
||||
}
|
||||
|
||||
export const formatValue = (displayedData, value) =>
|
||||
value === 0
|
||||
? ''
|
||||
: displayedData === 'distance'
|
||||
? `${value.toFixed(2)} km`
|
||||
: displayedData === 'duration'
|
||||
? formatDuration(value)
|
||||
: displayedData === 'ascent'
|
||||
? `${value.toFixed(2)} km`
|
||||
: displayedData === 'descent'
|
||||
? `${value.toFixed(2)} km`
|
||||
: value
|
||||
|
||||
const dateIncrement = (duration, day) => {
|
||||
switch (duration) {
|
||||
case 'week':
|
||||
return addDays(day, 7)
|
||||
case 'year':
|
||||
return addYears(day, 1)
|
||||
case 'month':
|
||||
default:
|
||||
return addMonths(day, 1)
|
||||
}
|
||||
}
|
||||
|
||||
const startDate = (duration, day, weekm) => {
|
||||
switch (duration) {
|
||||
case 'week':
|
||||
return startOfWeek(day, { weekStartsOn: weekm ? 1 : 0 })
|
||||
case 'year':
|
||||
return startOfYear(day)
|
||||
case 'month':
|
||||
default:
|
||||
return startOfMonth(day)
|
||||
}
|
||||
}
|
||||
|
||||
export const formatStats = (stats, sports, params, displayedSports, weekm) => {
|
||||
const nbWorkoutsStats = []
|
||||
const distanceStats = []
|
||||
const durationStats = []
|
||||
const ascentStats = []
|
||||
const descentStats = []
|
||||
|
||||
for (
|
||||
let day = startDate(params.duration, params.start, weekm);
|
||||
day <= params.end;
|
||||
day = dateIncrement(params.duration, day)
|
||||
) {
|
||||
const [xAxisFormat] = xAxisFormats.filter(
|
||||
x => x.duration === params.duration
|
||||
)
|
||||
const date = format(day, xAxisFormat.dateFormat)
|
||||
const xAxis = format(day, xAxisFormat.xAxis)
|
||||
const dataNbWorkouts = { date: xAxis }
|
||||
const dataDistance = { date: xAxis }
|
||||
const dataDuration = { date: xAxis }
|
||||
const dataAscent = { date: xAxis }
|
||||
const dataDescent = { date: xAxis }
|
||||
|
||||
if (stats[date]) {
|
||||
Object.keys(stats[date])
|
||||
.filter(sportId =>
|
||||
displayedSports ? displayedSports.includes(+sportId) : true
|
||||
)
|
||||
.map(sportId => {
|
||||
const sportLabel = sports.filter(s => s.id === +sportId)[0].label
|
||||
dataNbWorkouts[sportLabel] = stats[date][sportId].nb_workouts
|
||||
dataDistance[sportLabel] = stats[date][sportId].total_distance
|
||||
dataDuration[sportLabel] = stats[date][sportId].total_duration
|
||||
dataAscent[sportLabel] = stats[date][sportId].total_ascent / 1000
|
||||
dataDescent[sportLabel] = stats[date][sportId].total_descent / 1000
|
||||
return null
|
||||
})
|
||||
}
|
||||
nbWorkoutsStats.push(dataNbWorkouts)
|
||||
distanceStats.push(dataDistance)
|
||||
durationStats.push(dataDuration)
|
||||
ascentStats.push(dataAscent)
|
||||
descentStats.push(dataDescent)
|
||||
}
|
||||
|
||||
return {
|
||||
workouts: nbWorkoutsStats,
|
||||
distance: distanceStats,
|
||||
duration: durationStats,
|
||||
ascent: ascentStats,
|
||||
descent: descentStats,
|
||||
}
|
||||
}
|
@ -1,107 +0,0 @@
|
||||
import { format, subHours } from 'date-fns'
|
||||
import togeojson from '@mapbox/togeojson'
|
||||
|
||||
import { getDateWithTZ } from './index'
|
||||
|
||||
export const workoutColors = [
|
||||
'#55a8a3', // Cycling (sport)
|
||||
'#98C3A9', // Cycling (transport)
|
||||
'#D0838A', // Hiking
|
||||
'#ECC77E', // Mountain bike
|
||||
'#926692', // Running
|
||||
'#929292', // Walking
|
||||
'#f7af88', // Mountain Biking (Electric)
|
||||
'#fbc2a6', // Trail
|
||||
'#67a4bd', // Skiing (Alpine)
|
||||
'#9498d0', // Skiing (Cross Country)
|
||||
'#fad783', // Rowing
|
||||
]
|
||||
|
||||
export const recordsLabels = [
|
||||
{ record_type: 'AS', label: 'Ave. 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 formatWorkoutDate = (
|
||||
dateTime,
|
||||
dateFormat = null,
|
||||
timeFormat = null
|
||||
) => {
|
||||
if (!dateFormat) {
|
||||
dateFormat = 'yyyy/MM/dd'
|
||||
}
|
||||
if (!timeFormat) {
|
||||
timeFormat = 'HH:mm'
|
||||
}
|
||||
return {
|
||||
workout_date: dateTime ? format(dateTime, dateFormat) : null,
|
||||
workout_time: dateTime ? format(dateTime, timeFormat) : null,
|
||||
}
|
||||
}
|
||||
|
||||
export const formatWorkoutDuration = 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 = formatWorkoutDuration(chartData[i].duration)
|
||||
}
|
||||
return chartData
|
||||
}
|
||||
|
||||
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
|
||||
default:
|
||||
// 'LD'
|
||||
value = record.value // eslint-disable-line prefer-destructuring
|
||||
}
|
||||
const [recordType] = recordsLabels.filter(
|
||||
r => r.record_type === record.record_type
|
||||
)
|
||||
return {
|
||||
workout_date: formatWorkoutDate(getDateWithTZ(record.workout_date, tz))
|
||||
.workout_date,
|
||||
workout_id: record.workout_id,
|
||||
id: record.id,
|
||||
record_type: recordType.label,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
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, onlyActive = false) =>
|
||||
sports
|
||||
.filter(sport => (onlyActive ? sport.is_active : true))
|
||||
.map(sport => ({
|
||||
...sport,
|
||||
label: t(`sports:${sport.label}`),
|
||||
}))
|
||||
.sort(sortSports)
|
Reference in New Issue
Block a user