From 868e5ce377e545d08cab15c8fd9589ec8a0c0bd1 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Tue, 2 Feb 2021 23:49:23 +0800 Subject: [PATCH] code refactor --- src/consts/statistics.js | 30 ++++++-- src/views/mobile/statistics/Settings.vue | 27 +------ src/views/mobile/statistics/Transaction.vue | 83 ++++++++------------- 3 files changed, 56 insertions(+), 84 deletions(-) diff --git a/src/consts/statistics.js b/src/consts/statistics.js index f22e5523..f3681e7f 100644 --- a/src/consts/statistics.js +++ b/src/consts/statistics.js @@ -8,12 +8,30 @@ const allChartTypes = { const defaultChartType = allChartTypes.Pie; const allChartDataTypes = { - ExpenseByAccount: 0, - ExpenseByPrimaryCategory: 1, - ExpenseBySecondaryCategory: 2, - IncomeByAccount: 3, - IncomeByPrimaryCategory: 4, - IncomeBySecondaryCategory: 5 + ExpenseByAccount: { + type: 0, + name: 'Expense By Account' + }, + ExpenseByPrimaryCategory: { + type: 1, + name: 'Expense By Primary Category' + }, + ExpenseBySecondaryCategory: { + type: 2, + name: 'Expense By Secondary Category' + }, + IncomeByAccount: { + type: 3, + name: 'Income By Account' + }, + IncomeByPrimaryCategory: { + type: 4, + name: 'Income By Primary Category' + }, + IncomeBySecondaryCategory: { + type: 5, + name: 'Income By Secondary Category' + } }; const defaultChartDataType = allChartDataTypes.ExpenseByPrimaryCategory; diff --git a/src/views/mobile/statistics/Settings.vue b/src/views/mobile/statistics/Settings.vue index 7741a7b8..cef35476 100644 --- a/src/views/mobile/statistics/Settings.vue +++ b/src/views/mobile/statistics/Settings.vue @@ -43,32 +43,7 @@ export default { computed: { allChartDataTypes() { - return [ - { - type: this.$constants.statistics.allChartDataTypes.ExpenseByAccount, - name: 'Expense By Account' - }, - { - type: this.$constants.statistics.allChartDataTypes.ExpenseByPrimaryCategory, - name: 'Expense By Primary Category' - }, - { - type: this.$constants.statistics.allChartDataTypes.ExpenseBySecondaryCategory, - name: 'Expense By Secondary Category' - }, - { - type: this.$constants.statistics.allChartDataTypes.IncomeByAccount, - name: 'Income By Account' - }, - { - type: this.$constants.statistics.allChartDataTypes.IncomeByPrimaryCategory, - name: 'Income By Primary Category' - }, - { - type: this.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory, - name: 'Income By Secondary Category' - }, - ]; + return this.$constants.statistics.allChartDataTypes; }, allDateRanges() { const allDateRanges = []; diff --git a/src/views/mobile/statistics/Transaction.vue b/src/views/mobile/statistics/Transaction.vue index 63e6a527..219564db 100644 --- a/src/views/mobile/statistics/Transaction.vue +++ b/src/views/mobile/statistics/Transaction.vue @@ -53,8 +53,8 @@ v-else-if="!loading" @click="clickPieChartItem" > - {{ $t('Total Expense') }} - {{ $t('Total Income') }} + {{ $t('Total Expense') }} + {{ $t('Total Income') }} {{ statisticsData.totalAmount | currency(defaultCurrency) }} @@ -140,10 +140,10 @@ -
{{ $t('Total Expense') }}
-
{{ $t('Total Income') }}
+
{{ $t('Total Expense') }}
+
{{ $t('Total Income') }}
+ :class="{ 'statistics-list-item-overview-amount': true, 'text-color-teal': query.chartDataType === $constants.statistics.allChartDataTypes.ExpenseByAccount.type || query.chartDataType === $constants.statistics.allChartDataTypes.ExpenseByPrimaryCategory.type || query.chartDataType === $constants.statistics.allChartDataTypes.ExpenseBySecondaryCategory.type, 'text-color-red': query.chartDataType === $constants.statistics.allChartDataTypes.IncomeByAccount.type || query.chartDataType === $constants.statistics.allChartDataTypes.IncomeByPrimaryCategory.type || query.chartDataType === $constants.statistics.allChartDataTypes.IncomeBySecondaryCategory.type }"> {{ statisticsData.totalAmount | currency(defaultCurrency) }}
@@ -276,32 +276,7 @@ export default { return this.$store.state.transactionStatisticsFilter; }, allChartDataTypes() { - return [ - { - type: this.$constants.statistics.allChartDataTypes.ExpenseByAccount, - name: 'Expense By Account' - }, - { - type: this.$constants.statistics.allChartDataTypes.ExpenseByPrimaryCategory, - name: 'Expense By Primary Category' - }, - { - type: this.$constants.statistics.allChartDataTypes.ExpenseBySecondaryCategory, - name: 'Expense By Secondary Category' - }, - { - type: this.$constants.statistics.allChartDataTypes.IncomeByAccount, - name: 'Income By Account' - }, - { - type: this.$constants.statistics.allChartDataTypes.IncomeByPrimaryCategory, - name: 'Income By Primary Category' - }, - { - type: this.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory, - name: 'Income By Secondary Category' - }, - ]; + return this.$constants.statistics.allChartDataTypes; }, allDateRanges() { return this.$constants.datetime.allDateRanges; @@ -319,15 +294,15 @@ export default { continue; } - if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseByAccount || - self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseByPrimaryCategory || - self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseBySecondaryCategory) { + if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseByAccount.type || + self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseByPrimaryCategory.type || + self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseBySecondaryCategory.type) { if (item.category.type !== self.$constants.category.allCategoryTypes.Expense) { continue; } - } else if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByAccount || - self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByPrimaryCategory || - self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory) { + } else if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByAccount.type || + self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByPrimaryCategory.type || + self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory.type) { if (item.category.type !== self.$constants.category.allCategoryTypes.Income) { continue; } @@ -335,8 +310,8 @@ export default { continue; } - if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseByAccount || - self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByAccount) { + if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseByAccount.type || + self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByAccount.type) { if (self.$utilities.isNumber(item.amountInDefaultCurrency)) { let data = combinedData[item.account.id]; @@ -357,8 +332,8 @@ export default { allAmount += item.amountInDefaultCurrency; combinedData[item.account.id] = data; } - } else if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseByPrimaryCategory || - self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByPrimaryCategory) { + } else if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseByPrimaryCategory.type || + self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByPrimaryCategory.type) { if (self.$utilities.isNumber(item.amountInDefaultCurrency)) { let data = combinedData[item.primaryCategory.id]; @@ -379,8 +354,8 @@ export default { allAmount += item.amountInDefaultCurrency; combinedData[item.primaryCategory.id] = data; } - } else if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseBySecondaryCategory || - self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory) { + } else if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.ExpenseBySecondaryCategory.type || + self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory.type) { if (self.$utilities.isNumber(item.amountInDefaultCurrency)) { let data = combinedData[item.category.id]; @@ -439,7 +414,7 @@ export default { let defaultChartDataType = self.$settings.getStatisticsDefaultChartDataType(); - if (defaultChartDataType < self.$constants.statistics.allChartDataTypes.ExpenseByAccount || defaultChartDataType > self.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory) { + if (defaultChartDataType < self.$constants.statistics.allChartDataTypes.ExpenseByAccount.type || defaultChartDataType > self.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory.type) { defaultChartDataType = self.$constants.statistics.defaultChartDataType; } @@ -637,26 +612,30 @@ export default { }, filters: { chartDataTypeName(dataType, allChartDataTypes) { - for (let i = 0; i < allChartDataTypes.length; i++) { - if (allChartDataTypes[i].type === dataType) { - return allChartDataTypes[i].name; + for (let chartDataTypeField in allChartDataTypes) { + if (!Object.prototype.hasOwnProperty.call(allChartDataTypes, chartDataTypeField)) { + return; + } + + if (allChartDataTypes[chartDataTypeField].type === dataType) { + return allChartDataTypes[chartDataTypeField].name; } } - + return 'Statistics'; }, itemLinkUrl(item, query, allChartDataTypes) { const querys = []; - if (query.chartDataType === allChartDataTypes.IncomeByAccount || query.chartDataType === allChartDataTypes.IncomeByPrimaryCategory || query.chartDataType === allChartDataTypes.IncomeBySecondaryCategory) { + if (query.chartDataType === allChartDataTypes.IncomeByAccount.type || query.chartDataType === allChartDataTypes.IncomeByPrimaryCategory.type || query.chartDataType === allChartDataTypes.IncomeBySecondaryCategory.type) { querys.push('type=2'); - } else if (query.chartDataType === allChartDataTypes.ExpenseByAccount || query.chartDataType === allChartDataTypes.ExpenseByPrimaryCategory || query.chartDataType === allChartDataTypes.ExpenseBySecondaryCategory) { + } else if (query.chartDataType === allChartDataTypes.ExpenseByAccount.type || query.chartDataType === allChartDataTypes.ExpenseByPrimaryCategory.type || query.chartDataType === allChartDataTypes.ExpenseBySecondaryCategory.type) { querys.push('type=3'); } - if (query.chartDataType === allChartDataTypes.IncomeByAccount || query.chartDataType === allChartDataTypes.ExpenseByAccount) { + if (query.chartDataType === allChartDataTypes.IncomeByAccount.type || query.chartDataType === allChartDataTypes.ExpenseByAccount.type) { querys.push('accountId=' + item.id); - } else if (query.chartDataType === allChartDataTypes.IncomeByPrimaryCategory || query.chartDataType === allChartDataTypes.IncomeBySecondaryCategory || query.chartDataType === allChartDataTypes.ExpenseByPrimaryCategory || query.chartDataType === allChartDataTypes.ExpenseBySecondaryCategory) { + } else if (query.chartDataType === allChartDataTypes.IncomeByPrimaryCategory.type || query.chartDataType === allChartDataTypes.IncomeBySecondaryCategory.type || query.chartDataType === allChartDataTypes.ExpenseByPrimaryCategory.type || query.chartDataType === allChartDataTypes.ExpenseBySecondaryCategory.type) { querys.push('categoryId=' + item.id); }