From a09c320093fbfc1abb7a65bbf79b24199795fe21 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 22 Aug 2021 21:10:47 +0200 Subject: [PATCH] Client - init sports store --- fittrackee_client/src/store/constants.ts | 11 ++++ .../src/store/modules/root/index.ts | 2 + .../src/store/modules/sports/actions.ts | 28 ++++++++++ .../src/store/modules/sports/enums.ts | 11 ++++ .../src/store/modules/sports/getters.ts | 9 ++++ .../src/store/modules/sports/index.ts | 17 ++++++ .../src/store/modules/sports/mutations.ts | 11 ++++ .../src/store/modules/sports/state.ts | 5 ++ .../src/store/modules/sports/types.ts | 52 +++++++++++++++++++ .../src/store/modules/user/actions.ts | 3 +- fittrackee_client/src/store/types.ts | 3 ++ .../src/types/{workouts.ts => sports.ts} | 0 fittrackee_client/src/utils/index.ts | 4 +- fittrackee_client/src/utils/statistics.ts | 2 +- .../tests/unit/utils/constants.ts | 2 +- 15 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 fittrackee_client/src/store/modules/sports/actions.ts create mode 100644 fittrackee_client/src/store/modules/sports/enums.ts create mode 100644 fittrackee_client/src/store/modules/sports/getters.ts create mode 100644 fittrackee_client/src/store/modules/sports/index.ts create mode 100644 fittrackee_client/src/store/modules/sports/mutations.ts create mode 100644 fittrackee_client/src/store/modules/sports/state.ts create mode 100644 fittrackee_client/src/store/modules/sports/types.ts rename fittrackee_client/src/types/{workouts.ts => sports.ts} (100%) diff --git a/fittrackee_client/src/store/constants.ts b/fittrackee_client/src/store/constants.ts index 1ca46c05..1989345d 100644 --- a/fittrackee_client/src/store/constants.ts +++ b/fittrackee_client/src/store/constants.ts @@ -3,6 +3,11 @@ import { RootGetters, RootMutations, } from '@/store/modules/root/enums' +import { + SportsActions, + SportsGetters, + SportsMutation, +} from '@/store/modules/sports/enums' import { StatisticsActions, StatisticsGetters, @@ -20,6 +25,12 @@ export const ROOT_STORE = { MUTATIONS: RootMutations, } +export const SPORTS_STORE = { + ACTIONS: SportsActions, + GETTERS: SportsGetters, + MUTATIONS: SportsMutation, +} + export const STATS_STORE = { ACTIONS: StatisticsActions, GETTERS: StatisticsGetters, diff --git a/fittrackee_client/src/store/modules/root/index.ts b/fittrackee_client/src/store/modules/root/index.ts index 5087d7c7..f05d6a88 100644 --- a/fittrackee_client/src/store/modules/root/index.ts +++ b/fittrackee_client/src/store/modules/root/index.ts @@ -5,10 +5,12 @@ import { getters } from '@/store/modules/root/getters' import { mutations } from '@/store/modules/root/mutations' import { state } from '@/store/modules/root/state.ts' import { IRootState } from '@/store/modules/root/types' +import sportsModule from '@/store/modules/sports' import statsModule from '@/store/modules/statistics' import userModule from '@/store/modules/user' const modules: ModuleTree = { + sportsModule, statsModule, userModule, } diff --git a/fittrackee_client/src/store/modules/sports/actions.ts b/fittrackee_client/src/store/modules/sports/actions.ts new file mode 100644 index 00000000..97e483db --- /dev/null +++ b/fittrackee_client/src/store/modules/sports/actions.ts @@ -0,0 +1,28 @@ +import { ActionContext, ActionTree } from 'vuex' + +import authApi from '@/api/authApi' +import { ROOT_STORE, SPORTS_STORE } from '@/store/constants' +import { IRootState } from '@/store/modules/root/types' +import { ISportsActions, ISportsState } from '@/store/modules/sports/types' +import { handleError } from '@/utils' + +export const actions: ActionTree & ISportsActions = { + [SPORTS_STORE.ACTIONS.GET_SPORTS]( + context: ActionContext + ): void { + context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES) + authApi + .get('sports') + .then((res) => { + if (res.data.status === 'success') { + context.commit( + SPORTS_STORE.MUTATIONS.SET_SPORTS, + res.data.data.sports + ) + } else { + handleError(context, null) + } + }) + .catch((error) => handleError(context, error)) + }, +} diff --git a/fittrackee_client/src/store/modules/sports/enums.ts b/fittrackee_client/src/store/modules/sports/enums.ts new file mode 100644 index 00000000..cbd15c5a --- /dev/null +++ b/fittrackee_client/src/store/modules/sports/enums.ts @@ -0,0 +1,11 @@ +export enum SportsActions { + GET_SPORTS = 'GET_SPORTS', +} + +export enum SportsGetters { + SPORTS = 'SPORTS', +} + +export enum SportsMutation { + SET_SPORTS = 'SET_SPORTS', +} diff --git a/fittrackee_client/src/store/modules/sports/getters.ts b/fittrackee_client/src/store/modules/sports/getters.ts new file mode 100644 index 00000000..db8b8947 --- /dev/null +++ b/fittrackee_client/src/store/modules/sports/getters.ts @@ -0,0 +1,9 @@ +import { GetterTree } from 'vuex' + +import { SPORTS_STORE } from '@/store/constants' +import { IRootState } from '@/store/modules/root/types' +import { ISportsGetters, ISportsState } from '@/store/modules/sports/types' + +export const getters: GetterTree & ISportsGetters = { + [SPORTS_STORE.GETTERS.SPORTS]: (state: ISportsState) => state.sports, +} diff --git a/fittrackee_client/src/store/modules/sports/index.ts b/fittrackee_client/src/store/modules/sports/index.ts new file mode 100644 index 00000000..038a3248 --- /dev/null +++ b/fittrackee_client/src/store/modules/sports/index.ts @@ -0,0 +1,17 @@ +import { Module } from 'vuex' + +import { IRootState } from '@/store/modules/root/types' +import { actions } from '@/store/modules/sports/actions' +import { getters } from '@/store/modules/sports/getters' +import { mutations } from '@/store/modules/sports/mutations' +import { sportsState } from '@/store/modules/sports/state' +import { ISportsState } from '@/store/modules/sports/types' + +const sports: Module = { + state: sportsState, + actions, + getters, + mutations, +} + +export default sports diff --git a/fittrackee_client/src/store/modules/sports/mutations.ts b/fittrackee_client/src/store/modules/sports/mutations.ts new file mode 100644 index 00000000..61805962 --- /dev/null +++ b/fittrackee_client/src/store/modules/sports/mutations.ts @@ -0,0 +1,11 @@ +import { MutationTree } from 'vuex' + +import { SPORTS_STORE } from '@/store/constants' +import { ISportsState, TSportsMutations } from '@/store/modules/sports/types' +import { ISport } from '@/types/sports' + +export const mutations: MutationTree & TSportsMutations = { + [SPORTS_STORE.MUTATIONS.SET_SPORTS](state: ISportsState, sports: ISport[]) { + state.sports = sports + }, +} diff --git a/fittrackee_client/src/store/modules/sports/state.ts b/fittrackee_client/src/store/modules/sports/state.ts new file mode 100644 index 00000000..65d273ba --- /dev/null +++ b/fittrackee_client/src/store/modules/sports/state.ts @@ -0,0 +1,5 @@ +import { ISportsState } from '@/store/modules/sports/types' + +export const sportsState: ISportsState = { + sports: [], +} diff --git a/fittrackee_client/src/store/modules/sports/types.ts b/fittrackee_client/src/store/modules/sports/types.ts new file mode 100644 index 00000000..4603116d --- /dev/null +++ b/fittrackee_client/src/store/modules/sports/types.ts @@ -0,0 +1,52 @@ +import { + ActionContext, + CommitOptions, + DispatchOptions, + Store as VuexStore, +} from 'vuex' + +import { SPORTS_STORE } from '@/store/constants' +import { IRootState } from '@/store/modules/root/types' +import { ISport } from '@/types/sports' + +export interface ISportsState { + sports: ISport[] +} + +export interface ISportsActions { + [SPORTS_STORE.ACTIONS.GET_SPORTS]( + context: ActionContext + ): void +} + +export interface ISportsGetters { + [SPORTS_STORE.GETTERS.SPORTS](state: ISportsState): ISport[] +} + +export type TSportsMutations = { + [SPORTS_STORE.MUTATIONS.SET_SPORTS](state: S, sports: ISport[]): void +} + +export type TSportsStoreModule = Omit< + VuexStore, + 'commit' | 'getters' | 'dispatch' +> & { + dispatch( + key: K, + payload?: Parameters[1], + options?: DispatchOptions + ): ReturnType +} & { + getters: { + [K in keyof ISportsGetters]: ReturnType + } +} & { + commit< + K extends keyof TSportsMutations, + P extends Parameters[1] + >( + key: K, + payload?: P, + options?: CommitOptions + ): ReturnType +} diff --git a/fittrackee_client/src/store/modules/user/actions.ts b/fittrackee_client/src/store/modules/user/actions.ts index 74650ecf..bf04feb4 100644 --- a/fittrackee_client/src/store/modules/user/actions.ts +++ b/fittrackee_client/src/store/modules/user/actions.ts @@ -3,7 +3,7 @@ import { ActionContext, ActionTree } from 'vuex' import authApi from '@/api/authApi' import api from '@/api/defaultApi' import router from '@/router' -import { ROOT_STORE, USER_STORE } from '@/store/constants' +import { ROOT_STORE, SPORTS_STORE, USER_STORE } from '@/store/constants' import { IRootState } from '@/store/modules/root/types' import { IUserActions, IUserState } from '@/store/modules/user/types' import { ILoginOrRegisterData } from '@/types/user' @@ -36,6 +36,7 @@ export const actions: ActionTree & IUserActions = { USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE, res.data.data ) + context.dispatch(SPORTS_STORE.ACTIONS.GET_SPORTS) } else { handleError(context, null) } diff --git a/fittrackee_client/src/store/types.ts b/fittrackee_client/src/store/types.ts index d2828029..02ba298e 100644 --- a/fittrackee_client/src/store/types.ts +++ b/fittrackee_client/src/store/types.ts @@ -1,13 +1,16 @@ 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' type StoreModules = { rootModule: TRootStoreModule + sportsModule: TSportsStoreModule statsModule: TStatisticsStoreModule userModule: TUserStoreModule } export type Store = TUserStoreModule> & + TSportsStoreModule> & TStatisticsStoreModule> & TRootStoreModule> diff --git a/fittrackee_client/src/types/workouts.ts b/fittrackee_client/src/types/sports.ts similarity index 100% rename from fittrackee_client/src/types/workouts.ts rename to fittrackee_client/src/types/sports.ts diff --git a/fittrackee_client/src/utils/index.ts b/fittrackee_client/src/utils/index.ts index abdbfc61..8641742e 100644 --- a/fittrackee_client/src/utils/index.ts +++ b/fittrackee_client/src/utils/index.ts @@ -3,6 +3,7 @@ import { ActionContext } from 'vuex' import { ROOT_STORE } from '@/store/constants' 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' @@ -19,7 +20,8 @@ export const handleError = ( context: | ActionContext | ActionContext - | ActionContext, + | ActionContext + | ActionContext, error: AxiosError | null, msg = 'UNKNOWN' ): void => { diff --git a/fittrackee_client/src/utils/statistics.ts b/fittrackee_client/src/utils/statistics.ts index 25ed148f..83540e2e 100644 --- a/fittrackee_client/src/utils/statistics.ts +++ b/fittrackee_client/src/utils/statistics.ts @@ -1,6 +1,7 @@ import { format } from 'date-fns' import { genericObject } from '@/types/generic' +import { ISport } from '@/types/sports' import { IStatisticsChartData, IStatisticsChartDataset, @@ -9,7 +10,6 @@ import { TStatisticsDatasets, TStatisticsFromApi, } from '@/types/statistics' -import { ISport } from '@/types/workouts' import { incrementDate, startDate } from '@/utils/dates' // date format from api diff --git a/fittrackee_client/tests/unit/utils/constants.ts b/fittrackee_client/tests/unit/utils/constants.ts index 95cbc74f..55b6bc30 100644 --- a/fittrackee_client/tests/unit/utils/constants.ts +++ b/fittrackee_client/tests/unit/utils/constants.ts @@ -1,4 +1,4 @@ -import { ISport } from '@/types/workouts' +import { ISport } from '@/types/sports' export const sports: ISport[] = [ {