Skip to content

Open source personal finance application

Add unsubscribe actions

Ilya Komichev65bf439a5f80
diff --git a/src/components/app/store/actions.js b/src/components/app/store/actions.js
index ba6c84fd..a75f0137 100644
--- a/src/components/app/store/actions.js
+++ b/src/components/app/store/actions.js
@@ -1,14 +1,6 @@
import localforage from 'localforage'
import { app } from '@/firebase'
-async function clearUserData ({ dispatch }) {
- dispatch('setActiveTab', 'stat')
- dispatch('setUser', null)
- dispatch('setCategories', {})
- dispatch('setWallets', {})
- dispatch('setTrns', {})
-}
-
export default {
async initApp ({ rootState, commit, dispatch }) {
await dispatch('initAppFromCache')
@@ -17,12 +9,14 @@ export default {
app.auth().onAuthStateChanged(async (user) => {
if (user) {
try {
- if (!rootState.user.user.uid || rootState.user.user.uid !== user.uid) await clearUserData({ dispatch })
- dispatch('initUser', user)
- dispatch('initCurrencies')
- dispatch('initCategories')
- dispatch('initWallets')
- dispatch('initTrns')
+ if (rootState.user.user.uid && rootState.user.user.uid !== user.uid) {
+ dispatch('clearUserData')
+ }
+ await dispatch('initUser', user)
+ await dispatch('initCurrencies')
+ await dispatch('initCategories')
+ await dispatch('initWallets')
+ await dispatch('initTrns')
} catch (e) {
console.error(e)
}
@@ -65,5 +59,14 @@ export default {
if (categories && user && trns && wallets) {
commit('setAppStatus', 'ready')
}
+ },
+
+ async clearUserData ({ commit, dispatch }) {
+ commit('setAppStatus', 'loading')
+ dispatch('setActiveTab', 'stat')
+ dispatch('setUser', null)
+ dispatch('setCategories', null)
+ dispatch('setWallets', null)
+ dispatch('setTrns', null)
}
}
diff --git a/src/components/categories/store/actions.js b/src/components/categories/store/actions.js
index 0d38b22c..fd99b299 100644
--- a/src/components/categories/store/actions.js
+++ b/src/components/categories/store/actions.js
@@ -14,5 +14,10 @@ export default {
setCategories ({ commit }, items) {
commit('setCategories', items)
localforage.setItem('next.categories', items)
+ },
+
+ unsubcribeCategories ({ rootState }) {
+ const uid = rootState.user.user.uid
+ db.ref(`users/${uid}/categories`).off()
}
}
diff --git a/src/components/trns/store/actions.js b/src/components/trns/store/actions.js
index 85f9c180..4094a16b 100644
--- a/src/components/trns/store/actions.js
+++ b/src/components/trns/store/actions.js
@@ -59,21 +59,26 @@ export default {
},
// init
- initTrns ({ rootState, dispatch }) {
+ initTrns ({ rootState, dispatch, commit }) {
const uid = rootState.user.user.uid
db.ref(`users/${uid}/trns`).on('value', snapshot => {
const items = Object.freeze(snapshot.val())
dispatch('setTrns', items)
+ commit('setAppStatus', 'ready')
}, e => console.error(e))
},
setTrns ({ commit }, items) {
commit('setTrns', items)
- commit('setAppStatus', 'ready')
localforage.setItem('next.trns', items)
},
+ unsubcribeTrns ({ rootState }) {
+ const uid = rootState.user.user.uid
+ db.ref(`users/${uid}/trns`).off()
+ },
+
async iniOfflineTrns ({ dispatch }) {
db.ref('.info/connected').on('value', async snap => {
const isConnected = snap.val()
diff --git a/src/components/user/store/actions.js b/src/components/user/store/actions.js
index 3b48bccc..02b8f295 100644
--- a/src/components/user/store/actions.js
+++ b/src/components/user/store/actions.js
@@ -15,6 +15,8 @@ export default {
dispatch('setUser', user)
dispatch('saveUserInfo', user)
dispatch('saveLastLoginDate', user)
+ dispatch('saveUserInfo', user)
+ dispatch('saveLastLoginDate', user)
},
setUser ({ commit }, user) {
@@ -22,9 +24,12 @@ export default {
localforage.setItem('next.user', user)
},
- signOut ({ commit, dispatch }) {
+ async signOut ({ rootState, commit, dispatch }) {
commit('setAppStatus', 'login')
- dispatch('setUser', null)
+ dispatch('unsubcribeCategories')
+ dispatch('unsubcribeTrns')
+ dispatch('unsubcribeWallets')
+ dispatch('clearUserData')
firebase.auth().signOut()
},
diff --git a/src/components/wallets/store/actions.js b/src/components/wallets/store/actions.js
index 08385c3c..0f07c359 100644
--- a/src/components/wallets/store/actions.js
+++ b/src/components/wallets/store/actions.js
@@ -14,5 +14,10 @@ export default {
setWallets ({ commit }, items) {
commit('setWallets', items)
localforage.setItem('next.wallets', items)
+ },
+
+ unsubcribeWallets ({ rootState }) {
+ const uid = rootState.user.user.uid
+ db.ref(`users/${uid}/accounts`).off()
}
}