From 86396dd8c4aba90c4a9a1a5aa532e6428be8cd21 Mon Sep 17 00:00:00 2001 From: Roman Cervantes Date: Fri, 18 Oct 2019 11:15:09 -0700 Subject: [PATCH] Moved save settings to actions --- src/App.vue | 18 ------------------ src/components/WallpaperUpload.vue | 6 +----- src/pages/Settings.vue | 10 ++++++++-- src/store/actions.js | 13 +++++++++++++ 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/App.vue b/src/App.vue index b54f73dc..81441ec2 100644 --- a/src/App.vue +++ b/src/App.vue @@ -24,7 +24,6 @@ import NavHeader from '@/components/NavHeader'; import Toast from '@/components/Toast'; import firebase from 'firebase/app'; import { mapState } from 'vuex'; -import { debounce } from 'lodash'; import 'firebase/auth'; import 'firebase/firestore'; import 'firebase/storage'; @@ -105,14 +104,12 @@ export default { }, mounted() { - this.$bus.$on('SAVE_SETTINGS', this.saveSettings); this.$bus.$on('SAVE_TAGS', this.saveTags); this.$bus.$on('SAVE_NOTES', this.saveNotes); this.init(); }, beforeDestroy() { - this.$bus.$off('SAVE_SETTINGS'); this.$bus.$off('SAVE_TAGS'); this.$bus.$off('SAVE_NOTES'); }, @@ -170,21 +167,6 @@ export default { }); }, - saveSettings: debounce( - // eslint-disable-next-line - function(settings) { - // TOOD: move to actions - db.collection('settings').doc(this.user.uid).set(settings, { merge: true }) - .then(() => { - this.$store.commit('SET_SETTINGS', settings); - this.$bus.$emit('TOAST', { message: 'Settings saved' }); - }) - .catch(() => { - this.$bus.$emit('TOAST', { message: 'There was an error saving your settings', type: 'error' }); - this.$router.push({ name: 'sessionExpired' }); - }); - }, 500), - saveTags(tags, force) { if (tags) { // TOOD: move to actions diff --git a/src/components/WallpaperUpload.vue b/src/components/WallpaperUpload.vue index 0f90be44..3133cad4 100644 --- a/src/components/WallpaperUpload.vue +++ b/src/components/WallpaperUpload.vue @@ -109,12 +109,8 @@ export default { wallpapers: this.wallpapers, }; - const db = firebase.firestore(); - - // TOOD: move to actions - db.collection('settings').doc(this.user.uid).set(settings) + this.$store.dispatch('SAVE_SETTINGS', settings) .then(() => { - this.$store.commit('SET_SETTINGS', settings); this.$bus.$emit('TOAST', { message: 'Settings saved' }); this.loading = false; }) diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue index 062833cd..9d835c05 100644 --- a/src/pages/Settings.vue +++ b/src/pages/Settings.vue @@ -102,8 +102,14 @@ export default { methods: { save() { - // TODO: call action directly - this.$bus.$emit('SAVE_SETTINGS', this.localSettings); + this.$store.dispatch('SAVE_SETTINGS', this.localSettings) + .then(() => { + this.$bus.$emit('TOAST', { message: 'Settings saved' }); + }) + .catch(() => { + this.$bus.$emit('TOAST', { message: 'There was an error saving your settings', type: 'error' }); + this.$router.push({ name: 'sessionExpired' }); + }); }, deleteAccount() { diff --git a/src/store/actions.js b/src/store/actions.js index ec09f340..b5e0ee8f 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -24,6 +24,19 @@ export default { }); }, + SAVE_SETTINGS({ commit, state }, settings) { + const db = firebase.firestore(); + + return new Promise((resolve, reject) => { + db.collection('settings').doc(state.user.uid).set(settings, { merge: true }) + .then(() => { + commit('SET_SETTINGS', settings); + resolve(); + }) + .catch(reject); + }); + }, + LOAD_RELEASES({ commit }) { return new Promise((resolve, reject) => { axios.get('https://api.github.com/repos/romancm/gamebrary/releases')