Client - init sports store

This commit is contained in:
Sam 2021-08-22 21:10:47 +02:00
parent cac9607489
commit a09c320093
15 changed files with 156 additions and 4 deletions

View File

@ -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,

View File

@ -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<IRootState> = {
sportsModule,
statsModule,
userModule,
}

View File

@ -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<ISportsState, IRootState> & ISportsActions = {
[SPORTS_STORE.ACTIONS.GET_SPORTS](
context: ActionContext<ISportsState, IRootState>
): 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))
},
}

View File

@ -0,0 +1,11 @@
export enum SportsActions {
GET_SPORTS = 'GET_SPORTS',
}
export enum SportsGetters {
SPORTS = 'SPORTS',
}
export enum SportsMutation {
SET_SPORTS = 'SET_SPORTS',
}

View File

@ -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<ISportsState, IRootState> & ISportsGetters = {
[SPORTS_STORE.GETTERS.SPORTS]: (state: ISportsState) => state.sports,
}

View File

@ -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<ISportsState, IRootState> = {
state: sportsState,
actions,
getters,
mutations,
}
export default sports

View File

@ -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<ISportsState> & TSportsMutations = {
[SPORTS_STORE.MUTATIONS.SET_SPORTS](state: ISportsState, sports: ISport[]) {
state.sports = sports
},
}

View File

@ -0,0 +1,5 @@
import { ISportsState } from '@/store/modules/sports/types'
export const sportsState: ISportsState = {
sports: [],
}

View File

@ -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<ISportsState, IRootState>
): void
}
export interface ISportsGetters {
[SPORTS_STORE.GETTERS.SPORTS](state: ISportsState): ISport[]
}
export type TSportsMutations<S = ISportsState> = {
[SPORTS_STORE.MUTATIONS.SET_SPORTS](state: S, sports: ISport[]): void
}
export type TSportsStoreModule<S = ISportsState> = Omit<
VuexStore<S>,
'commit' | 'getters' | 'dispatch'
> & {
dispatch<K extends keyof ISportsActions>(
key: K,
payload?: Parameters<ISportsActions[K]>[1],
options?: DispatchOptions
): ReturnType<ISportsActions[K]>
} & {
getters: {
[K in keyof ISportsGetters]: ReturnType<ISportsGetters[K]>
}
} & {
commit<
K extends keyof TSportsMutations,
P extends Parameters<TSportsMutations[K]>[1]
>(
key: K,
payload?: P,
options?: CommitOptions
): ReturnType<TSportsMutations[K]>
}

View File

@ -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<IUserState, IRootState> & IUserActions = {
USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE,
res.data.data
)
context.dispatch(SPORTS_STORE.ACTIONS.GET_SPORTS)
} else {
handleError(context, null)
}

View File

@ -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<Pick<StoreModules, 'userModule'>> &
TSportsStoreModule<Pick<StoreModules, 'sportsModule'>> &
TStatisticsStoreModule<Pick<StoreModules, 'statsModule'>> &
TRootStoreModule<Pick<StoreModules, 'rootModule'>>

View File

@ -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<IRootState, IRootState>
| ActionContext<IUserState, IRootState>
| ActionContext<IStatisticsState, IRootState>,
| ActionContext<IStatisticsState, IRootState>
| ActionContext<ISportsState, IRootState>,
error: AxiosError | null,
msg = 'UNKNOWN'
): void => {

View File

@ -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

View File

@ -1,4 +1,4 @@
import { ISport } from '@/types/workouts'
import { ISport } from '@/types/sports'
export const sports: ISport[] = [
{