diff --git a/fittrackee_client/src/store/constants.ts b/fittrackee_client/src/store/constants.ts index 1989345d..3e1da6e4 100644 --- a/fittrackee_client/src/store/constants.ts +++ b/fittrackee_client/src/store/constants.ts @@ -18,6 +18,11 @@ import { UserGetters, UserMutations, } from '@/store/modules/user/enums' +import { + WorkoutsActions, + WorkoutsGetters, + WorkoutsMutations, +} from '@/store/modules/workouts/enums' export const ROOT_STORE = { ACTIONS: RootActions, @@ -42,3 +47,9 @@ export const USER_STORE = { GETTERS: UserGetters, MUTATIONS: UserMutations, } + +export const WORKOUTS_STORE = { + ACTIONS: WorkoutsActions, + GETTERS: WorkoutsGetters, + MUTATIONS: WorkoutsMutations, +} diff --git a/fittrackee_client/src/store/modules/workouts/actions.ts b/fittrackee_client/src/store/modules/workouts/actions.ts new file mode 100644 index 00000000..92050567 --- /dev/null +++ b/fittrackee_client/src/store/modules/workouts/actions.ts @@ -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 & + IWorkoutsActions = { + [WORKOUTS_STORE.ACTIONS.GET_CALENDAR_WORKOUTS]( + context: ActionContext, + 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)) + }, +} diff --git a/fittrackee_client/src/store/modules/workouts/enums.ts b/fittrackee_client/src/store/modules/workouts/enums.ts new file mode 100644 index 00000000..209d29a6 --- /dev/null +++ b/fittrackee_client/src/store/modules/workouts/enums.ts @@ -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', +} diff --git a/fittrackee_client/src/store/modules/workouts/getters.ts b/fittrackee_client/src/store/modules/workouts/getters.ts new file mode 100644 index 00000000..7a27b1ee --- /dev/null +++ b/fittrackee_client/src/store/modules/workouts/getters.ts @@ -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 & + IWorkoutsGetters = { + [WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS]: (state: IWorkoutsState) => { + return state.calendar_workouts + }, +} diff --git a/fittrackee_client/src/store/modules/workouts/index.ts b/fittrackee_client/src/store/modules/workouts/index.ts new file mode 100644 index 00000000..4fe77499 --- /dev/null +++ b/fittrackee_client/src/store/modules/workouts/index.ts @@ -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 = { + state: workoutsState, + actions, + getters, + mutations, +} diff --git a/fittrackee_client/src/store/modules/workouts/mutations.ts b/fittrackee_client/src/store/modules/workouts/mutations.ts new file mode 100644 index 00000000..d59cd5ff --- /dev/null +++ b/fittrackee_client/src/store/modules/workouts/mutations.ts @@ -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 & TWorkoutsMutations = { + [WORKOUTS_STORE.MUTATIONS.SET_CALENDAR_WORKOUTS]( + state: IWorkoutsState, + workouts: IWorkout[] + ) { + state.calendar_workouts = workouts + }, +} diff --git a/fittrackee_client/src/store/modules/workouts/state.ts b/fittrackee_client/src/store/modules/workouts/state.ts new file mode 100644 index 00000000..7898b295 --- /dev/null +++ b/fittrackee_client/src/store/modules/workouts/state.ts @@ -0,0 +1,6 @@ +import { IWorkoutsState } from '@/store/modules/workouts/types' + +export const workoutsState: IWorkoutsState = { + calendar_workouts: [], + user_workouts: [], +} diff --git a/fittrackee_client/src/store/modules/workouts/types.ts b/fittrackee_client/src/store/modules/workouts/types.ts new file mode 100644 index 00000000..83a85e20 --- /dev/null +++ b/fittrackee_client/src/store/modules/workouts/types.ts @@ -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, + payload: IWorkoutsPayload + ): void +} + +export interface IWorkoutsGetters { + [WORKOUTS_STORE.GETTERS.CALENDAR_WORKOUTS](state: IWorkoutsState): IWorkout[] +} + +export type TWorkoutsMutations = { + [WORKOUTS_STORE.MUTATIONS.SET_CALENDAR_WORKOUTS]( + state: S, + workouts: IWorkout[] + ): void +} + +export type TWorkoutsStoreModule = Omit< + VuexStore, + 'commit' | 'getters' | 'dispatch' +> & { + dispatch( + key: K, + payload?: Parameters[1], + options?: DispatchOptions + ): ReturnType +} & { + getters: { + [K in keyof IWorkoutsGetters]: ReturnType + } +} & { + commit< + K extends keyof TWorkoutsMutations, + P extends Parameters[1] + >( + key: K, + payload?: P, + options?: CommitOptions + ): ReturnType +} diff --git a/fittrackee_client/src/store/types.ts b/fittrackee_client/src/store/types.ts index 02ba298e..263dd40c 100644 --- a/fittrackee_client/src/store/types.ts +++ b/fittrackee_client/src/store/types.ts @@ -2,15 +2,18 @@ import { TRootStoreModule } from '@/store/modules/root/types' import { TSportsStoreModule } from '@/store/modules/sports/types' import { TStatisticsStoreModule } from '@/store/modules/statistics/types' import { TUserStoreModule } from '@/store/modules/user/types' +import { TWorkoutsStoreModule } from '@/store/modules/workouts/types' type StoreModules = { rootModule: TRootStoreModule sportsModule: TSportsStoreModule statsModule: TStatisticsStoreModule userModule: TUserStoreModule + workoutsModule: TWorkoutsStoreModule } export type Store = TUserStoreModule> & TSportsStoreModule> & TStatisticsStoreModule> & + TWorkoutsStoreModule> & TRootStoreModule> diff --git a/fittrackee_client/src/types/workouts.ts b/fittrackee_client/src/types/workouts.ts new file mode 100644 index 00000000..a2f3d9f9 --- /dev/null +++ b/fittrackee_client/src/types/workouts.ts @@ -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 +} diff --git a/fittrackee_client/src/utils/index.ts b/fittrackee_client/src/utils/index.ts index 8641742e..0edf17fa 100644 --- a/fittrackee_client/src/utils/index.ts +++ b/fittrackee_client/src/utils/index.ts @@ -6,6 +6,7 @@ import { IRootState } from '@/store/modules/root/types' import { ISportsState } from '@/store/modules/sports/types' import { IStatisticsState } from '@/store/modules/statistics/types' import { IUserState } from '@/store/modules/user/types' +import { IWorkoutsState } from '@/store/modules/workouts/types' export const getApiUrl = (): string => { return process.env.NODE_ENV === 'production' @@ -21,7 +22,8 @@ export const handleError = ( | ActionContext | ActionContext | ActionContext - | ActionContext, + | ActionContext + | ActionContext, error: AxiosError | null, msg = 'UNKNOWN' ): void => {