2021-10-01 16:48:48 +02:00
|
|
|
import { ISport, ITranslatedSport } from '@/types/sports'
|
2021-09-30 21:09:15 +02:00
|
|
|
import { IWorkout } from '@/types/workouts'
|
2021-09-22 10:39:25 +02:00
|
|
|
|
2021-10-01 16:48:48 +02:00
|
|
|
// TODO: allow user to change colors
|
2021-09-20 12:18:40 +02:00
|
|
|
export const sportColors: Record<string, string> = {
|
2021-10-01 16:48:48 +02:00
|
|
|
'Cycling (Sport)': '#4c9792',
|
|
|
|
'Cycling (Transport)': '#88af98',
|
|
|
|
Hiking: '#bb757c',
|
|
|
|
'Mountain Biking': '#d4b371',
|
2021-11-03 19:20:38 +01:00
|
|
|
'Mountain Biking (Electric)': '#fc9d6f',
|
|
|
|
Rowing: '#fcce72',
|
2021-10-01 16:48:48 +02:00
|
|
|
Running: '#835b83',
|
2021-11-03 19:20:38 +01:00
|
|
|
'Skiing (Alpine)': '#67a4bd',
|
|
|
|
'Skiing (Cross Country)': '#9498d0',
|
|
|
|
Trail: '#09a98a',
|
2021-10-01 16:48:48 +02:00
|
|
|
Walking: '#838383',
|
2021-09-20 12:18:40 +02:00
|
|
|
}
|
2021-09-22 10:39:25 +02:00
|
|
|
|
2021-09-30 21:09:15 +02:00
|
|
|
export const sportIdColors = (sports: ISport[]): Record<number, string> => {
|
|
|
|
const colors: Record<number, string> = {}
|
2021-11-12 18:55:15 +01:00
|
|
|
sports.map(
|
|
|
|
(sport) =>
|
|
|
|
(colors[sport.id] = sport.color ? sport.color : sportColors[sport.label])
|
|
|
|
)
|
2021-09-30 21:09:15 +02:00
|
|
|
return colors
|
|
|
|
}
|
|
|
|
|
2021-10-01 16:48:48 +02:00
|
|
|
const sortSports = (a: ITranslatedSport, b: ITranslatedSport): number => {
|
|
|
|
const sportATranslatedLabel = a.translatedLabel.toLowerCase()
|
|
|
|
const sportBTranslatedLabel = b.translatedLabel.toLowerCase()
|
|
|
|
return sportATranslatedLabel > sportBTranslatedLabel
|
|
|
|
? 1
|
|
|
|
: sportATranslatedLabel < sportBTranslatedLabel
|
|
|
|
? -1
|
|
|
|
: 0
|
2021-09-22 10:39:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const translateSports = (
|
|
|
|
sports: ISport[],
|
|
|
|
t: CallableFunction,
|
2021-11-13 12:12:22 +01:00
|
|
|
onlyActive = false,
|
|
|
|
userSports: number[] | null = null
|
2021-10-01 16:48:48 +02:00
|
|
|
): ITranslatedSport[] =>
|
2021-09-22 10:39:25 +02:00
|
|
|
sports
|
2021-11-13 12:12:22 +01:00
|
|
|
.filter((sport) =>
|
|
|
|
onlyActive
|
|
|
|
? userSports === null
|
|
|
|
? sport.is_active_for_user
|
|
|
|
: userSports.includes(sport.id) || sport.is_active
|
|
|
|
: true
|
|
|
|
)
|
2021-09-22 10:39:25 +02:00
|
|
|
.map((sport) => ({
|
|
|
|
...sport,
|
2021-10-01 16:48:48 +02:00
|
|
|
translatedLabel: t(`sports.${sport.label}.LABEL`),
|
2021-09-22 10:39:25 +02:00
|
|
|
}))
|
|
|
|
.sort(sortSports)
|
2021-09-30 21:09:15 +02:00
|
|
|
|
2021-10-01 16:48:48 +02:00
|
|
|
export const getSportLabel = (workout: IWorkout, sports: ISport[]): string => {
|
2021-09-30 21:09:15 +02:00
|
|
|
return sports
|
|
|
|
.filter((sport) => sport.id === workout.sport_id)
|
2021-10-01 16:48:48 +02:00
|
|
|
.map((sport) => sport.label)[0]
|
2021-09-30 21:09:15 +02:00
|
|
|
}
|
2021-11-12 18:52:08 +01:00
|
|
|
|
|
|
|
export const getSportColor = (
|
|
|
|
workout: IWorkout,
|
|
|
|
sports: ISport[]
|
|
|
|
): string | null => {
|
|
|
|
return sports
|
|
|
|
.filter((sport) => sport.id === workout.sport_id)
|
|
|
|
.map((sport) => sport.color)[0]
|
|
|
|
}
|