Client - init store for users workouts displayed in timeline

This commit is contained in:
Sam 2021-09-08 18:56:16 +02:00
parent 03241cfe83
commit 5ab293b9e7
6 changed files with 53 additions and 18 deletions

View File

@ -10,27 +10,43 @@ import {
import { IWorkoutsPayload } from '@/types/workouts' import { IWorkoutsPayload } from '@/types/workouts'
import { handleError } from '@/utils' import { handleError } from '@/utils'
const getWorkouts = (
context: ActionContext<IWorkoutsState, IRootState>,
payload: IWorkoutsPayload,
target: string
): void => {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
.get('workouts', {
params: payload,
})
.then((res) => {
if (res.data.status === 'success') {
context.commit(
target === 'CALENDAR_WORKOUTS'
? WORKOUTS_STORE.MUTATIONS.SET_CALENDAR_WORKOUTS
: WORKOUTS_STORE.MUTATIONS.SET_USER_WORKOUTS,
res.data.data.workouts
)
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
}
export const actions: ActionTree<IWorkoutsState, IRootState> & export const actions: ActionTree<IWorkoutsState, IRootState> &
IWorkoutsActions = { IWorkoutsActions = {
[WORKOUTS_STORE.ACTIONS.GET_CALENDAR_WORKOUTS]( [WORKOUTS_STORE.ACTIONS.GET_CALENDAR_WORKOUTS](
context: ActionContext<IWorkoutsState, IRootState>, context: ActionContext<IWorkoutsState, IRootState>,
payload: IWorkoutsPayload payload: IWorkoutsPayload
): void { ): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES) getWorkouts(context, payload, 'CALENDAR_WORKOUTS')
authApi },
.get('workouts', { [WORKOUTS_STORE.ACTIONS.GET_USER_WORKOUTS](
params: payload, context: ActionContext<IWorkoutsState, IRootState>,
}) payload: IWorkoutsPayload
.then((res) => { ): void {
if (res.data.status === 'success') { getWorkouts(context, payload, 'USER_WORKOUTS')
context.commit(
WORKOUTS_STORE.MUTATIONS.SET_CALENDAR_WORKOUTS,
res.data.data.workouts
)
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
}, },
} }

View File

@ -1,11 +1,14 @@
export enum WorkoutsActions { export enum WorkoutsActions {
GET_CALENDAR_WORKOUTS = 'GET_CALENDAR_WORKOUTS', GET_CALENDAR_WORKOUTS = 'GET_CALENDAR_WORKOUTS',
GET_USER_WORKOUTS = 'GET_USER_WORKOUTS',
} }
export enum WorkoutsGetters { export enum WorkoutsGetters {
CALENDAR_WORKOUTS = 'CALENDAR_WORKOUTS', CALENDAR_WORKOUTS = 'CALENDAR_WORKOUTS',
USER_WORKOUTS = 'USER_WORKOUTS',
} }
export enum WorkoutsMutations { export enum WorkoutsMutations {
SET_CALENDAR_WORKOUTS = 'SET_CALENDAR_WORKOUTS', SET_CALENDAR_WORKOUTS = 'SET_CALENDAR_WORKOUTS',
SET_USER_WORKOUTS = 'SET_USER_WORKOUTS',
} }

View File

@ -12,4 +12,7 @@ export const getters: GetterTree<IWorkoutsState, IRootState> &
[WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS]: (state: IWorkoutsState) => { [WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS]: (state: IWorkoutsState) => {
return state.calendar_workouts return state.calendar_workouts
}, },
[WORKOUTS_STORE.GETTERS.USER_WORKOUTS]: (state: IWorkoutsState) => {
return state.user_workouts
},
} }

View File

@ -14,4 +14,10 @@ export const mutations: MutationTree<IWorkoutsState> & TWorkoutsMutations = {
) { ) {
state.calendar_workouts = workouts state.calendar_workouts = workouts
}, },
[WORKOUTS_STORE.MUTATIONS.SET_USER_WORKOUTS](
state: IWorkoutsState,
workouts: IWorkout[]
) {
state.user_workouts = workouts
},
} }

View File

@ -19,10 +19,15 @@ export interface IWorkoutsActions {
context: ActionContext<IWorkoutsState, IRootState>, context: ActionContext<IWorkoutsState, IRootState>,
payload: IWorkoutsPayload payload: IWorkoutsPayload
): void ): void
[WORKOUTS_STORE.ACTIONS.GET_USER_WORKOUTS](
context: ActionContext<IWorkoutsState, IRootState>,
payload: IWorkoutsPayload
): void
} }
export interface IWorkoutsGetters { export interface IWorkoutsGetters {
[WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS](state: IWorkoutsState): IWorkout[] [WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS](state: IWorkoutsState): IWorkout[]
[WORKOUTS_STORE.GETTERS.USER_WORKOUTS](state: IWorkoutsState): IWorkout[]
} }
export type TWorkoutsMutations<S = IWorkoutsState> = { export type TWorkoutsMutations<S = IWorkoutsState> = {
@ -30,6 +35,10 @@ export type TWorkoutsMutations<S = IWorkoutsState> = {
state: S, state: S,
workouts: IWorkout[] workouts: IWorkout[]
): void ): void
[WORKOUTS_STORE.MUTATIONS.SET_USER_WORKOUTS](
state: S,
workouts: IWorkout[]
): void
} }
export type TWorkoutsStoreModule<S = IWorkoutsState> = Omit< export type TWorkoutsStoreModule<S = IWorkoutsState> = Omit<

View File

@ -1,5 +1,3 @@
import { IStatisticsParams } from '@/types/statistics'
export interface IWorkoutSegment { export interface IWorkoutSegment {
ascent: number ascent: number
ave_speed: number ave_speed: number