Client - init utils methods needed for charts
This commit is contained in:
@ -2,10 +2,10 @@ import { ActionContext } from 'vuex'
|
||||
|
||||
import { STATS_STORE } from '@/store/constants'
|
||||
import { IRootState } from '@/store/modules/root/interfaces'
|
||||
import { IUserStatisticsPayload, TStatistics } from '@/types/statistics'
|
||||
import { IUserStatisticsPayload, TStatisticsFromApi } from '@/types/statistics'
|
||||
|
||||
export interface IStatisticsState {
|
||||
statistics: TStatistics
|
||||
statistics: TStatisticsFromApi
|
||||
}
|
||||
|
||||
export interface IStatisticsActions {
|
||||
@ -16,5 +16,5 @@ export interface IStatisticsActions {
|
||||
}
|
||||
|
||||
export interface IStatisticsGetters {
|
||||
[STATS_STORE.GETTERS.USER_STATS](state: IStatisticsState): TStatistics
|
||||
[STATS_STORE.GETTERS.USER_STATS](state: IStatisticsState): TStatisticsFromApi
|
||||
}
|
||||
|
@ -3,13 +3,13 @@ import { MutationTree } from 'vuex'
|
||||
import { STATS_STORE } from '@/store/constants'
|
||||
import { IStatisticsState } from '@/store/modules/statistics/interfaces'
|
||||
import { TStatisticsMutations } from '@/store/modules/statistics/types'
|
||||
import { TStatistics } from '@/types/statistics'
|
||||
import { TStatisticsFromApi } from '@/types/statistics'
|
||||
|
||||
export const mutations: MutationTree<IStatisticsState> & TStatisticsMutations =
|
||||
{
|
||||
[STATS_STORE.MUTATIONS.UPDATE_USER_STATS](
|
||||
state: IStatisticsState,
|
||||
statistics: TStatistics
|
||||
statistics: TStatisticsFromApi
|
||||
) {
|
||||
state.statistics = statistics
|
||||
},
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { IStatisticsState } from '@/store/modules/statistics/interfaces'
|
||||
import { TStatistics } from '@/types/statistics'
|
||||
import { TStatisticsFromApi } from '@/types/statistics'
|
||||
|
||||
export const statisticsState: IStatisticsState = {
|
||||
statistics: <TStatistics>{},
|
||||
statistics: <TStatisticsFromApi>{},
|
||||
}
|
||||
|
@ -6,12 +6,12 @@ import {
|
||||
IStatisticsActions,
|
||||
IStatisticsGetters,
|
||||
} from '@/store/modules/statistics/interfaces'
|
||||
import { TStatistics } from '@/types/statistics'
|
||||
import { TStatisticsFromApi } from '@/types/statistics'
|
||||
|
||||
export type TStatisticsMutations<S = IStatisticsState> = {
|
||||
[STATS_STORE.MUTATIONS.UPDATE_USER_STATS](
|
||||
state: S,
|
||||
statistics: TStatistics
|
||||
statistics: TStatisticsFromApi
|
||||
): void
|
||||
}
|
||||
|
||||
|
3
fittrackee_client/src/types/generic.ts
Normal file
3
fittrackee_client/src/types/generic.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export type genericObject = {
|
||||
[key: string]: string
|
||||
}
|
@ -1,12 +1,3 @@
|
||||
export interface IStatistics {
|
||||
nb_workouts: number
|
||||
total_distance: number
|
||||
total_duration: number
|
||||
}
|
||||
export type TStatistics = {
|
||||
[key in string | number]: IStatistics
|
||||
}
|
||||
|
||||
export interface IStatisticsParams {
|
||||
from: string
|
||||
to: string
|
||||
@ -18,3 +9,37 @@ export interface IUserStatisticsPayload {
|
||||
filterType: string
|
||||
params: IStatisticsParams
|
||||
}
|
||||
|
||||
export interface IStatisticsDateParams {
|
||||
duration: string
|
||||
start: Date
|
||||
end: Date
|
||||
}
|
||||
|
||||
export type TDatasetKeys = 'nb_workouts' | 'total_duration' | 'total_distance'
|
||||
|
||||
export type TStatistics = Record<TDatasetKeys, number>
|
||||
|
||||
export type TSportStatistics = {
|
||||
[key in number]: TStatistics
|
||||
}
|
||||
|
||||
export type TStatisticsFromApi = {
|
||||
[key in string]: TSportStatistics
|
||||
}
|
||||
|
||||
export interface IStatisticsChartDataset {
|
||||
label: string
|
||||
backgroundColor: string[]
|
||||
data: (number | null)[]
|
||||
}
|
||||
|
||||
export type TStatisticsDatasets = Record<
|
||||
TDatasetKeys,
|
||||
IStatisticsChartDataset[]
|
||||
>
|
||||
|
||||
export interface IStatisticsChartData {
|
||||
labels: string[]
|
||||
datasets: TStatisticsDatasets
|
||||
}
|
||||
|
7
fittrackee_client/src/types/workouts.ts
Normal file
7
fittrackee_client/src/types/workouts.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export interface ISport {
|
||||
has_workouts: boolean
|
||||
id: number
|
||||
img: string
|
||||
is_active: boolean
|
||||
label: string
|
||||
}
|
42
fittrackee_client/src/utils/dates.ts
Normal file
42
fittrackee_client/src/utils/dates.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import {
|
||||
addDays,
|
||||
addMonths,
|
||||
addYears,
|
||||
startOfMonth,
|
||||
startOfWeek,
|
||||
startOfYear,
|
||||
} from 'date-fns'
|
||||
|
||||
export const startDate = (
|
||||
duration: string,
|
||||
day: Date,
|
||||
weekStartingMonday: boolean
|
||||
): Date => {
|
||||
switch (duration) {
|
||||
case 'week':
|
||||
return startOfWeek(day, { weekStartsOn: weekStartingMonday ? 1 : 0 })
|
||||
case 'year':
|
||||
return startOfYear(day)
|
||||
case 'month':
|
||||
return startOfMonth(day)
|
||||
default:
|
||||
throw new Error(
|
||||
`Invalid duration, expected: "week", "month", "year", got: "${duration}"`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export const incrementDate = (duration: string, day: Date): Date => {
|
||||
switch (duration) {
|
||||
case 'week':
|
||||
return addDays(day, 7)
|
||||
case 'year':
|
||||
return addYears(day, 1)
|
||||
case 'month':
|
||||
return addMonths(day, 1)
|
||||
default:
|
||||
throw new Error(
|
||||
`Invalid duration, expected: "week", "month", "year", got: "${duration}"`
|
||||
)
|
||||
}
|
||||
}
|
103
fittrackee_client/src/utils/statistics.ts
Normal file
103
fittrackee_client/src/utils/statistics.ts
Normal file
@ -0,0 +1,103 @@
|
||||
import { format } from 'date-fns'
|
||||
|
||||
import { genericObject } from '@/types/generic'
|
||||
import {
|
||||
IStatisticsChartData,
|
||||
IStatisticsChartDataset,
|
||||
IStatisticsDateParams,
|
||||
TDatasetKeys,
|
||||
TStatisticsDatasets,
|
||||
TStatisticsFromApi,
|
||||
} from '@/types/statistics'
|
||||
import { ISport } from '@/types/workouts'
|
||||
import { incrementDate, startDate } from '@/utils/dates'
|
||||
|
||||
// date format from api
|
||||
const dateFormats: genericObject = {
|
||||
week: 'yyyy-MM-dd',
|
||||
month: 'yyyy-MM',
|
||||
year: 'yyyy',
|
||||
}
|
||||
|
||||
const data: TDatasetKeys[] = ['nb_workouts', 'total_duration', 'total_distance']
|
||||
|
||||
export const getDateKeys = (
|
||||
params: IStatisticsDateParams,
|
||||
weekStartingMonday: boolean
|
||||
): Date[] => {
|
||||
const days = []
|
||||
for (
|
||||
let day = startDate(params.duration, params.start, weekStartingMonday);
|
||||
day <= params.end;
|
||||
day = incrementDate(params.duration, day)
|
||||
) {
|
||||
days.push(day)
|
||||
}
|
||||
return days
|
||||
}
|
||||
|
||||
const getStatisticsChartDataset = (
|
||||
sportLabel: string
|
||||
): IStatisticsChartDataset =>
|
||||
Object.assign(
|
||||
{},
|
||||
{
|
||||
label: sportLabel,
|
||||
backgroundColor: [],
|
||||
data: [],
|
||||
}
|
||||
)
|
||||
|
||||
export const getDatasets = (displayedSports: ISport[]): TStatisticsDatasets => {
|
||||
const datasets: TStatisticsDatasets = {
|
||||
nb_workouts: [],
|
||||
total_distance: [],
|
||||
total_duration: [],
|
||||
}
|
||||
displayedSports.map((sport) => {
|
||||
datasets.nb_workouts.push(getStatisticsChartDataset(sport.label))
|
||||
datasets.total_distance.push(getStatisticsChartDataset(sport.label))
|
||||
datasets.total_duration.push(getStatisticsChartDataset(sport.label))
|
||||
})
|
||||
return datasets
|
||||
}
|
||||
|
||||
export const formatStats = (
|
||||
params: IStatisticsDateParams,
|
||||
weekStartingMonday: boolean,
|
||||
sports: ISport[],
|
||||
displayedSportsId: number[],
|
||||
apiStats: TStatisticsFromApi
|
||||
): IStatisticsChartData => {
|
||||
const dayKeys = getDateKeys(params, weekStartingMonday)
|
||||
const dateFormat = dateFormats[params.duration]
|
||||
const displayedSports = sports.filter((sport) =>
|
||||
displayedSportsId.length == 0 ? true : displayedSportsId.includes(sport.id)
|
||||
)
|
||||
const labels: string[] = []
|
||||
const datasets = getDatasets(displayedSports)
|
||||
const sportsId: Record<string, number> = {}
|
||||
displayedSports.map(
|
||||
(displayedSport) => (sportsId[displayedSport.label] = displayedSport.id)
|
||||
)
|
||||
|
||||
dayKeys.map((key) => {
|
||||
const date: string = format(key, dateFormat)
|
||||
labels.push(date)
|
||||
data.map((datasetKey) => {
|
||||
datasets[datasetKey].map((dataset) =>
|
||||
dataset.data.push(
|
||||
apiStats !== {} &&
|
||||
date in apiStats &&
|
||||
sportsId[dataset.label] in apiStats[date]
|
||||
? apiStats[date][sportsId[dataset.label]][datasetKey]
|
||||
: null
|
||||
)
|
||||
)
|
||||
})
|
||||
})
|
||||
return {
|
||||
labels,
|
||||
datasets,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user