code refactor

This commit is contained in:
MaysWind
2023-08-13 20:01:44 +08:00
parent 141dc843f3
commit 41a8b8007a
4 changed files with 41 additions and 51 deletions
+11
View File
@@ -1,6 +1,17 @@
import categoryConstants from '@/consts/category.js'; import categoryConstants from '@/consts/category.js';
import transactionConstants from '@/consts/transaction.js'; import transactionConstants from '@/consts/transaction.js';
export function setCategoryModelByAnotherCategory(category, category2) {
category.id = category2.id;
category.type = category2.type;
category.parentId = category2.parentId;
category.name = category2.name;
category.icon = category2.icon;
category.color = category2.color;
category.comment = category2.comment;
category.visible = !category2.hidden;
}
export function transactionTypeToCategoryType(transactionType) { export function transactionTypeToCategoryType(transactionType) {
if (transactionType === transactionConstants.allTransactionTypes.Income) { if (transactionType === transactionConstants.allTransactionTypes.Income) {
return categoryConstants.allCategoryTypes.Income; return categoryConstants.allCategoryTypes.Income;
+13
View File
@@ -1,6 +1,8 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import categoryConstants from '@/consts/category.js'; import categoryConstants from '@/consts/category.js';
import iconConstants from '@/consts/icon.js';
import colorConstants from '@/consts/color.js';
import { isEquals } from '@/lib/common.js'; import { isEquals } from '@/lib/common.js';
import services from '@/lib/services.js'; import services from '@/lib/services.js';
import logger from '@/lib/logger.js'; import logger from '@/lib/logger.js';
@@ -126,6 +128,17 @@ export const useTransactionCategoriesStore = defineStore('transactionCategories'
transactionCategoryListStateInvalid: true, transactionCategoryListStateInvalid: true,
}), }),
actions: { actions: {
generateNewTransactionCategoryModel(type, parentId) {
return {
type: type || categoryConstants.allCategoryTypes.Income,
name: '',
parentId: parentId || '0',
icon: iconConstants.defaultCategoryIconId,
color: colorConstants.defaultCategoryColor,
comment: '',
visible: true
};
},
updateTransactionCategoryListInvalidState(invalidState) { updateTransactionCategoryListInvalidState(invalidState) {
this.transactionCategoryListStateInvalid = invalidState; this.transactionCategoryListStateInvalid = invalidState;
}, },
@@ -76,6 +76,7 @@ import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js';
import categoryConstants from '@/consts/category.js'; import categoryConstants from '@/consts/category.js';
import iconConstants from '@/consts/icon.js'; import iconConstants from '@/consts/icon.js';
import colorConstants from '@/consts/color.js'; import colorConstants from '@/consts/color.js';
import { setCategoryModelByAnotherCategory } from '@/lib/category.js';
export default { export default {
props: [ props: [
@@ -86,19 +87,14 @@ export default {
'open' 'open'
], ],
data() { data() {
const transactionCategoriesStore = useTransactionCategoriesStore();
const newTransactionCategory = transactionCategoriesStore.generateNewTransactionCategoryModel();
return { return {
showState: false, showState: false,
editCategoryId: null, editCategoryId: null,
loading: false, loading: false,
category: { category: newTransactionCategory,
type: categoryConstants.allCategoryTypes.Income,
name: '',
parentId: '0',
icon: iconConstants.defaultCategoryIconId,
color: colorConstants.defaultCategoryColor,
comment: '',
visible: true
},
submitting: false, submitting: false,
resolve: null, resolve: null,
reject: null reject: null
@@ -148,25 +144,19 @@ export default {
self.loading = true; self.loading = true;
self.submitting = false; self.submitting = false;
self.category.id = null; const newTransactionCategory = self.transactionCategoriesStore.generateNewTransactionCategoryModel();
self.category.type = categoryConstants.allCategoryTypes.Income; setCategoryModelByAnotherCategory(self.category, newTransactionCategory);
self.category.parentId = '0';
self.category.name = '';
self.category.icon = iconConstants.defaultCategoryIconId;
self.category.color = colorConstants.defaultCategoryColor;
self.category.comment = '';
self.category.visible = true;
if (options.id) { if (options.id) {
if (options.currentCategory) { if (options.currentCategory) {
self.setCategory(options.currentCategory); setCategoryModelByAnotherCategory(self.category, options.currentCategory);
} }
self.editCategoryId = options.id; self.editCategoryId = options.id;
self.transactionCategoriesStore.getCategory({ self.transactionCategoriesStore.getCategory({
categoryId: self.editCategoryId categoryId: self.editCategoryId
}).then(category => { }).then(category => {
self.setCategory(category); setCategoryModelByAnotherCategory(self.category, category);
self.loading = false; self.loading = false;
}).catch(error => { }).catch(error => {
self.loading = false; self.loading = false;
@@ -265,16 +255,6 @@ export default {
} }
this.showState = false; this.showState = false;
},
setCategory(category) {
this.category.id = category.id;
this.category.type = category.type;
this.category.parentId = category.parentId;
this.category.name = category.name;
this.category.icon = category.icon;
this.category.color = category.color;
this.category.comment = category.comment;
this.category.visible = !category.hidden;
} }
} }
} }
+8 -22
View File
@@ -134,6 +134,7 @@ import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js';
import categoryConstants from '@/consts/category.js'; import categoryConstants from '@/consts/category.js';
import iconConstants from '@/consts/icon.js'; import iconConstants from '@/consts/icon.js';
import colorConstants from '@/consts/color.js'; import colorConstants from '@/consts/color.js';
import { setCategoryModelByAnotherCategory } from '@/lib/category.js';
export default { export default {
props: [ props: [
@@ -141,24 +142,17 @@ export default {
'f7router' 'f7router'
], ],
data() { data() {
const self = this; const transactionCategoriesStore = useTransactionCategoriesStore();
const query = self.f7route.query; const query = this.f7route.query;
const newTransactionCategory = transactionCategoriesStore.generateNewTransactionCategoryModel(parseInt(query.type), query.parentId);
newTransactionCategory.showIconSelectionSheet = false;
newTransactionCategory.showColorSelectionSheet = false;
return { return {
editCategoryId: null, editCategoryId: null,
loading: false, loading: false,
loadingError: null, loadingError: null,
category: { category: newTransactionCategory,
type: parseInt(query.type),
name: '',
parentId: query.parentId,
icon: iconConstants.defaultCategoryIconId,
color: colorConstants.defaultCategoryColor,
comment: '',
visible: true,
showIconSelectionSheet: false,
showColorSelectionSheet: false
},
submitting: false submitting: false
}; };
}, },
@@ -216,15 +210,7 @@ export default {
self.transactionCategoriesStore.getCategory({ self.transactionCategoriesStore.getCategory({
categoryId: self.editCategoryId categoryId: self.editCategoryId
}).then(category => { }).then(category => {
self.category.id = category.id; setCategoryModelByAnotherCategory(self.category, category);
self.category.type = category.type;
self.category.parentId = category.type.parentId;
self.category.name = category.name;
self.category.icon = category.icon;
self.category.color = category.color;
self.category.comment = category.comment;
self.category.visible = !category.hidden;
self.loading = false; self.loading = false;
}).catch(error => { }).catch(error => {
if (error.processed) { if (error.processed) {