2021-10-01 16:48:48 +02:00
|
|
|
import { ITranslatedSport } from '@/types/sports'
|
2021-09-22 10:39:25 +02:00
|
|
|
import { IRecord, IRecordsBySports } from '@/types/workouts'
|
|
|
|
import { formatWorkoutDate, getDateWithTZ } from '@/utils/dates'
|
|
|
|
|
|
|
|
export const formatRecord = (
|
|
|
|
record: IRecord,
|
|
|
|
tz: string
|
|
|
|
): Record<string, string | number> => {
|
|
|
|
let value
|
|
|
|
switch (record.record_type) {
|
|
|
|
case 'AS':
|
|
|
|
case 'MS':
|
|
|
|
value = `${record.value} km/h`
|
|
|
|
break
|
|
|
|
case 'FD':
|
|
|
|
value = `${record.value} km`
|
|
|
|
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-09-22 10:39:25 +02:00
|
|
|
tz: string
|
|
|
|
): 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-09-22 10:39:25 +02:00
|
|
|
records: [],
|
|
|
|
}
|
|
|
|
}
|
2021-10-01 16:48:48 +02:00
|
|
|
sportList[sport.translatedLabel].records.push(formatRecord(record, tz))
|
2021-09-22 10:39:25 +02:00
|
|
|
}
|
|
|
|
return sportList
|
|
|
|
}, {})
|