Client - init sports store
This commit is contained in:
parent
cac9607489
commit
a09c320093
@ -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,
|
||||
|
@ -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,
|
||||
}
|
||||
|
28
fittrackee_client/src/store/modules/sports/actions.ts
Normal file
28
fittrackee_client/src/store/modules/sports/actions.ts
Normal 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))
|
||||
},
|
||||
}
|
11
fittrackee_client/src/store/modules/sports/enums.ts
Normal file
11
fittrackee_client/src/store/modules/sports/enums.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export enum SportsActions {
|
||||
GET_SPORTS = 'GET_SPORTS',
|
||||
}
|
||||
|
||||
export enum SportsGetters {
|
||||
SPORTS = 'SPORTS',
|
||||
}
|
||||
|
||||
export enum SportsMutation {
|
||||
SET_SPORTS = 'SET_SPORTS',
|
||||
}
|
9
fittrackee_client/src/store/modules/sports/getters.ts
Normal file
9
fittrackee_client/src/store/modules/sports/getters.ts
Normal 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,
|
||||
}
|
17
fittrackee_client/src/store/modules/sports/index.ts
Normal file
17
fittrackee_client/src/store/modules/sports/index.ts
Normal 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
|
11
fittrackee_client/src/store/modules/sports/mutations.ts
Normal file
11
fittrackee_client/src/store/modules/sports/mutations.ts
Normal 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
|
||||
},
|
||||
}
|
5
fittrackee_client/src/store/modules/sports/state.ts
Normal file
5
fittrackee_client/src/store/modules/sports/state.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { ISportsState } from '@/store/modules/sports/types'
|
||||
|
||||
export const sportsState: ISportsState = {
|
||||
sports: [],
|
||||
}
|
52
fittrackee_client/src/store/modules/sports/types.ts
Normal file
52
fittrackee_client/src/store/modules/sports/types.ts
Normal 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]>
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
@ -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'>>
|
||||
|
@ -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 => {
|
||||
|
@ -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
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ISport } from '@/types/workouts'
|
||||
import { ISport } from '@/types/sports'
|
||||
|
||||
export const sports: ISport[] = [
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user