Client - refactor store

This commit is contained in:
Sam 2021-08-11 18:33:02 +02:00
parent f8b020940a
commit 6b848228ca
16 changed files with 104 additions and 42 deletions

View File

@ -83,7 +83,7 @@
} }
function updateLanguage(option: IDropdownOption) { function updateLanguage(option: IDropdownOption) {
locale.value = option.value.toString() locale.value = option.value.toString()
store.commit('setLanguage', option.value) store.commit('UPDATE_LANG', option.value)
} }
return { return {

View File

@ -0,0 +1,10 @@
import { RootMutations } from '@/store/modules/root/enums'
import { UserGetters } from '@/store/modules/user/enums'
export const ROOT_STORE = {
MUTATIONS: RootMutations,
}
export const USER_STORE = {
GETTERS: UserGetters,
}

View File

@ -1,18 +1,7 @@
import { createStore } from 'vuex' import { createStore } from 'vuex'
import { DefaultStateTypes } from '@/types/state' import { IRootState } from '@/store/modules/root/interfaces'
import user from './modules/user' import root from '@/store/modules/root'
export default createStore({ const store = createStore<IRootState>(root)
state: {
language: 'en', export default store
},
mutations: {
setLanguage(state: DefaultStateTypes, language: string) {
state.language = language
},
},
actions: {},
modules: {
user,
},
})

View File

@ -0,0 +1,3 @@
export enum RootMutations {
UPDATE_LANG = 'UPDATE_LANG',
}

View File

@ -0,0 +1,17 @@
import { Module, ModuleTree } from 'vuex'
import { IRootState } from '@/store/modules/root/interfaces'
import { mutations } from '@/store/modules/root/mutations'
import { state } from '@/store/modules/root/state.ts'
import userModule from '@/store/modules/user'
const modules: ModuleTree<IRootState> = {
userModule,
}
const root: Module<IRootState, IRootState> = {
state,
mutations,
modules,
}
export default root

View File

@ -0,0 +1,5 @@
export interface IRootState {
root: boolean
language: string
errorMessage: string | null
}

View File

@ -0,0 +1,10 @@
import { MutationTree } from 'vuex'
import { ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { TRootMutations } from '@/store/modules/root/types'
export const mutations: MutationTree<IRootState> & TRootMutations = {
[ROOT_STORE.MUTATIONS.UPDATE_LANG](state: IRootState, language: string) {
state.language = language
},
}

View File

@ -0,0 +1,7 @@
import { IRootState } from '@/store/modules/root/interfaces'
export const state: IRootState = {
root: true,
language: 'en',
errorMessage: null,
}

View File

@ -0,0 +1,6 @@
import { ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
export type TRootMutations<S = IRootState> = {
[ROOT_STORE.MUTATIONS.UPDATE_LANG](state: S, language: string): void
}

View File

@ -1,20 +0,0 @@
import { UserStateTypes } from '@/types/state'
const userState = {
authToken: null,
}
export type Getters = {
isAuthenticated(state: UserStateTypes): boolean
}
const getters: Getters = {
isAuthenticated(state: UserStateTypes) {
return state.authToken !== null
},
}
export default {
state: userState,
getters,
}

View File

@ -0,0 +1,3 @@
export enum UserGetters {
IS_AUTHENTICATED = 'IS_AUTHENTICATED',
}

View File

@ -0,0 +1,10 @@
import { GetterTree } from 'vuex'
import { USER_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { IUserGetters, IUserState } from '@/store/modules/user/interfaces'
export const getters: GetterTree<IUserState, IRootState> & IUserGetters = {
[USER_STORE.GETTERS.IS_AUTHENTICATED]: (state: IUserState) => {
return state.authToken !== null
},
}

View File

@ -0,0 +1,12 @@
import { Module } from 'vuex'
import { IRootState } from '@/store/modules/root/interfaces'
import { getters } from '@/store/modules/user/getters'
import { IUserState } from '@/store/modules/user/interfaces'
import { userState } from '@/store/modules/user/state.ts'
const user: Module<IUserState, IRootState> = {
state: userState,
getters,
}
export default user

View File

@ -0,0 +1,9 @@
import { USER_STORE } from '@/store/constants'
export interface IUserState {
authToken: string | null
}
export interface IUserGetters {
[USER_STORE.GETTERS.IS_AUTHENTICATED](state: IUserState): boolean
}

View File

@ -0,0 +1,5 @@
import { IUserState } from '@/store/modules/user/interfaces'
export const userState: IUserState = {
authToken: null,
}

View File

@ -1,7 +1,3 @@
export interface DefaultStateTypes { export interface IDefaultState {
language: string language: string
} }
export interface UserStateTypes {
authToken: string
}