From 8bed529d057d0a16884db50c5cbaaf0ca454d1ad Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 13 Aug 2023 20:06:42 +0800 Subject: [PATCH] code refactor --- src/stores/transactionCategory.js | 28 ++++++++++++++----- .../categories/list/dialogs/EditDialog.vue | 17 ++--------- src/views/mobile/categories/EditPage.vue | 17 ++--------- 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/src/stores/transactionCategory.js b/src/stores/transactionCategory.js index 4515110b..c2cb281b 100644 --- a/src/stores/transactionCategory.js +++ b/src/stores/transactionCategory.js @@ -248,23 +248,37 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories' }); }); }, - saveCategory({ category }) { + saveCategory({ category, isEdit }) { const self = this; + const submitCategory = { + type: category.type, + name: category.name, + parentId: category.parentId, + icon: category.icon, + color: category.color, + comment: category.comment + }; + + if (isEdit) { + submitCategory.id = category.id; + submitCategory.hidden = !category.visible; + } + return new Promise((resolve, reject) => { let promise = null; - if (!category.id) { - promise = services.addTransactionCategory(category); + if (!submitCategory.id) { + promise = services.addTransactionCategory(submitCategory); } else { - promise = services.modifyTransactionCategory(category); + promise = services.modifyTransactionCategory(submitCategory); } promise.then(response => { const data = response.data; if (!data || !data.success || !data.result) { - if (!category.id) { + if (!submitCategory.id) { reject({ message: 'Unable to add category' }); } else { reject({ message: 'Unable to save category' }); @@ -276,7 +290,7 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories' data.result.subCategories = []; } - if (!category.id) { + if (!submitCategory.id) { addCategoryToTransactionCategoryList(self, data.result); } else { updateCategoryInTransactionCategoryList(self, data.result); @@ -289,7 +303,7 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories' if (error.response && error.response.data && error.response.data.errorMessage) { reject({ error: error.response.data }); } else if (!error.processed) { - if (!category.id) { + if (!submitCategory.id) { reject({ message: 'Unable to add category' }); } else { reject({ message: 'Unable to save category' }); diff --git a/src/views/desktop/categories/list/dialogs/EditDialog.vue b/src/views/desktop/categories/list/dialogs/EditDialog.vue index b25478c2..028973fe 100644 --- a/src/views/desktop/categories/list/dialogs/EditDialog.vue +++ b/src/views/desktop/categories/list/dialogs/EditDialog.vue @@ -209,22 +209,9 @@ export default { self.submitting = true; - const submitCategory = { - type: self.category.type, - name: self.category.name, - parentId: self.category.parentId, - icon: self.category.icon, - color: self.category.color, - comment: self.category.comment - }; - - if (self.editCategoryId) { - submitCategory.id = self.category.id; - submitCategory.hidden = !self.category.visible; - } - self.transactionCategoriesStore.saveCategory({ - category: submitCategory + category: self.category, + isEdit: !!self.editCategoryId }).then(() => { self.submitting = false; diff --git a/src/views/mobile/categories/EditPage.vue b/src/views/mobile/categories/EditPage.vue index 8b0a4b29..259bf6c3 100644 --- a/src/views/mobile/categories/EditPage.vue +++ b/src/views/mobile/categories/EditPage.vue @@ -252,22 +252,9 @@ export default { self.submitting = true; self.$showLoading(() => self.submitting); - const submitCategory = { - type: self.category.type, - name: self.category.name, - parentId: self.category.parentId, - icon: self.category.icon, - color: self.category.color, - comment: self.category.comment - }; - - if (self.editCategoryId) { - submitCategory.id = self.category.id; - submitCategory.hidden = !self.category.visible; - } - self.transactionCategoriesStore.saveCategory({ - category: submitCategory + category: self.category, + isEdit: !!self.editCategoryId }).then(() => { self.submitting = false; self.$hideLoading();