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'
|
2022-07-03 16:19:59 +02:00
|
|
|
import { ICardRecord, IRecord, IRecordsBySports } from '@/types/workouts'
|
2021-09-22 10:39:25 +02:00
|
|
|
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> => {
|
2022-03-24 17:17:44 +01:00
|
|
|
const distanceUnitFrom: TUnit = 'km'
|
|
|
|
const distanceUnitTo: TUnit = useImperialUnits
|
|
|
|
? units[distanceUnitFrom].defaultTarget
|
|
|
|
: distanceUnitFrom
|
|
|
|
const ascentUnitFrom: TUnit = 'm'
|
|
|
|
const ascentUnitTo: TUnit = useImperialUnits
|
|
|
|
? units[ascentUnitFrom].defaultTarget
|
|
|
|
: ascentUnitFrom
|
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,
|
2022-03-24 17:17:44 +01:00
|
|
|
distanceUnitFrom,
|
|
|
|
distanceUnitTo,
|
2021-11-14 12:12:21 +01:00
|
|
|
2
|
2022-03-24 17:17:44 +01:00
|
|
|
)} ${distanceUnitTo}/h`
|
2021-09-22 10:39:25 +02:00
|
|
|
break
|
|
|
|
case 'FD':
|
2022-07-23 08:20:02 +02:00
|
|
|
value = `${convertDistance(
|
|
|
|
+record.value,
|
|
|
|
distanceUnitFrom,
|
|
|
|
distanceUnitTo,
|
|
|
|
3
|
|
|
|
)} ${distanceUnitTo}`
|
2022-03-24 17:17:44 +01:00
|
|
|
break
|
|
|
|
case 'HA':
|
2022-07-23 08:20:02 +02:00
|
|
|
value = `${convertDistance(
|
|
|
|
+record.value,
|
|
|
|
ascentUnitFrom,
|
|
|
|
ascentUnitTo,
|
|
|
|
2
|
|
|
|
)} ${ascentUnitTo}`
|
2021-09-22 10:39:25 +02:00
|
|
|
break
|
|
|
|
case 'LD':
|
|
|
|
value = record.value
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw new Error(
|
2022-03-24 17:17:44 +01:00
|
|
|
`Invalid record type, expected: "AS", "FD", "HA", "LD", "MD", got: "${record.record_type}"`
|
2021-09-22 10:39:25 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 16:19:59 +02:00
|
|
|
export const sortRecords = (a: ICardRecord, b: ICardRecord): number => {
|
|
|
|
const recordALabel = a.label.toLowerCase()
|
|
|
|
const recordBLabel = b.label.toLowerCase()
|
|
|
|
return recordALabel > recordBLabel ? 1 : recordALabel < recordBLabel ? -1 : 0
|
|
|
|
}
|
|
|
|
|
2021-09-22 10:39:25 +02:00
|
|
|
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,
|
2022-07-23 08:20:02 +02:00
|
|
|
useImperialUnits: boolean,
|
|
|
|
display_ascent: boolean
|
2021-09-22 10:39:25 +02:00
|
|
|
): IRecordsBySports =>
|
2022-07-23 08:20:02 +02:00
|
|
|
records
|
|
|
|
.filter((r) => (display_ascent ? true : r.record_type !== 'HA'))
|
|
|
|
.reduce((sportList: IRecordsBySports, record) => {
|
|
|
|
const sport = translatedSports.find((s) => s.id === record.sport_id)
|
|
|
|
if (sport && sport.label) {
|
|
|
|
if (sportList[sport.translatedLabel] === void 0) {
|
|
|
|
sportList[sport.translatedLabel] = {
|
|
|
|
label: sport.label,
|
|
|
|
color: sport.color,
|
|
|
|
records: [],
|
|
|
|
}
|
2021-09-22 10:39:25 +02:00
|
|
|
}
|
2022-07-23 08:20:02 +02:00
|
|
|
sportList[sport.translatedLabel].records.push(
|
|
|
|
formatRecord(record, tz, useImperialUnits)
|
|
|
|
)
|
2021-09-22 10:39:25 +02:00
|
|
|
}
|
2022-07-23 08:20:02 +02:00
|
|
|
return sportList
|
|
|
|
}, {})
|