Client - display records on Dashboard (wip)
This commit is contained in:
@ -4,6 +4,7 @@ import {
|
||||
addYears,
|
||||
endOfMonth,
|
||||
endOfWeek,
|
||||
format,
|
||||
startOfMonth,
|
||||
startOfWeek,
|
||||
startOfYear,
|
||||
@ -60,3 +61,20 @@ export const getCalendarStartAndEnd = (
|
||||
end: endOfWeek(monthEnd),
|
||||
}
|
||||
}
|
||||
|
||||
export const formatWorkoutDate = (
|
||||
dateTime: Date,
|
||||
dateFormat: string | null = null,
|
||||
timeFormat: string | null = null
|
||||
): Record<string, string> => {
|
||||
if (!dateFormat) {
|
||||
dateFormat = 'yyyy/MM/dd'
|
||||
}
|
||||
if (!timeFormat) {
|
||||
timeFormat = 'HH:mm'
|
||||
}
|
||||
return {
|
||||
workout_date: format(dateTime, dateFormat),
|
||||
workout_time: format(dateTime, timeFormat),
|
||||
}
|
||||
}
|
||||
|
53
fittrackee_client/src/utils/records.ts
Normal file
53
fittrackee_client/src/utils/records.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import { ISport } from '@/types/sports'
|
||||
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[],
|
||||
sports: ISport[],
|
||||
tz: string
|
||||
): IRecordsBySports =>
|
||||
records.reduce((sportList: IRecordsBySports, record) => {
|
||||
const sport = sports.find((s) => s.id === record.sport_id)
|
||||
if (sport && sport.label) {
|
||||
if (sportList[sport.label] === void 0) {
|
||||
sportList[sport.label] = {
|
||||
img: sport.img,
|
||||
records: [],
|
||||
}
|
||||
}
|
||||
sportList[sport.label].records.push(formatRecord(record, tz))
|
||||
}
|
||||
return sportList
|
||||
}, {})
|
@ -1,3 +1,5 @@
|
||||
import { ISport } from '@/types/sports'
|
||||
|
||||
export const sportColors: Record<string, string> = {
|
||||
'Cycling (Sport)': '#55A8A3',
|
||||
'Cycling (Transport)': '#98C3A9',
|
||||
@ -6,3 +8,22 @@ export const sportColors: Record<string, string> = {
|
||||
Running: '#926692',
|
||||
Walking: '#929292',
|
||||
}
|
||||
|
||||
const sortSports = (a: ISport, b: ISport): number => {
|
||||
const sportALabel = a.label.toLowerCase()
|
||||
const sportBLabel = b.label.toLowerCase()
|
||||
return sportALabel > sportBLabel ? 1 : sportALabel < sportBLabel ? -1 : 0
|
||||
}
|
||||
|
||||
export const translateSports = (
|
||||
sports: ISport[],
|
||||
t: CallableFunction,
|
||||
onlyActive = false
|
||||
): ISport[] =>
|
||||
sports
|
||||
.filter((sport) => (onlyActive ? sport.is_active : true))
|
||||
.map((sport) => ({
|
||||
...sport,
|
||||
label: t(`sports.${sport.label}.LABEL`),
|
||||
}))
|
||||
.sort(sortSports)
|
||||
|
Reference in New Issue
Block a user