From 48c1fa8e6963e7b99a5faf605decd3bcb770ab0a Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 22 Dec 2022 01:59:27 +0000 Subject: [PATCH] Added timeout to fetch user totals which prevents Duplicate calls which would be really annoying --- .../src/components/NoteTitleDisplayCard.vue | 12 +++++---- client/src/pages/NotesPage.vue | 8 ++++-- client/src/stores/mainStore.js | 26 +++++++++++-------- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/client/src/components/NoteTitleDisplayCard.vue b/client/src/components/NoteTitleDisplayCard.vue index 36618bb..8769a7b 100644 --- a/client/src/components/NoteTitleDisplayCard.vue +++ b/client/src/components/NoteTitleDisplayCard.vue @@ -227,10 +227,12 @@ }, pinNote(){ //togglePinned() <- old name this.showWorking = true - let postData = {'pinned': !this.note.pinned, 'noteId':this.note.id} + this.note.pinned = this.note.pinned == 1 ? 0:1 + let postData = {'pinned': this.note.pinned, 'noteId':this.note.id} axios.post('/api/note/setpinned', postData) .then(data => { this.showWorking = false + // this event is triggered by the server after note is saved // this.$bus.$emit('update_single_note', this.note.id) }) .catch(error => { this.$bus.$emit('notification', 'Failed to Pin Note') }) @@ -246,11 +248,10 @@ //Show message so no one worries where note went let message = 'Moved to Archive' if(postData.archived != 1){ - message = 'Moved to main list' + message = 'Moved out of Archive' } this.$bus.$emit('notification', message) - - // this.$bus.$emit('update_single_note', this.note.id) + this.$bus.$emit('update_single_note', this.note.id) }) .catch(error => { this.$bus.$emit('notification', 'Failed to Archive Note') }) }, @@ -265,9 +266,10 @@ //Show message so no one worries where note went let message = 'Moved to Trash' if(postData.trashed == 0){ - message = 'Moved to main list' + message = 'Moved out of Trash' } this.$bus.$emit('notification', message) + this.$bus.$emit('update_single_note', this.note.id) }) .catch(error => { this.$bus.$emit('notification', 'Failed to Trash Note') }) diff --git a/client/src/pages/NotesPage.vue b/client/src/pages/NotesPage.vue index 7e5a117..28945da 100644 --- a/client/src/pages/NotesPage.vue +++ b/client/src/pages/NotesPage.vue @@ -127,7 +127,7 @@ :data="note" :title-view="titleView || isFloatingList" :currently-open="openNotes.includes(note.id)" - :key="note.id + note.color + '-' +note.title.length + '-' +note.subtext.length + '-' + note.tag_count + note.updated" + :key="note.id + note.color + '-' +note.title.length + '-' +note.subtext.length + '-' + note.tag_count + note.updated + note.archived + note.pinned + note.trashed" /> @@ -564,16 +564,20 @@ // @TODO Don't even trigger this if the note wasn't changed updateSingleNote(noteId, focuseAndAnimate = true){ + console.log('updating single note', noteId) + noteId = parseInt(noteId) //Find local note, if it exists; continue let note = null - if(this.$refs['note-'+noteId] && this.$refs['note-'+noteId][0] && this.$refs['note-'+noteId][0].note){ + if(this.$refs['note-'+noteId]?.[0]?.note){ note = this.$refs['note-'+noteId][0].note //Show that note is working on updating this.$refs['note-'+noteId][0].showWorking = true } + this.rebuildNoteCategorise() + // return //Lookup one note using passed in ID const postData = { diff --git a/client/src/stores/mainStore.js b/client/src/stores/mainStore.js index 37d698e..baf8fa6 100644 --- a/client/src/stores/mainStore.js +++ b/client/src/stores/mainStore.js @@ -9,6 +9,7 @@ export default new Vuex.Store({ username: null, nightMode: false, isUserOnMobile: false, + fetchTotalsTimeout: null, userTotals: null, activeSessions: 0, }, @@ -155,17 +156,20 @@ export default new Vuex.Store({ } }, actions: { - fetchAndUpdateUserTotals ({ commit }) { - axios.post('/api/user/totals') - .then( ({data}) => { - commit('setUserTotals', data) - }) - .catch( error => { - if(error.response && error.response.status == 400){ - commit('destroyLoginToken') - location.reload() - } - }) + fetchAndUpdateUserTotals ({ commit, state }) { + clearTimeout(state.fetchTotalsTimeout) + state.fetchTotalsTimeout = setTimeout(() => { + axios.post('/api/user/totals') + .then( ({data}) => { + commit('setUserTotals', data) + }) + .catch( error => { + if(error.response && error.response.status == 400){ + commit('destroyLoginToken') + location.reload() + } + }) + }, 100) } } }) \ No newline at end of file