Skip to content

Commit

Permalink
feat: Split main store
Browse files Browse the repository at this point in the history
  • Loading branch information
erdkse committed Oct 3, 2021
1 parent 04b1b7f commit 8b09dd6
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 51 deletions.
10 changes: 9 additions & 1 deletion src/interfaces/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {IUser} from './user';
export interface IState {
export interface IAuthState {
token: string;
user: IUser;
}

export interface IAuthModule {
namespaced: boolean;
state: IAuthState;
mutations: any;
actions: any;
getters: any;
}
6 changes: 3 additions & 3 deletions src/modules/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class Login extends Vue {
try {
this.isAuthLoading = true;
const token = await loginByAuth(this.email, this.password);
this.$store.dispatch('login', token);
this.$store.dispatch('auth/login', token);
this.toast.success('Login succeeded');
this.isAuthLoading = false;
} catch (error: any) {
Expand All @@ -49,7 +49,7 @@ export default class Login extends Vue {
try {
this.isFacebookLoading = true;
const token = await loginByFacebook();
this.$store.dispatch('login', token);
this.$store.dispatch('auth/login', token);
this.isFacebookLoading = false;
} catch (error: any) {
this.toast.error(error.message);
Expand All @@ -61,7 +61,7 @@ export default class Login extends Vue {
try {
this.isGoogleLoading = true;
const token = await loginByGoogle();
this.$store.dispatch('login', token);
this.$store.dispatch('auth/login', token);
this.isGoogleLoading = false;
} catch (error: any) {
this.toast.error(error.message);
Expand Down
9 changes: 4 additions & 5 deletions src/modules/main/footer/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
<strong>
<span>{{ $t("labels.copyright") }} &copy; {{currentYear}}</span>
<a href="https://erdkse.com" target="_blank" rel="noopener noreferrer">
erdkse.com</a
>
<span>&nbsp;erdkse.com</span>
</a>
<span>.</span>
</strong>
<span> {{ $t("labels.allRightReserved") }}</span>
<div class="float-right d-none d-sm-inline-block">
<b>{{ $t("labels.version") }} </b>
<span>{{version}}</span>
<b>{{ $t("labels.version") }}</b>
<span>:&nbsp;{{version}}</span>
</div>
</footer>
4 changes: 2 additions & 2 deletions src/modules/main/header/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import Dropdown from '@/components/dropdown/dropdown.vue';
})
export default class User extends Vue {
get user(): IUser {
return this.$store.getters.user;
return this.$store.getters['auth/user'];
}

private logout() {
this.$store.dispatch('logout');
this.$store.dispatch('auth/logout');
}
}
4 changes: 2 additions & 2 deletions src/modules/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export default class Main extends Vue {
this.appElement.classList.add('layout-fixed');
try {
const user = await getProfile();
this.$store.dispatch('getUser', user);
this.$store.dispatch('auth/getUser', user);
} catch (error) {
this.$store.dispatch('logout');
this.$store.dispatch('auth/logout');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/main/menu-sidebar/menu-sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import MenuItem from '@/components/menu-item/menu-item.vue';
export default class MenuSidebar extends Vue {
public menu = MENU;
get user(): IUser {
return this.$store.getters.user;
return this.$store.getters['auth/user'];
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/modules/register/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Register extends Vue {
try {
this.isAuthLoading = true;
const token = await registerByAuth(this.email, this.password);
this.$store.dispatch('login', token);
this.$store.dispatch('auth/login', token);
this.toast.success('Register succeeded');
this.isAuthLoading = false;
} catch (error: any) {
Expand All @@ -53,7 +53,7 @@ export default class Register extends Vue {
try {
this.isFacebookLoading = true;
const token = await registerByFacebook();
this.$store.dispatch('login', token);
this.$store.dispatch('auth/login', token);
this.isFacebookLoading = false;
} catch (error: any) {
this.toast.error(error.message);
Expand All @@ -65,7 +65,7 @@ export default class Register extends Vue {
try {
this.isGoogleLoading = true;
const token = await registerByGoogle();
this.$store.dispatch('login', token);
this.$store.dispatch('auth/login', token);
this.isGoogleLoading = false;
} catch (error: any) {
this.toast.error(error.message);
Expand Down
4 changes: 2 additions & 2 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ const router = createRouter({
});

router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth && !store.getters.token) {
if (to.meta.requiresAuth && !store.getters['auth/token']) {
next('/login');
} else if (to.meta.requiresUnauth && !!store.getters.token) {
} else if (to.meta.requiresUnauth && !!store.getters['auth/token']) {
next('/');
} else {
next();
Expand Down
6 changes: 3 additions & 3 deletions src/store/actions.ts → src/store/authModule/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {IUser} from '@/interfaces/user';
import router from '@/router/index';

export default {
login: (context: any, payload: string): any => {
login: (context: any, payload: string): void => {
context.commit('setToken', payload);
router.replace('/');
},
getUser: (context: any, payload: IUser): any => {
getUser: (context: any, payload: IUser): void => {
context.commit('setUser', payload);
},
logout: (context: any) => {
logout: (context: any): void => {
context.commit('setToken', null);
context.commit('setUser', null);
localStorage.removeItem('gatekeeper_token');
Expand Down
7 changes: 7 additions & 0 deletions src/store/authModule/getters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {IAuthState} from '@/interfaces/state';
import {IUser} from '@/interfaces/user';

export default {
user: (state: IAuthState): IUser => state.user,
token: (state: IAuthState): string => state.token
};
17 changes: 17 additions & 0 deletions src/store/authModule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mutations from './mutations';
import actions from './actions';
import getters from './getters';
import {IAuthModule} from '@/interfaces/state';

const authModule: IAuthModule = {
namespaced: true,
state: {
token: localStorage.getItem('gatekeeper_token'),
user: null
},
mutations,
actions,
getters
};

export default authModule;
11 changes: 11 additions & 0 deletions src/store/authModule/mutations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {IAuthState} from '@/interfaces/state';
import {IUser} from '@/interfaces/user';

export default {
setToken: (state: IAuthState, payload: string): void => {
state.token = payload;
},
setUser: (state: IAuthState, payload: IUser): void => {
state.user = payload;
}
};
7 changes: 0 additions & 7 deletions src/store/getters.ts

This file was deleted.

17 changes: 6 additions & 11 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import {createStore} from 'vuex';
import mutations from './mutations';
import actions from './actions';
import getters from './getters';
import authModule from './authModule';
import uiModule from './uiModule';

export default createStore({
state: {
token: localStorage.getItem('gatekeeper_token'),
user: null
},
mutations,
actions,
getters,
modules: {}
modules: {
auth: authModule,
ui: uiModule
}
});
11 changes: 0 additions & 11 deletions src/store/mutations.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/store/uiModule/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
toggleSidebarMenu: (context: any): any => {
context.commit('toggleSidebarMenu');
}
};
4 changes: 4 additions & 0 deletions src/store/uiModule/getters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
isSidebarMenuCollapsed: (state: any): boolean =>
state.isSidebarMenuCollapsed
};
15 changes: 15 additions & 0 deletions src/store/uiModule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import mutations from './mutations';
import actions from './actions';
import getters from './getters';

const uiModule = {
namespaced: true,
state: {
isSidebarMenuCollapsed: false
},
mutations,
actions,
getters
};

export default uiModule;
5 changes: 5 additions & 0 deletions src/store/uiModule/mutations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
toggleSidebarMenu: (state: any): void => {
state.isSidebarMenuCollapsed = !state.isSidebarMenuCollapsed;
}
};

0 comments on commit 8b09dd6

Please sign in to comment.