2021-10-01 16:48:48 +02:00
|
|
|
import { ITranslatedSport } from '@/types/sports'
|
2021-11-14 12:12:21 +01:00
|
|
|
import { TUnit } from '@/types/units'
|
2021-09-22 10:39:25 +02:00
|
|
|
import { IRecord, IRecordsBySports } from '@/types/workouts'
|
|
|
|
import { formatWorkoutDate, getDateWithTZ } from '@/utils/dates'
|
2021-11-14 12:12:21 +01:00
|
|
|
import { convertDistance, units } from '@/utils/units'
|
2021-09-22 10:39:25 +02:00
|
|
|
|
|
|
|
export const formatRecord = (
|
|
|
|
record: IRecord,
|
2021-11-14 12:12:21 +01:00
|
|
|
tz: string,
|
|
|
|
useImperialUnits: boolean
|
2021-09-22 10:39:25 +02:00
|
|
|
): Record<string, string | number> => {
|
2021-11-14 12:12:21 +01:00
|
|
|
const unitFrom: TUnit = 'km'
|
|
|
|
const unitTo: TUnit = useImperialUnits
|
|
|
|
? units[unitFrom].defaultTarget
|
|
|
|
: unitFrom
|
2021-09-22 10:39:25 +02:00
|
|
|
let value
|
|
|
|
switch (record.record_type) {
|
|
|
|
case 'AS':
|
|
|
|
case 'MS':
|
2021-11-14 12:12:21 +01:00
|
|
|
value = `${convertDistance(
|
|
|
|
+record.value,
|
|
|
|
unitFrom,
|
|
|
|
unitTo,
|
|
|
|
2
|
|
|
|
)} ${unitTo}/h`
|
2021-09-22 10:39:25 +02:00
|
|
|
break
|
|
|
|
case 'FD':
|
2021-11-14 12:12:21 +01:00
|
|
|
value = `${convertDistance(+record.value, unitFrom, unitTo, 2)} ${unitTo}`
|
2021-09-22 10:39:25 +02:00
|
|
|
break
|
|
|
|
case 'LD':
|
|
|
|
value = record.value
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw new Error(
|
|
|
|
`Invalid record type, expected: "AS", "FD", "LD", "MD", got: "${record.record_type}"`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
workout_date: formatWorkoutDate(getDateWithTZ(record.workout_date, tz))
|
|
|
|
.workout_date,
|
|
|
|
workout_id: record.workout_id,
|
|
|
|
id: record.id,
|
|
|
|
record_type: record.record_type,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getRecordsBySports = (
|
|
|
|
records: IRecord[],
|
2021-10-01 16:48:48 +02:00
|
|
|
translatedSports: ITranslatedSport[],
|
2021-11-14 12:12:21 +01:00
|
|
|
tz: string,
|
|
|
|
useImperialUnits: boolean
|
2021-09-22 10:39:25 +02:00
|
|
|
): IRecordsBySports =>
|
|
|
|
records.reduce((sportList: IRecordsBySports, record) => {
|
2021-10-01 16:48:48 +02:00
|
|
|
const sport = translatedSports.find((s) => s.id === record.sport_id)
|
2021-09-22 10:39:25 +02:00
|
|
|
if (sport && sport.label) {
|
2021-10-01 16:48:48 +02:00
|
|
|
if (sportList[sport.translatedLabel] === void 0) {
|
|
|
|
sportList[sport.translatedLabel] = {
|
|
|
|
label: sport.label,
|
2021-11-12 18:52:08 +01:00
|
|
|
color: sport.color,
|
2021-09-22 10:39:25 +02:00
|
|
|
records: [],
|
|
|
|
}
|
|
|
|
}
|
2021-11-14 12:12:21 +01:00
|
|
|
sportList[sport.translatedLabel].records.push(
|
|
|
|
formatRecord(record, tz, useImperialUnits)
|
|
|
|
)
|
2021-09-22 10:39:25 +02:00
|
|
|
}
|
|
|
|
return sportList
|
|
|
|
}, {})
|