From 6b848228ca634e860316c243f5eb1d46b226e8eb Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 11 Aug 2021 18:33:02 +0200 Subject: [PATCH] Client - refactor store --- fittrackee_client/src/components/NavBar.vue | 2 +- fittrackee_client/src/store/constants.ts | 10 +++++++++ fittrackee_client/src/store/index.ts | 21 +++++-------------- .../src/store/modules/root/enums.ts | 3 +++ .../src/store/modules/root/index.ts | 17 +++++++++++++++ .../src/store/modules/root/interfaces.ts | 5 +++++ .../src/store/modules/root/mutations.ts | 10 +++++++++ .../src/store/modules/root/state.ts | 7 +++++++ .../src/store/modules/root/types.ts | 6 ++++++ fittrackee_client/src/store/modules/user.ts | 20 ------------------ .../src/store/modules/user/enums.ts | 3 +++ .../src/store/modules/user/getters.ts | 10 +++++++++ .../src/store/modules/user/index.ts | 12 +++++++++++ .../src/store/modules/user/interfaces.ts | 9 ++++++++ .../src/store/modules/user/state.ts | 5 +++++ fittrackee_client/src/types/state.ts | 6 +----- 16 files changed, 104 insertions(+), 42 deletions(-) create mode 100644 fittrackee_client/src/store/constants.ts create mode 100644 fittrackee_client/src/store/modules/root/enums.ts create mode 100644 fittrackee_client/src/store/modules/root/index.ts create mode 100644 fittrackee_client/src/store/modules/root/interfaces.ts create mode 100644 fittrackee_client/src/store/modules/root/mutations.ts create mode 100644 fittrackee_client/src/store/modules/root/state.ts create mode 100644 fittrackee_client/src/store/modules/root/types.ts delete mode 100644 fittrackee_client/src/store/modules/user.ts create mode 100644 fittrackee_client/src/store/modules/user/enums.ts create mode 100644 fittrackee_client/src/store/modules/user/getters.ts create mode 100644 fittrackee_client/src/store/modules/user/index.ts create mode 100644 fittrackee_client/src/store/modules/user/interfaces.ts create mode 100644 fittrackee_client/src/store/modules/user/state.ts diff --git a/fittrackee_client/src/components/NavBar.vue b/fittrackee_client/src/components/NavBar.vue index d03603e3..bf709269 100644 --- a/fittrackee_client/src/components/NavBar.vue +++ b/fittrackee_client/src/components/NavBar.vue @@ -83,7 +83,7 @@ } function updateLanguage(option: IDropdownOption) { locale.value = option.value.toString() - store.commit('setLanguage', option.value) + store.commit('UPDATE_LANG', option.value) } return { diff --git a/fittrackee_client/src/store/constants.ts b/fittrackee_client/src/store/constants.ts new file mode 100644 index 00000000..f1f83a98 --- /dev/null +++ b/fittrackee_client/src/store/constants.ts @@ -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, +} diff --git a/fittrackee_client/src/store/index.ts b/fittrackee_client/src/store/index.ts index 8abbdb96..c3d82fd5 100644 --- a/fittrackee_client/src/store/index.ts +++ b/fittrackee_client/src/store/index.ts @@ -1,18 +1,7 @@ import { createStore } from 'vuex' -import { DefaultStateTypes } from '@/types/state' -import user from './modules/user' +import { IRootState } from '@/store/modules/root/interfaces' +import root from '@/store/modules/root' -export default createStore({ - state: { - language: 'en', - }, - mutations: { - setLanguage(state: DefaultStateTypes, language: string) { - state.language = language - }, - }, - actions: {}, - modules: { - user, - }, -}) +const store = createStore(root) + +export default store diff --git a/fittrackee_client/src/store/modules/root/enums.ts b/fittrackee_client/src/store/modules/root/enums.ts new file mode 100644 index 00000000..3f344444 --- /dev/null +++ b/fittrackee_client/src/store/modules/root/enums.ts @@ -0,0 +1,3 @@ +export enum RootMutations { + UPDATE_LANG = 'UPDATE_LANG', +} diff --git a/fittrackee_client/src/store/modules/root/index.ts b/fittrackee_client/src/store/modules/root/index.ts new file mode 100644 index 00000000..acb8d2e4 --- /dev/null +++ b/fittrackee_client/src/store/modules/root/index.ts @@ -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 = { + userModule, +} + +const root: Module = { + state, + mutations, + modules, +} + +export default root diff --git a/fittrackee_client/src/store/modules/root/interfaces.ts b/fittrackee_client/src/store/modules/root/interfaces.ts new file mode 100644 index 00000000..088bcdfa --- /dev/null +++ b/fittrackee_client/src/store/modules/root/interfaces.ts @@ -0,0 +1,5 @@ +export interface IRootState { + root: boolean + language: string + errorMessage: string | null +} diff --git a/fittrackee_client/src/store/modules/root/mutations.ts b/fittrackee_client/src/store/modules/root/mutations.ts new file mode 100644 index 00000000..735b08f3 --- /dev/null +++ b/fittrackee_client/src/store/modules/root/mutations.ts @@ -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 & TRootMutations = { + [ROOT_STORE.MUTATIONS.UPDATE_LANG](state: IRootState, language: string) { + state.language = language + }, +} diff --git a/fittrackee_client/src/store/modules/root/state.ts b/fittrackee_client/src/store/modules/root/state.ts new file mode 100644 index 00000000..89c9f9ac --- /dev/null +++ b/fittrackee_client/src/store/modules/root/state.ts @@ -0,0 +1,7 @@ +import { IRootState } from '@/store/modules/root/interfaces' + +export const state: IRootState = { + root: true, + language: 'en', + errorMessage: null, +} diff --git a/fittrackee_client/src/store/modules/root/types.ts b/fittrackee_client/src/store/modules/root/types.ts new file mode 100644 index 00000000..52f9476f --- /dev/null +++ b/fittrackee_client/src/store/modules/root/types.ts @@ -0,0 +1,6 @@ +import { ROOT_STORE } from '@/store/constants' +import { IRootState } from '@/store/modules/root/interfaces' + +export type TRootMutations = { + [ROOT_STORE.MUTATIONS.UPDATE_LANG](state: S, language: string): void +} diff --git a/fittrackee_client/src/store/modules/user.ts b/fittrackee_client/src/store/modules/user.ts deleted file mode 100644 index 14bab8bc..00000000 --- a/fittrackee_client/src/store/modules/user.ts +++ /dev/null @@ -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, -} diff --git a/fittrackee_client/src/store/modules/user/enums.ts b/fittrackee_client/src/store/modules/user/enums.ts new file mode 100644 index 00000000..0fa5f2bf --- /dev/null +++ b/fittrackee_client/src/store/modules/user/enums.ts @@ -0,0 +1,3 @@ +export enum UserGetters { + IS_AUTHENTICATED = 'IS_AUTHENTICATED', +} diff --git a/fittrackee_client/src/store/modules/user/getters.ts b/fittrackee_client/src/store/modules/user/getters.ts new file mode 100644 index 00000000..a4cf40e8 --- /dev/null +++ b/fittrackee_client/src/store/modules/user/getters.ts @@ -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 & IUserGetters = { + [USER_STORE.GETTERS.IS_AUTHENTICATED]: (state: IUserState) => { + return state.authToken !== null + }, +} diff --git a/fittrackee_client/src/store/modules/user/index.ts b/fittrackee_client/src/store/modules/user/index.ts new file mode 100644 index 00000000..dbccfbae --- /dev/null +++ b/fittrackee_client/src/store/modules/user/index.ts @@ -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 = { + state: userState, + getters, +} + +export default user diff --git a/fittrackee_client/src/store/modules/user/interfaces.ts b/fittrackee_client/src/store/modules/user/interfaces.ts new file mode 100644 index 00000000..f47eaac0 --- /dev/null +++ b/fittrackee_client/src/store/modules/user/interfaces.ts @@ -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 +} diff --git a/fittrackee_client/src/store/modules/user/state.ts b/fittrackee_client/src/store/modules/user/state.ts new file mode 100644 index 00000000..37e25790 --- /dev/null +++ b/fittrackee_client/src/store/modules/user/state.ts @@ -0,0 +1,5 @@ +import { IUserState } from '@/store/modules/user/interfaces' + +export const userState: IUserState = { + authToken: null, +} diff --git a/fittrackee_client/src/types/state.ts b/fittrackee_client/src/types/state.ts index 791cb90d..c478e85d 100644 --- a/fittrackee_client/src/types/state.ts +++ b/fittrackee_client/src/types/state.ts @@ -1,7 +1,3 @@ -export interface DefaultStateTypes { +export interface IDefaultState { language: string } - -export interface UserStateTypes { - authToken: string -}