code refactor

This commit is contained in:
MaysWind
2023-08-13 20:06:42 +08:00
parent 41a8b8007a
commit 8bed529d05
3 changed files with 25 additions and 37 deletions
+21 -7
View File
@@ -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' });
@@ -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;
+2 -15
View File
@@ -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();