From f743e0d77dad9636ee0d030fa8aa884dc2d7dbcb Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Tue, 14 Aug 2018 15:44:27 -0300 Subject: [PATCH] Removes update from api when deletion leaves orphans terms. This is done now only locally. --- .../components/lists/recursive-term-item.vue | 31 +++++++++---------- src/admin/components/lists/terms-list.vue | 18 +---------- src/js/store/modules/taxonomy/actions.js | 8 ++++- src/js/store/modules/taxonomy/mutations.js | 5 +-- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/admin/components/lists/recursive-term-item.vue b/src/admin/components/lists/recursive-term-item.vue index ecf6151c3..d95c596a6 100644 --- a/src/admin/components/lists/recursive-term-item.vue +++ b/src/admin/components/lists/recursive-term-item.vue @@ -123,7 +123,9 @@ export default { ...mapActions('taxonomy', [ 'updateChildTerm', 'deleteChildTerm', - 'fetchChildTerms' + 'fetchChildTerms', + 'clearTerms', + 'updateChildTermLocal' ]), addNewChildTerm() { this.showChildren = true; @@ -205,8 +207,12 @@ export default { title: this.$i18n.get('label_warning'), message: this.$i18n.get('info_warning_selected_term_delete'), onConfirm: () => { + // If all checks passed, term can be deleted - this.deleteChildTerm({taxonomyId: this.taxonomyId, termId: this.term.id, parent: this.term.parent }) + this.deleteChildTerm({ + taxonomyId: this.taxonomyId, + termId: this.term.id, + parent: this.term.parent }) .then(() => { this.totalTerms = this.totalTerms - 1; }) @@ -216,21 +222,14 @@ export default { // Updates parent IDs for orphans if (this.term.children != undefined && this.term.children.length > 0) { - for (let orphanTerm of this.term.children) { - this.updateChildTerm({ - taxonomyId: this.taxonomyId, - termId: orphanTerm.id, - name: orphanTerm.name, - description: orphanTerm.description, - parent: this.term.parent, + for (let orphanTerm of this.term.children) { + this.updateChildTermLocal({ + term: orphanTerm, + parent: this.term.parent, oldParent: this.term.id - }) - .catch((error) => { - this.$console.log(error); - }); - } - } - + }); + } + } }, } }); diff --git a/src/admin/components/lists/terms-list.vue b/src/admin/components/lists/terms-list.vue index e359a896f..9ff2f1d3e 100644 --- a/src/admin/components/lists/terms-list.vue +++ b/src/admin/components/lists/terms-list.vue @@ -344,27 +344,11 @@ export default { deleteBasicTerm(term) { this.deleteTerm({taxonomyId: this.taxonomyId, termId: term.id }) .then(() => { - this.totalTerms = this.totalTerms - 1; + this.searchTerms(this.offset); }) .catch((error) => { this.$console.log(error); }); - - // Updates parent IDs for orphans - for (let orphanTerm of this.termsList) { - if (orphanTerm.parent == term.id) { - this.updateTerm({ - taxonomyId: this.taxonomyId, - termId: orphanTerm.id, - name: orphanTerm.name, - description: orphanTerm.description, - parent: term.parent - }) - .catch((error) => { - this.$console.log(error); - }); - } - } } }, created() { diff --git a/src/js/store/modules/taxonomy/actions.js b/src/js/store/modules/taxonomy/actions.js index 34efb6538..dfff48957 100644 --- a/src/js/store/modules/taxonomy/actions.js +++ b/src/js/store/modules/taxonomy/actions.js @@ -277,6 +277,11 @@ export const updateChildTerm = ({ commit }, { taxonomyId, termId, name, descript }); }; +// Used to update parent changes after deletion only locally +export const updateChildTermLocal = ({ commit }, { term, parent, oldParent }) => { + commit('updateChildTerm', { term: term, parent: parent, oldParent: oldParent }); +}; + export const deleteChildTerm = ({ commit }, { taxonomyId, termId, parent }) => { return new Promise(( resolve, reject ) => { axios.tainacan.delete(`/taxonomy/${taxonomyId}/terms/${termId}?permanently=1`) @@ -294,4 +299,5 @@ export const deleteChildTerm = ({ commit }, { taxonomyId, termId, parent }) => { export const clearTerms = ({ commit }) => { commit('clearTerms'); -}; \ No newline at end of file +}; + diff --git a/src/js/store/modules/taxonomy/mutations.js b/src/js/store/modules/taxonomy/mutations.js index 19f61cccd..418abf144 100644 --- a/src/js/store/modules/taxonomy/mutations.js +++ b/src/js/store/modules/taxonomy/mutations.js @@ -123,10 +123,11 @@ export const updateChildTerm = (state, { term, parent, oldParent }) => { } } } else { + // Removes from old parent - this.deleteChildTerm(term.id, oldParent) + deleteChildTerm(term.id, oldParent) // Adds it to new one - this.addChildTerm(term, parent); + addChildTerm(term, parent); } };