Client - refactor store interfaces (move w/ types)

This commit is contained in:
Sam 2021-08-22 20:38:22 +02:00
parent 3b8ac44433
commit cac9607489
23 changed files with 124 additions and 127 deletions

View File

@ -1,7 +1,7 @@
import { createStore } from 'vuex' import { createStore } from 'vuex'
import root from '@/store/modules/root' import root from '@/store/modules/root'
import { IRootState } from '@/store/modules/root/interfaces' import { IRootState } from '@/store/modules/root/types'
const store = createStore<IRootState>(root) const store = createStore<IRootState>(root)

View File

@ -2,7 +2,7 @@ import { ActionContext, ActionTree } from 'vuex'
import authApi from '@/api/authApi' import authApi from '@/api/authApi'
import { ROOT_STORE } from '@/store/constants' import { ROOT_STORE } from '@/store/constants'
import { IRootActions, IRootState } from '@/store/modules/root/interfaces' import { IRootActions, IRootState } from '@/store/modules/root/types'
import { handleError } from '@/utils' import { handleError } from '@/utils'
export const actions: ActionTree<IRootState, IRootState> & IRootActions = { export const actions: ActionTree<IRootState, IRootState> & IRootActions = {

View File

@ -1,7 +1,7 @@
import { GetterTree } from 'vuex' import { GetterTree } from 'vuex'
import { ROOT_STORE } from '@/store/constants' import { ROOT_STORE } from '@/store/constants'
import { IRootState, IRootGetters } from '@/store/modules/root/interfaces' import { IRootGetters, IRootState } from '@/store/modules/root/types'
export const getters: GetterTree<IRootState, IRootState> & IRootGetters = { export const getters: GetterTree<IRootState, IRootState> & IRootGetters = {
[ROOT_STORE.GETTERS.APP_CONFIG]: (state: IRootState) => { [ROOT_STORE.GETTERS.APP_CONFIG]: (state: IRootState) => {

View File

@ -2,9 +2,9 @@ import { Module, ModuleTree } from 'vuex'
import { actions } from '@/store/modules/root/actions' import { actions } from '@/store/modules/root/actions'
import { getters } from '@/store/modules/root/getters' import { getters } from '@/store/modules/root/getters'
import { IRootState } from '@/store/modules/root/interfaces'
import { mutations } from '@/store/modules/root/mutations' import { mutations } from '@/store/modules/root/mutations'
import { state } from '@/store/modules/root/state.ts' import { state } from '@/store/modules/root/state.ts'
import { IRootState } from '@/store/modules/root/types'
import statsModule from '@/store/modules/statistics' import statsModule from '@/store/modules/statistics'
import userModule from '@/store/modules/user' import userModule from '@/store/modules/user'

View File

@ -1,27 +0,0 @@
import { ActionContext } from 'vuex'
import { ROOT_STORE } from '@/store/constants'
import { IAppConfig, IApplication } from '@/types/application'
export interface IRootState {
root: boolean
language: string
errorMessages: string | string[] | null
application: IApplication
appLoading: boolean
}
export interface IRootActions {
[ROOT_STORE.ACTIONS.GET_APPLICATION_CONFIG](
context: ActionContext<IRootState, IRootState>
): void
}
export interface IRootGetters {
[ROOT_STORE.GETTERS.APP_CONFIG](state: IRootState): IAppConfig
[ROOT_STORE.GETTERS.APP_LOADING](state: IRootState): boolean
[ROOT_STORE.GETTERS.ERROR_MESSAGES](
state: IRootState
): string | string[] | null
[ROOT_STORE.GETTERS.LANGUAGE](state: IRootState): string
}

View File

@ -1,8 +1,7 @@
import { MutationTree } from 'vuex' import { MutationTree } from 'vuex'
import { ROOT_STORE } from '@/store/constants' import { ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces' import { IRootState, TRootMutations } from '@/store/modules/root/types'
import { TRootMutations } from '@/store/modules/root/types'
import { IAppConfig } from '@/types/application' import { IAppConfig } from '@/types/application'
export const mutations: MutationTree<IRootState> & TRootMutations = { export const mutations: MutationTree<IRootState> & TRootMutations = {

View File

@ -1,4 +1,4 @@
import { IRootState } from '@/store/modules/root/interfaces' import { IRootState } from '@/store/modules/root/types'
import { IApplication } from '@/types/application' import { IApplication } from '@/types/application'
export const state: IRootState = { export const state: IRootState = {

View File

@ -1,11 +1,38 @@
import { Store as VuexStore, CommitOptions, DispatchOptions } from 'vuex' import {
ActionContext,
CommitOptions,
DispatchOptions,
Store as VuexStore,
} from 'vuex'
import { ROOT_STORE } from '@/store/constants' import { ROOT_STORE } from '@/store/constants'
import { import { IAppConfig, IApplication } from '@/types/application'
IRootActions,
IRootGetters, export interface IRootState {
IRootState, root: boolean
} from '@/store/modules/root/interfaces' language: string
errorMessages: string | string[] | null
application: IApplication
appLoading: boolean
}
export interface IRootActions {
[ROOT_STORE.ACTIONS.GET_APPLICATION_CONFIG](
context: ActionContext<IRootState, IRootState>
): void
}
export interface IRootGetters {
[ROOT_STORE.GETTERS.APP_CONFIG](state: IRootState): IAppConfig
[ROOT_STORE.GETTERS.APP_LOADING](state: IRootState): boolean
[ROOT_STORE.GETTERS.ERROR_MESSAGES](
state: IRootState
): string | string[] | null
[ROOT_STORE.GETTERS.LANGUAGE](state: IRootState): string
}
export type TRootMutations<S = IRootState> = { export type TRootMutations<S = IRootState> = {
[ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES](state: S): void [ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES](state: S): void

View File

@ -2,11 +2,11 @@ import { ActionContext, ActionTree } from 'vuex'
import authApi from '@/api/authApi' import authApi from '@/api/authApi'
import { STATS_STORE, ROOT_STORE } from '@/store/constants' import { STATS_STORE, ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces' import { IRootState } from '@/store/modules/root/types'
import { import {
IStatisticsActions, IStatisticsActions,
IStatisticsState, IStatisticsState,
} from '@/store/modules/statistics/interfaces' } from '@/store/modules/statistics/types'
import { IUserStatisticsPayload } from '@/types/statistics' import { IUserStatisticsPayload } from '@/types/statistics'
import { handleError } from '@/utils' import { handleError } from '@/utils'

View File

@ -1,11 +1,11 @@
import { GetterTree } from 'vuex' import { GetterTree } from 'vuex'
import { STATS_STORE } from '@/store/constants' import { STATS_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces' import { IRootState } from '@/store/modules/root/types'
import { import {
IStatisticsGetters, IStatisticsGetters,
IStatisticsState, IStatisticsState,
} from '@/store/modules/statistics/interfaces' } from '@/store/modules/statistics/types'
export const getters: GetterTree<IStatisticsState, IRootState> & export const getters: GetterTree<IStatisticsState, IRootState> &
IStatisticsGetters = { IStatisticsGetters = {

View File

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

View File

@ -1,20 +0,0 @@
import { ActionContext } from 'vuex'
import { STATS_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { IUserStatisticsPayload, TStatisticsFromApi } from '@/types/statistics'
export interface IStatisticsState {
statistics: TStatisticsFromApi
}
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): TStatisticsFromApi
}

View File

@ -1,8 +1,10 @@
import { MutationTree } from 'vuex' import { MutationTree } from 'vuex'
import { STATS_STORE } from '@/store/constants' import { STATS_STORE } from '@/store/constants'
import { IStatisticsState } from '@/store/modules/statistics/interfaces' import {
import { TStatisticsMutations } from '@/store/modules/statistics/types' IStatisticsState,
TStatisticsMutations,
} from '@/store/modules/statistics/types'
import { TStatisticsFromApi } from '@/types/statistics' import { TStatisticsFromApi } from '@/types/statistics'
export const mutations: MutationTree<IStatisticsState> & TStatisticsMutations = export const mutations: MutationTree<IStatisticsState> & TStatisticsMutations =

View File

@ -1,4 +1,4 @@
import { IStatisticsState } from '@/store/modules/statistics/interfaces' import { IStatisticsState } from '@/store/modules/statistics/types'
import { TStatisticsFromApi } from '@/types/statistics' import { TStatisticsFromApi } from '@/types/statistics'
export const statisticsState: IStatisticsState = { export const statisticsState: IStatisticsState = {

View File

@ -1,12 +1,28 @@
import { Store as VuexStore, CommitOptions, DispatchOptions } from 'vuex' import {
ActionContext,
CommitOptions,
DispatchOptions,
Store as VuexStore,
} from 'vuex'
import { STATS_STORE } from '@/store/constants' import { STATS_STORE } from '@/store/constants'
import { import { IRootState } from '@/store/modules/root/types'
IStatisticsState, import { IUserStatisticsPayload, TStatisticsFromApi } from '@/types/statistics'
IStatisticsActions,
IStatisticsGetters, export interface IStatisticsState {
} from '@/store/modules/statistics/interfaces' statistics: TStatisticsFromApi
import { TStatisticsFromApi } from '@/types/statistics' }
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): TStatisticsFromApi
}
export type TStatisticsMutations<S = IStatisticsState> = { export type TStatisticsMutations<S = IStatisticsState> = {
[STATS_STORE.MUTATIONS.UPDATE_USER_STATS]( [STATS_STORE.MUTATIONS.UPDATE_USER_STATS](

View File

@ -4,8 +4,8 @@ import authApi from '@/api/authApi'
import api from '@/api/defaultApi' import api from '@/api/defaultApi'
import router from '@/router' import router from '@/router'
import { ROOT_STORE, USER_STORE } from '@/store/constants' import { ROOT_STORE, USER_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces' import { IRootState } from '@/store/modules/root/types'
import { IUserActions, IUserState } from '@/store/modules/user/interfaces' import { IUserActions, IUserState } from '@/store/modules/user/types'
import { ILoginOrRegisterData } from '@/types/user' import { ILoginOrRegisterData } from '@/types/user'
import { handleError } from '@/utils' import { handleError } from '@/utils'

View File

@ -1,8 +1,8 @@
import { GetterTree } from 'vuex' import { GetterTree } from 'vuex'
import { USER_STORE } from '@/store/constants' import { USER_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces' import { IRootState } from '@/store/modules/root/types'
import { IUserGetters, IUserState } from '@/store/modules/user/interfaces' import { IUserGetters, IUserState } from '@/store/modules/user/types'
export const getters: GetterTree<IUserState, IRootState> & IUserGetters = { export const getters: GetterTree<IUserState, IRootState> & IUserGetters = {
[USER_STORE.GETTERS.AUTH_TOKEN]: (state: IUserState) => { [USER_STORE.GETTERS.AUTH_TOKEN]: (state: IUserState) => {

View File

@ -1,11 +1,11 @@
import { Module } from 'vuex' import { Module } from 'vuex'
import { IRootState } from '@/store/modules/root/interfaces' import { IRootState } from '@/store/modules/root/types'
import { actions } from '@/store/modules/user/actions' import { actions } from '@/store/modules/user/actions'
import { getters } from '@/store/modules/user/getters' import { getters } from '@/store/modules/user/getters'
import { IUserState } from '@/store/modules/user/interfaces'
import { mutations } from '@/store/modules/user/mutations' import { mutations } from '@/store/modules/user/mutations'
import { userState } from '@/store/modules/user/state.ts' import { userState } from '@/store/modules/user/state.ts'
import { IUserState } from '@/store/modules/user/types'
const user: Module<IUserState, IRootState> = { const user: Module<IUserState, IRootState> = {
state: userState, state: userState,

View File

@ -1,32 +0,0 @@
import { ActionContext } from 'vuex'
import { USER_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { IAuthUserProfile, ILoginOrRegisterData } from '@/types/user'
export interface IUserState {
authToken: string | null
authUserProfile: IAuthUserProfile
}
export interface IUserActions {
[USER_STORE.ACTIONS.CHECK_AUTH_USER](
context: ActionContext<IUserState, IRootState>
): void
[USER_STORE.ACTIONS.GET_USER_PROFILE](
context: ActionContext<IUserState, IRootState>
): void
[USER_STORE.ACTIONS.LOGIN_OR_REGISTER](
context: ActionContext<IUserState, IRootState>,
data: ILoginOrRegisterData
): void
[USER_STORE.ACTIONS.LOGOUT](
context: ActionContext<IUserState, IRootState>
): void
}
export interface IUserGetters {
[USER_STORE.GETTERS.AUTH_TOKEN](state: IUserState): string | null
[USER_STORE.GETTERS.AUTH_USER_PROFILE](state: IUserState): IAuthUserProfile
[USER_STORE.GETTERS.IS_AUTHENTICATED](state: IUserState): boolean
}

View File

@ -1,8 +1,7 @@
import { MutationTree } from 'vuex' import { MutationTree } from 'vuex'
import { USER_STORE } from '@/store/constants' import { USER_STORE } from '@/store/constants'
import { IUserState } from '@/store/modules/user/interfaces' import { IUserState, TUserMutations } from '@/store/modules/user/types'
import { TUserMutations } from '@/store/modules/user/types'
import { IAuthUserProfile } from '@/types/user' import { IAuthUserProfile } from '@/types/user'
export const mutations: MutationTree<IUserState> & TUserMutations = { export const mutations: MutationTree<IUserState> & TUserMutations = {

View File

@ -1,4 +1,4 @@
import { IUserState } from '@/store/modules/user/interfaces' import { IUserState } from '@/store/modules/user/types'
import { IAuthUserProfile } from '@/types/user' import { IAuthUserProfile } from '@/types/user'
export const userState: IUserState = { export const userState: IUserState = {

View File

@ -1,12 +1,45 @@
import { Store as VuexStore, CommitOptions, DispatchOptions } from 'vuex' import {
ActionContext,
CommitOptions,
DispatchOptions,
Store as VuexStore,
} from 'vuex'
import { USER_STORE } from '@/store/constants' import { USER_STORE } from '@/store/constants'
import { import { IRootState } from '@/store/modules/root/types'
IUserActions, import { IAuthUserProfile, ILoginOrRegisterData } from '@/types/user'
IUserGetters,
IUserState, export interface IUserState {
} from '@/store/modules/user/interfaces' authToken: string | null
import { IAuthUserProfile } from '@/types/user' authUserProfile: IAuthUserProfile
}
export interface IUserActions {
[USER_STORE.ACTIONS.CHECK_AUTH_USER](
context: ActionContext<IUserState, IRootState>
): void
[USER_STORE.ACTIONS.GET_USER_PROFILE](
context: ActionContext<IUserState, IRootState>
): void
[USER_STORE.ACTIONS.LOGIN_OR_REGISTER](
context: ActionContext<IUserState, IRootState>,
data: ILoginOrRegisterData
): void
[USER_STORE.ACTIONS.LOGOUT](
context: ActionContext<IUserState, IRootState>
): void
}
export interface IUserGetters {
[USER_STORE.GETTERS.AUTH_TOKEN](state: IUserState): string | null
[USER_STORE.GETTERS.AUTH_USER_PROFILE](state: IUserState): IAuthUserProfile
[USER_STORE.GETTERS.IS_AUTHENTICATED](state: IUserState): boolean
}
export type TUserMutations<S = IUserState> = { export type TUserMutations<S = IUserState> = {
[USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN](state: S): void [USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN](state: S): void

View File

@ -2,9 +2,9 @@ import { AxiosError } from 'axios'
import { ActionContext } from 'vuex' import { ActionContext } from 'vuex'
import { ROOT_STORE } from '@/store/constants' import { ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces' import { IRootState } from '@/store/modules/root/types'
import { IStatisticsState } from '@/store/modules/statistics/interfaces' import { IStatisticsState } from '@/store/modules/statistics/types'
import { IUserState } from '@/store/modules/user/interfaces' import { IUserState } from '@/store/modules/user/types'
export const getApiUrl = (): string => { export const getApiUrl = (): string => {
return process.env.NODE_ENV === 'production' return process.env.NODE_ENV === 'production'