code refactor

This commit is contained in:
MaysWind
2023-07-01 02:19:04 +08:00
parent 96c233d5c5
commit ee399d8a08
2 changed files with 78 additions and 56 deletions
+48 -26
View File
@@ -11,16 +11,50 @@ import iconConstants from '@/consts/icon.js';
import colorConstants from '@/consts/color.js'; import colorConstants from '@/consts/color.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';
import { isNumber, isObject } from '@/lib/common.js'; import {
isEquals,
isNumber,
isObject
} from '@/lib/common.js';
export const useStatisticsStore = defineStore('statistics', {
state: () => ({
transactionStatisticsFilter: {
dateType: statisticsConstants.defaultDataRangeType,
startTime: 0,
endTime: 0,
chartType: statisticsConstants.defaultChartType,
chartDataType: statisticsConstants.defaultChartDataType,
filterAccountIds: {},
filterCategoryIds: {}
},
transactionStatisticsData: {},
transactionStatisticsStateInvalid: true
}),
getters: {
transactionStatistics(state) {
const statistics = state.transactionStatisticsData;
const finalStatistics = {
startTime: statistics.startTime,
endTime: statistics.endTime,
items: []
};
function loadTransactionStatistics(state, { statistics, defaultCurrency }) {
if (statistics && statistics.items && statistics.items.length) { if (statistics && statistics.items && statistics.items.length) {
const userStore = useUserStore();
const accountsStore = useAccountsStore(); const accountsStore = useAccountsStore();
const transactionCategoriesStore = useTransactionCategoriesStore(); const transactionCategoriesStore = useTransactionCategoriesStore();
const exchangeRatesStore = useExchangeRatesStore(); const exchangeRatesStore = useExchangeRatesStore();
const defaultCurrency = userStore.currentUserDefaultCurrency;
for (let i = 0; i < statistics.items.length; i++) { for (let i = 0; i < statistics.items.length; i++) {
const item = statistics.items[i]; const dataItem = statistics.items[i];
const item = {
categoryId: dataItem.categoryId,
accountId: dataItem.accountId,
amount: dataItem.amount
};
if (item.accountId) { if (item.accountId) {
item.account = accountsStore.allAccountsMap[item.accountId]; item.account = accountsStore.allAccountsMap[item.accountId];
@@ -53,27 +87,13 @@ function loadTransactionStatistics(state, { statistics, defaultCurrency }) {
} else { } else {
item.amountInDefaultCurrency = null; item.amountInDefaultCurrency = null;
} }
finalStatistics.items.push(item);
} }
} }
state.transactionStatistics = statistics; return finalStatistics;
}
export const useStatisticsStore = defineStore('statistics', {
state: () => ({
transactionStatisticsFilter: {
dateType: statisticsConstants.defaultDataRangeType,
startTime: 0,
endTime: 0,
chartType: statisticsConstants.defaultChartType,
chartDataType: statisticsConstants.defaultChartDataType,
filterAccountIds: {},
filterCategoryIds: {}
}, },
transactionStatistics: [],
transactionStatisticsStateInvalid: true
}),
getters: {
statisticsItemsByTransactionStatisticsData(state) { statisticsItemsByTransactionStatisticsData(state) {
if (!state.transactionStatistics || !state.transactionStatistics.items) { if (!state.transactionStatistics || !state.transactionStatistics.items) {
return null; return null;
@@ -381,7 +401,7 @@ export const useStatisticsStore = defineStore('statistics', {
this.transactionStatisticsFilter.sortingType = filter.sortingType; this.transactionStatisticsFilter.sortingType = filter.sortingType;
} }
}, },
loadTransactionStatistics({ defaultCurrency }) { loadTransactionStatistics({ force }) {
const self = this; const self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -396,15 +416,17 @@ export const useStatisticsStore = defineStore('statistics', {
return; return;
} }
loadTransactionStatistics(self, {
statistics: data.result,
defaultCurrency: defaultCurrency
});
if (self.transactionStatisticsStateInvalid) { if (self.transactionStatisticsStateInvalid) {
self.updateTransactionStatisticsInvalidState(false); self.updateTransactionStatisticsInvalidState(false);
} }
if (force && data.result && isEquals(self.transactionStatisticsData, data.result)) {
reject({ message: 'Data is up to date' });
return;
}
self.transactionStatisticsData = data.result;
resolve(data.result); resolve(data.result);
}).catch(error => { }).catch(error => {
logger.error('failed to get transaction statistics', error); logger.error('failed to get transaction statistics', error);
@@ -474,7 +474,7 @@ export default {
self.transactionCategoriesStore.loadAllCategories({ force: false }) self.transactionCategoriesStore.loadAllCategories({ force: false })
]).then(() => { ]).then(() => {
return self.statisticsStore.loadTransactionStatistics({ return self.statisticsStore.loadTransactionStatistics({
defaultCurrency: self.defaultCurrency force: false
}); });
}).then(() => { }).then(() => {
self.loading = false; self.loading = false;
@@ -507,7 +507,7 @@ export default {
self.query.chartDataType === self.allChartDataTypes.IncomeByPrimaryCategory.type || self.query.chartDataType === self.allChartDataTypes.IncomeByPrimaryCategory.type ||
self.query.chartDataType === self.allChartDataTypes.IncomeBySecondaryCategory.type) { self.query.chartDataType === self.allChartDataTypes.IncomeBySecondaryCategory.type) {
dispatchPromise = self.statisticsStore.loadTransactionStatistics({ dispatchPromise = self.statisticsStore.loadTransactionStatistics({
defaultCurrency: self.defaultCurrency force: force
}); });
} else if (self.query.chartDataType === self.allChartDataTypes.AccountTotalAssets.type || } else if (self.query.chartDataType === self.allChartDataTypes.AccountTotalAssets.type ||
self.query.chartDataType === self.allChartDataTypes.AccountTotalLiabilities.type) { self.query.chartDataType === self.allChartDataTypes.AccountTotalLiabilities.type) {