Client - init statistics store

This commit is contained in:
Sam 2021-08-21 21:13:16 +02:00
parent 6f6291fb2b
commit 0c516b9986
14 changed files with 200 additions and 2 deletions

View File

@ -3,6 +3,11 @@ import {
RootGetters,
RootMutations,
} from '@/store/modules/root/enums'
import {
StatisticsActions,
StatisticsGetters,
StatisticsMutations,
} from '@/store/modules/statistics/enums'
import {
UserActions,
UserGetters,
@ -15,6 +20,12 @@ export const ROOT_STORE = {
MUTATIONS: RootMutations,
}
export const STATS_STORE = {
ACTIONS: StatisticsActions,
GETTERS: StatisticsGetters,
MUTATIONS: StatisticsMutations,
}
export const USER_STORE = {
ACTIONS: UserActions,
GETTERS: UserGetters,

View File

@ -5,9 +5,11 @@ import { getters } from '@/store/modules/root/getters'
import { IRootState } from '@/store/modules/root/interfaces'
import { mutations } from '@/store/modules/root/mutations'
import { state } from '@/store/modules/root/state.ts'
import statsModule from '@/store/modules/statistics'
import userModule from '@/store/modules/user'
const modules: ModuleTree<IRootState> = {
statsModule,
userModule,
}

View File

@ -0,0 +1,36 @@
import { ActionContext, ActionTree } from 'vuex'
import authApi from '@/api/authApi'
import { STATS_STORE, ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import {
IStatisticsActions,
IStatisticsState,
} from '@/store/modules/statistics/interfaces'
import { IUserStatisticsPayload } from '@/types/statistics'
import { handleError } from '@/utils'
export const actions: ActionTree<IStatisticsState, IRootState> &
IStatisticsActions = {
[STATS_STORE.ACTIONS.GET_USER_STATS](
context: ActionContext<IStatisticsState, IRootState>,
payload: IUserStatisticsPayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
.get(`stats/${payload.username}/${payload.filterType}`, {
params: payload.params,
})
.then((res) => {
if (res.data.status === 'success') {
context.commit(
STATS_STORE.MUTATIONS.UPDATE_USER_STATS,
res.data.data.statistics
)
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
},
}

View File

@ -0,0 +1,11 @@
export enum StatisticsActions {
GET_USER_STATS = 'GET_USER_STATS',
}
export enum StatisticsGetters {
USER_STATS = 'USER_STATS',
}
export enum StatisticsMutations {
UPDATE_USER_STATS = 'UPDATE_USER_STATS',
}

View File

@ -0,0 +1,15 @@
import { GetterTree } from 'vuex'
import { STATS_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import {
IStatisticsGetters,
IStatisticsState,
} from '@/store/modules/statistics/interfaces'
export const getters: GetterTree<IStatisticsState, IRootState> &
IStatisticsGetters = {
[STATS_STORE.GETTERS.USER_STATS]: (state: IStatisticsState) => {
return state.statistics
},
}

View File

@ -0,0 +1,17 @@
import { Module } from 'vuex'
import { IRootState } from '@/store/modules/root/interfaces'
import { actions } from '@/store/modules/statistics/actions'
import { getters } from '@/store/modules/statistics/getters'
import { IStatisticsState } from '@/store/modules/statistics/interfaces'
import { mutations } from '@/store/modules/statistics/mutations'
import { statisticsState } from '@/store/modules/statistics/state'
const statistics: Module<IStatisticsState, IRootState> = {
state: statisticsState,
actions,
getters,
mutations,
}
export default statistics

View File

@ -0,0 +1,20 @@
import { ActionContext } from 'vuex'
import { STATS_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { IUserStatisticsPayload, TStatistics } from '@/types/statistics'
export interface IStatisticsState {
statistics: TStatistics
}
export interface IStatisticsActions {
[STATS_STORE.ACTIONS.GET_USER_STATS](
context: ActionContext<IStatisticsState, IRootState>,
payload: IUserStatisticsPayload
): void
}
export interface IStatisticsGetters {
[STATS_STORE.GETTERS.USER_STATS](state: IStatisticsState): TStatistics
}

View File

@ -0,0 +1,16 @@
import { MutationTree } from 'vuex'
import { STATS_STORE } from '@/store/constants'
import { IStatisticsState } from '@/store/modules/statistics/interfaces'
import { TStatisticsMutations } from '@/store/modules/statistics/types'
import { TStatistics } from '@/types/statistics'
export const mutations: MutationTree<IStatisticsState> & TStatisticsMutations =
{
[STATS_STORE.MUTATIONS.UPDATE_USER_STATS](
state: IStatisticsState,
statistics: TStatistics
) {
state.statistics = statistics
},
}

View File

@ -0,0 +1,6 @@
import { IStatisticsState } from '@/store/modules/statistics/interfaces'
import { TStatistics } from '@/types/statistics'
export const statisticsState: IStatisticsState = {
statistics: <TStatistics>{},
}

View File

@ -0,0 +1,40 @@
import { Store as VuexStore, CommitOptions, DispatchOptions } from 'vuex'
import { STATS_STORE } from '@/store/constants'
import {
IStatisticsState,
IStatisticsActions,
IStatisticsGetters,
} from '@/store/modules/statistics/interfaces'
import { TStatistics } from '@/types/statistics'
export type TStatisticsMutations<S = IStatisticsState> = {
[STATS_STORE.MUTATIONS.UPDATE_USER_STATS](
state: S,
statistics: TStatistics
): void
}
export type TStatisticsStoreModule<S = IStatisticsState> = Omit<
VuexStore<S>,
'commit' | 'getters' | 'dispatch'
> & {
dispatch<K extends keyof IStatisticsActions>(
key: K,
payload?: Parameters<IStatisticsActions[K]>[1],
options?: DispatchOptions
): ReturnType<IStatisticsActions[K]>
} & {
getters: {
[K in keyof IStatisticsGetters]: ReturnType<IStatisticsGetters[K]>
}
} & {
commit<
K extends keyof TStatisticsMutations,
P extends Parameters<TStatisticsMutations[K]>[1]
>(
key: K,
payload?: P,
options?: CommitOptions
): ReturnType<TStatisticsMutations[K]>
}

View File

@ -4,7 +4,6 @@ import { USER_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { IAuthUserProfile, ILoginOrRegisterData } from '@/types/user'
// STORE
export interface IUserState {
authToken: string | null
authUserProfile: IAuthUserProfile

View File

@ -1,10 +1,13 @@
import { TRootStoreModule } from '@/store/modules/root/types'
import { TStatisticsStoreModule } from '@/store/modules/statistics/types'
import { TUserStoreModule } from '@/store/modules/user/types'
type StoreModules = {
rootModule: TRootStoreModule
statsModule: TStatisticsStoreModule
userModule: TUserStoreModule
}
export type Store = TUserStoreModule<Pick<StoreModules, 'userModule'>> &
TStatisticsStoreModule<Pick<StoreModules, 'statsModule'>> &
TRootStoreModule<Pick<StoreModules, 'rootModule'>>

View File

@ -0,0 +1,20 @@
export interface IStatistics {
nb_workouts: number
total_distance: number
total_duration: number
}
export type TStatistics = {
[key in string | number]: IStatistics
}
export interface IStatisticsParams {
from: string
to: string
time: string
}
export interface IUserStatisticsPayload {
username: string
filterType: string
params: IStatisticsParams
}

View File

@ -3,6 +3,7 @@ import { ActionContext } from 'vuex'
import { ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { IStatisticsState } from '@/store/modules/statistics/interfaces'
import { IUserState } from '@/store/modules/user/interfaces'
export const getApiUrl = (): string => {
@ -17,7 +18,8 @@ const removeLastDot = (text: string): string => text.replace(/\.$/gm, '')
export const handleError = (
context:
| ActionContext<IRootState, IRootState>
| ActionContext<IUserState, IRootState>,
| ActionContext<IUserState, IRootState>
| ActionContext<IStatisticsState, IRootState>,
error: AxiosError | null,
msg = 'UNKNOWN'
): void => {