Skip to content

Commit

Permalink
feat: reset store properly on logout (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutosh1206 authored Jul 8, 2022
1 parent 55b227b commit 32f70f2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
40 changes: 37 additions & 3 deletions src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,17 @@ import BookmarksIcon from '@/components/icons/Bookmarks.vue'
import CapsuleBlogchain from '@/components/icons/CapsuleBlogchain.vue'
import Crown2Icon from '@/components/icons/Crown2.vue'
import DashboardIcon from '@/components/icons/OverviewIcon.vue'
import { namespace as paymentProfileNamespace } from '@/store/paymentProfile'
import { MutationType as profileMutation, namespace as paymentProfileNamespace } from '@/store/paymentProfile'
import { MutationType, namespace as sessionStoreNamespace } from '~/store/session'
import { MutationType as subscriptionMutation, namespace as subscriptionNamespace } from '~/store/subscriptions'
import {
MutationType as subscriptionTierMutation,
namespace as subscriptionTierNamespace,
} from '~/store/subscriptionTiers'
import { MutationType as draftMutation, namespace as draftNamespace } from '~/store/draft'
import { MutationType as settingMutation, namespace as settingNamespace } from '~/store/settings'
import { walletLogout } from '@/backend/near'
interface IData {
showDropdown: boolean
Expand Down Expand Up @@ -308,13 +316,39 @@ export default Vue.extend({
...mapMutations(sessionStoreNamespace, {
endSession: MutationType.LOGOUT,
}),
...mapMutations(paymentProfileNamespace, {
resetProfile: profileMutation.RESET,
}),
...mapMutations(subscriptionNamespace, {
resetSubs: subscriptionMutation.RESET,
}),
...mapMutations(subscriptionTierNamespace, {
resetSubTiers: subscriptionTierMutation.LOGOUT,
}),
...mapMutations(draftNamespace, {
clearDrafts: draftMutation.CLEAR_DRAFTS,
}),
...mapMutations(settingNamespace, {
resetSettings: settingMutation.RESET_SETTINGS,
}),
disconnect(): void {
this.endSession()
this.resetProfile()
this.resetSubs()
this.resetSubTiers()
this.clearDrafts()
this.resetSettings()
this.$store.commit(`reset`)
this.$store.commit(`widgets/reset`)
const keystore = new BrowserLocalStorageKeyStore()
keystore.clear()
localStorage.clear()
window.localStorage.clear()
walletLogout()
this.$router.push(`/login`)
this.$store.commit(`widgets/reset`)
},
toggleMobileMenu() {
this.showDropdown = !this.showDropdown
Expand Down
7 changes: 0 additions & 7 deletions src/store/session.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// import type { Context } from '@nuxt/types'
import type { GetterTree, MutationTree } from 'vuex'
import { keyStores } from 'near-api-js'
import type { RootState } from './index'
import { Profile } from '@/backend/profile'
import { removeNearPrivateKey, walletLogout } from '@/backend/near'
export interface Session {
id: Profile[`id`]
name: Profile[`name`]
Expand Down Expand Up @@ -71,12 +69,7 @@ export const mutations: MutationTree<Session> = {
state.location = newLocation
},
[MutationType.LOGOUT]: (state) => {
const keystore = new keyStores.BrowserLocalStorageKeyStore()
keystore.clear()
Object.assign(state, createDefaultSession(``))
removeNearPrivateKey()
window.localStorage.removeItem(`accountId`)
walletLogout()
state.homeFeed = `TOP`
},
}
Expand Down
28 changes: 18 additions & 10 deletions src/store/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ export interface SettingState {
showUnauthPopup: boolean
}

export const state = (): SettingState => ({
mode: `Light`,
color: `Green`,
recentlyPosted: false,
recentlyInSettings: false,
lastActivePost: ``,
lastActivePostOffset: 0,
showUnauthPopup: false,
lastTopAlgorithm: `This month`,
})
export const state = (): SettingState => createDefaultSetting()

export const getters: GetterTree<SettingState, RootState> = {}

Expand All @@ -36,6 +27,7 @@ export const MutationType = {
SET_LAST_ACTIVE_POST: `setLastActivePost`,
SET_LAST_ACTIVE_TOP_ALGORITHM: `setLastActiveTopAlgorithm`,
TOGGLE_UNAUTH_POPUP: `toggleUnauthPopup`,
RESET_SETTINGS: `resetSettings`,
}

export const mutations: MutationTree<SettingState> = {
Expand Down Expand Up @@ -64,6 +56,9 @@ export const mutations: MutationTree<SettingState> = {
[MutationType.TOGGLE_UNAUTH_POPUP]: (state, c: boolean = !state.showUnauthPopup) => {
state.showUnauthPopup = c
},
[MutationType.RESET_SETTINGS]: (state) => {
Object.assign(state, createDefaultSetting())
},
}

export const actionType = {
Expand All @@ -76,3 +71,16 @@ export const actions: ActionTree<SettingState, RootState> = {
commit(MutationType.CHANGE_COLOR, `Green`)
},
}

export function createDefaultSetting(): SettingState {
return {
mode: `Light`,
color: `Green`,
recentlyPosted: false,
recentlyInSettings: false,
lastActivePost: ``,
lastActivePostOffset: 0,
showUnauthPopup: false,
lastTopAlgorithm: `This month`,
}
}

0 comments on commit 32f70f2

Please sign in to comment.