Move logout to store

This commit is contained in:
Matthias 2021-08-28 19:46:30 +02:00
parent 8579499418
commit f30f968a2f
4 changed files with 24 additions and 11 deletions

View File

@ -45,7 +45,7 @@
<router-link class="dropdown-item" to="/settings">Settings</router-link> <router-link class="dropdown-item" to="/settings">Settings</router-link>
<b-checkbox v-model="layoutLockedLocal" class="pl-5">Lock layout</b-checkbox> <b-checkbox v-model="layoutLockedLocal" class="pl-5">Lock layout</b-checkbox>
<b-dropdown-item @click="resetDynamicLayout">Reset Layout</b-dropdown-item> <b-dropdown-item @click="resetDynamicLayout">Reset Layout</b-dropdown-item>
<router-link class="dropdown-item" to="/" @click.native="logout()" <router-link class="dropdown-item" to="/" @click.native="clickLogout()"
>Sign Out</router-link >Sign Out</router-link
> >
</b-nav-item-dropdown> </b-nav-item-dropdown>
@ -64,7 +64,6 @@
import { Component, Vue, Watch } from 'vue-property-decorator'; import { Component, Vue, Watch } from 'vue-property-decorator';
import LoginModal from '@/views/LoginModal.vue'; import LoginModal from '@/views/LoginModal.vue';
import { State, Action, namespace, Getter } from 'vuex-class'; import { State, Action, namespace, Getter } from 'vuex-class';
import userService from '@/shared/userService';
import BootswatchThemeSelect from '@/components/BootswatchThemeSelect.vue'; import BootswatchThemeSelect from '@/components/BootswatchThemeSelect.vue';
import { LayoutActions, LayoutGetters } from '@/store/modules/layout'; import { LayoutActions, LayoutGetters } from '@/store/modules/layout';
import { BotStoreGetters } from '@/store/modules/ftbot'; import { BotStoreGetters } from '@/store/modules/ftbot';
@ -95,6 +94,8 @@ export default class NavBar extends Vue {
@ftbot.Action getState; @ftbot.Action getState;
@ftbot.Action logout;
@ftbot.Getter [BotStoreGetters.botName]: string; @ftbot.Getter [BotStoreGetters.botName]: string;
@ftbot.Getter [BotStoreGetters.openTradeCount]: number; @ftbot.Getter [BotStoreGetters.openTradeCount]: number;
@ -130,8 +131,8 @@ export default class NavBar extends Vue {
} }
} }
logout(): void { clickLogout(): void {
userService.logout(); this.logout();
this.setLoggedIn(false); this.setLoggedIn(false);
} }

View File

@ -6,6 +6,13 @@ const AUTH_LOGIN_INFO = 'auth_login_info';
const APIBASE = '/api/v1'; const APIBASE = '/api/v1';
export class UserService { export class UserService {
private botId: string;
constructor(botId: string) {
console.log('botId', botId);
this.botId = botId;
}
private storeLoginInfo(loginInfo: AuthStorage): void { private storeLoginInfo(loginInfo: AuthStorage): void {
localStorage.setItem(AUTH_LOGIN_INFO, JSON.stringify(loginInfo)); localStorage.setItem(AUTH_LOGIN_INFO, JSON.stringify(loginInfo));
} }
@ -142,10 +149,10 @@ export class UserService {
} }
} }
export function useUserService() { export function useUserService(botId: string) {
const userservice = new UserService(); const userservice = new UserService(botId);
userservice.migrateLogin(); userservice.migrateLogin();
return userservice; return userservice;
} }
export default useUserService(); export default useUserService('ftbot.0');

View File

@ -53,7 +53,7 @@ export default function createBotStore() {
return { return {
namespaced: true, namespaced: true,
modules: { modules: {
'ftbot.0': createBotSubStore(), 'ftbot.0': createBotSubStore('ftbot.0'),
}, },
state, state,
mutations, mutations,

View File

@ -1,5 +1,5 @@
import { useApi } from '@/shared/apiService'; import { useApi } from '@/shared/apiService';
import userService from '@/shared/userService'; import { useUserService } from '@/shared/userService';
import { import {
BacktestResult, BacktestResult,
@ -123,9 +123,11 @@ export enum BotStoreActions {
pollBacktest = 'pollBacktest', pollBacktest = 'pollBacktest',
removeBacktest = 'removeBacktest', removeBacktest = 'removeBacktest',
stopBacktest = 'stopBacktest', stopBacktest = 'stopBacktest',
logout = 'logout',
} }
export function createBotSubStore() { export function createBotSubStore(botId: string) {
const userService = useUserService(botId);
const { api } = useApi(userService); const { api } = useApi(userService);
return { return {
@ -389,8 +391,11 @@ export function createBotSubStore() {
.catch(console.error); .catch(console.error);
} }
}, },
[BotStoreActions.logout]() {
userService.logout();
},
[BotStoreActions.setRefreshRequired]({ commit }, refreshRequired: boolean) { [BotStoreActions.setRefreshRequired]({ commit }, refreshRequired: boolean) {
commit('setRefreshRequired', refreshRequired); commit('updateRefreshRequired', refreshRequired);
}, },
[BotStoreActions.setDetailTrade]({ commit }, trade: Trade) { [BotStoreActions.setDetailTrade]({ commit }, trade: Trade) {
commit('setDetailTrade', trade); commit('setDetailTrade', trade);