Client - init workouts store

This commit is contained in:
Sam 2021-09-05 10:39:32 +02:00
parent 18908ce89d
commit a85860581f
11 changed files with 245 additions and 1 deletions

View File

@ -18,6 +18,11 @@ import {
UserGetters, UserGetters,
UserMutations, UserMutations,
} from '@/store/modules/user/enums' } from '@/store/modules/user/enums'
import {
WorkoutsActions,
WorkoutsGetters,
WorkoutsMutations,
} from '@/store/modules/workouts/enums'
export const ROOT_STORE = { export const ROOT_STORE = {
ACTIONS: RootActions, ACTIONS: RootActions,
@ -42,3 +47,9 @@ export const USER_STORE = {
GETTERS: UserGetters, GETTERS: UserGetters,
MUTATIONS: UserMutations, MUTATIONS: UserMutations,
} }
export const WORKOUTS_STORE = {
ACTIONS: WorkoutsActions,
GETTERS: WorkoutsGetters,
MUTATIONS: WorkoutsMutations,
}

View File

@ -0,0 +1,36 @@
import { ActionContext, ActionTree } from 'vuex'
import authApi from '@/api/authApi'
import { ROOT_STORE, WORKOUTS_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/types'
import {
IWorkoutsActions,
IWorkoutsState,
} from '@/store/modules/workouts/types'
import { IWorkoutsPayload } from '@/types/workouts'
import { handleError } from '@/utils'
export const actions: ActionTree<IWorkoutsState, IRootState> &
IWorkoutsActions = {
[WORKOUTS_STORE.ACTIONS.GET_CALENDAR_WORKOUTS](
context: ActionContext<IWorkoutsState, IRootState>,
payload: IWorkoutsPayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
.get('workouts', {
params: payload,
})
.then((res) => {
if (res.data.status === 'success') {
context.commit(
WORKOUTS_STORE.MUTATIONS.SET_CALENDAR_WORKOUTS,
res.data.data.statistics
)
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
},
}

View File

@ -0,0 +1,11 @@
export enum WorkoutsActions {
GET_CALENDAR_WORKOUTS = 'GET_CALENDAR_WORKOUTS',
}
export enum WorkoutsGetters {
CALENDAR_WORKOUTS = 'CALENDAR_WORKOUTS',
}
export enum WorkoutsMutations {
SET_CALENDAR_WORKOUTS = 'SET_CALENDAR_WORKOUTS',
}

View File

@ -0,0 +1,15 @@
import { GetterTree } from 'vuex'
import { WORKOUTS_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/types'
import {
IWorkoutsGetters,
IWorkoutsState,
} from '@/store/modules/workouts/types'
export const getters: GetterTree<IWorkoutsState, IRootState> &
IWorkoutsGetters = {
[WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS]: (state: IWorkoutsState) => {
return state.calendar_workouts
},
}

View File

@ -0,0 +1,15 @@
import { Module } from 'vuex'
import { IRootState } from '@/store/modules/root/types'
import { actions } from '@/store/modules/workouts/actions'
import { getters } from '@/store/modules/workouts/getters'
import { mutations } from '@/store/modules/workouts/mutations'
import { workoutsState } from '@/store/modules/workouts/state'
import { IWorkoutsState } from '@/store/modules/workouts/types'
const workouts: Module<IWorkoutsState, IRootState> = {
state: workoutsState,
actions,
getters,
mutations,
}

View File

@ -0,0 +1,17 @@
import { MutationTree } from 'vuex'
import { WORKOUTS_STORE } from '@/store/constants'
import {
IWorkoutsState,
TWorkoutsMutations,
} from '@/store/modules/workouts/types'
import { IWorkout } from '@/types/workouts'
export const mutations: MutationTree<IWorkoutsState> & TWorkoutsMutations = {
[WORKOUTS_STORE.MUTATIONS.SET_CALENDAR_WORKOUTS](
state: IWorkoutsState,
workouts: IWorkout[]
) {
state.calendar_workouts = workouts
},
}

View File

@ -0,0 +1,6 @@
import { IWorkoutsState } from '@/store/modules/workouts/types'
export const workoutsState: IWorkoutsState = {
calendar_workouts: [],
user_workouts: [],
}

View File

@ -0,0 +1,57 @@
import {
ActionContext,
CommitOptions,
DispatchOptions,
Store as VuexStore,
} from 'vuex'
import { WORKOUTS_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/types'
import { IWorkout, IWorkoutsPayload } from '@/types/workouts'
export interface IWorkoutsState {
user_workouts: IWorkout[]
calendar_workouts: IWorkout[]
}
export interface IWorkoutsActions {
[WORKOUTS_STORE.ACTIONS.GET_CALENDAR_WORKOUTS](
context: ActionContext<IWorkoutsState, IRootState>,
payload: IWorkoutsPayload
): void
}
export interface IWorkoutsGetters {
[WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS](state: IWorkoutsState): IWorkout[]
}
export type TWorkoutsMutations<S = IWorkoutsState> = {
[WORKOUTS_STORE.MUTATIONS.SET_CALENDAR_WORKOUTS](
state: S,
workouts: IWorkout[]
): void
}
export type TWorkoutsStoreModule<S = IWorkoutsState> = Omit<
VuexStore<S>,
'commit' | 'getters' | 'dispatch'
> & {
dispatch<K extends keyof IWorkoutsActions>(
key: K,
payload?: Parameters<IWorkoutsActions[K]>[1],
options?: DispatchOptions
): ReturnType<IWorkoutsActions[K]>
} & {
getters: {
[K in keyof IWorkoutsGetters]: ReturnType<IWorkoutsGetters[K]>
}
} & {
commit<
K extends keyof TWorkoutsMutations,
P extends Parameters<TWorkoutsMutations[K]>[1]
>(
key: K,
payload?: P,
options?: CommitOptions
): ReturnType<TWorkoutsMutations[K]>
}

View File

@ -2,15 +2,18 @@ import { TRootStoreModule } from '@/store/modules/root/types'
import { TSportsStoreModule } from '@/store/modules/sports/types' import { TSportsStoreModule } from '@/store/modules/sports/types'
import { TStatisticsStoreModule } from '@/store/modules/statistics/types' import { TStatisticsStoreModule } from '@/store/modules/statistics/types'
import { TUserStoreModule } from '@/store/modules/user/types' import { TUserStoreModule } from '@/store/modules/user/types'
import { TWorkoutsStoreModule } from '@/store/modules/workouts/types'
type StoreModules = { type StoreModules = {
rootModule: TRootStoreModule rootModule: TRootStoreModule
sportsModule: TSportsStoreModule sportsModule: TSportsStoreModule
statsModule: TStatisticsStoreModule statsModule: TStatisticsStoreModule
userModule: TUserStoreModule userModule: TUserStoreModule
workoutsModule: TWorkoutsStoreModule
} }
export type Store = TUserStoreModule<Pick<StoreModules, 'userModule'>> & export type Store = TUserStoreModule<Pick<StoreModules, 'userModule'>> &
TSportsStoreModule<Pick<StoreModules, 'sportsModule'>> & TSportsStoreModule<Pick<StoreModules, 'sportsModule'>> &
TStatisticsStoreModule<Pick<StoreModules, 'statsModule'>> & TStatisticsStoreModule<Pick<StoreModules, 'statsModule'>> &
TWorkoutsStoreModule<Pick<StoreModules, 'workoutsModule'>> &
TRootStoreModule<Pick<StoreModules, 'rootModule'>> TRootStoreModule<Pick<StoreModules, 'rootModule'>>

View File

@ -0,0 +1,71 @@
import { IStatisticsParams } from '@/types/statistics'
export interface IWorkoutSegment {
ascent: number
ave_speed: number
descent: number
distance: number
duration: string
max_alt: number
max_speed: number
min_alt: number
moving: string
pauses: string
segment_id: number
workout_id: string
}
export interface IRecord {
id: number
record_type: string
sport_id: number
user: string
value: number
workout_date: string
workout_id: string
}
export interface IWeather {
humidity: number
icon: string
summary: string
temperature: number
wind: number
}
export interface IWorkout {
ascent: number | null
ave_speed: number
bounds: number[]
creation_date: string
descent: number | null
distance: number
duration: string
id: string
map: string | null
max_alt: number | null
max_speed: number
min_alt: number | null
modification_date: string | null
moving: string
next_workout: string | null
notes: string
pauses: string | null
previous_workout: string | null
records: IRecord[]
segments: IWorkoutSegment[]
sport_id: number
title: string
user: string
weather_end: IWeather | null
weather_start: IWeather | null
with_gpx: boolean
workout_date: string
}
export interface IWorkoutsPayload {
from: string
to: string
order: string
per_page: number
}

View File

@ -6,6 +6,7 @@ import { IRootState } from '@/store/modules/root/types'
import { ISportsState } from '@/store/modules/sports/types' import { ISportsState } from '@/store/modules/sports/types'
import { IStatisticsState } from '@/store/modules/statistics/types' import { IStatisticsState } from '@/store/modules/statistics/types'
import { IUserState } from '@/store/modules/user/types' import { IUserState } from '@/store/modules/user/types'
import { IWorkoutsState } from '@/store/modules/workouts/types'
export const getApiUrl = (): string => { export const getApiUrl = (): string => {
return process.env.NODE_ENV === 'production' return process.env.NODE_ENV === 'production'
@ -21,7 +22,8 @@ export const handleError = (
| ActionContext<IRootState, IRootState> | ActionContext<IRootState, IRootState>
| ActionContext<IUserState, IRootState> | ActionContext<IUserState, IRootState>
| ActionContext<IStatisticsState, IRootState> | ActionContext<IStatisticsState, IRootState>
| ActionContext<ISportsState, IRootState>, | ActionContext<ISportsState, IRootState>
| ActionContext<IWorkoutsState, IRootState>,
error: AxiosError | null, error: AxiosError | null,
msg = 'UNKNOWN' msg = 'UNKNOWN'
): void => { ): void => {