From 7f2a4fee072e7a7cd1b9f903c9221651aed650c4 Mon Sep 17 00:00:00 2001 From: theo1 Date: Mon, 19 Oct 2020 16:46:30 +0200 Subject: [PATCH] Using Vuex action to async/await the mutation and get an up-to-date state. --- md-parser/src/components/EditPage.vue | 9 ++++++--- md-parser/src/components/UserInputPage.vue | 10 +++++----- md-parser/src/store/store.js | 6 +++++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/md-parser/src/components/EditPage.vue b/md-parser/src/components/EditPage.vue index 9f95a02..a83059a 100644 --- a/md-parser/src/components/EditPage.vue +++ b/md-parser/src/components/EditPage.vue @@ -56,14 +56,17 @@ export default { "date_created": data.date_created, "date_modified": data.date_modified, }) - // update content - this.$store.commit('updateText', data.content) + // Call the Vuex action to update the content, + // and await the mutation to be over before going to the Input page. + this.$store.dispatch('updateText', data.content) + .then(() => { + this.$router.push('/write') + }) }, response => { console.log('Error: ', response) } ) - this.$router.push('/write') } } } diff --git a/md-parser/src/components/UserInputPage.vue b/md-parser/src/components/UserInputPage.vue index 9442ed5..83d69ae 100644 --- a/md-parser/src/components/UserInputPage.vue +++ b/md-parser/src/components/UserInputPage.vue @@ -25,21 +25,21 @@ export default { }, data: function() { return{ - userText: '', } }, methods: { // saves article content articleUpate : function() { - console.log('updating in UserInputPage') this.userText = this.$store.getters.getContent }, articlePublish: function(){ this.$router.push('/published') - } + }, }, - mounted: function(){ - this.userText = this.$store.getters.getContent + computed: { + userText: function(){ + return this.$store.getters.getContent + } } } diff --git a/md-parser/src/store/store.js b/md-parser/src/store/store.js index cd3dc5d..08931f1 100644 --- a/md-parser/src/store/store.js +++ b/md-parser/src/store/store.js @@ -30,7 +30,6 @@ export const store = new Vuex.Store({ today (state, date) { let dateObj = new Date() let today = dateObj.getDate() + '/' + dateObj.getMonth() + '/' + dateObj.getFullYear() - console.log("Today : ", today) if (date === 1) { state.article.meta.date_created = today } @@ -42,5 +41,10 @@ export const store = new Vuex.Store({ state.article.meta = {} state.article.content = "" } + }, + actions: { + updateText(context, newText){ + context.commit('updateText', newText) + } } })