Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Jun 30, 2023
1 parent 96c233d commit ee399d8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 56 deletions.
130 changes: 76 additions & 54 deletions src/stores/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,11 @@ import iconConstants from '@/consts/icon.js';
import colorConstants from '@/consts/color.js';
import services from '@/lib/services.js';
import logger from '@/lib/logger.js';
import { isNumber, isObject } from '@/lib/common.js';

function loadTransactionStatistics(state, { statistics, defaultCurrency }) {
if (statistics && statistics.items && statistics.items.length) {
const accountsStore = useAccountsStore();
const transactionCategoriesStore = useTransactionCategoriesStore();
const exchangeRatesStore = useExchangeRatesStore();

for (let i = 0; i < statistics.items.length; i++) {
const item = statistics.items[i];

if (item.accountId) {
item.account = accountsStore.allAccountsMap[item.accountId];
}

if (item.account && item.account.parentId !== '0') {
item.primaryAccount = accountsStore.allAccountsMap[item.account.parentId];
} else {
item.primaryAccount = item.account;
}

if (item.categoryId) {
item.category = transactionCategoriesStore.allTransactionCategoriesMap[item.categoryId];
}

if (item.category && item.category.parentId !== '0') {
item.primaryCategory = transactionCategoriesStore.allTransactionCategoriesMap[item.category.parentId];
} else {
item.primaryCategory = item.category;
}

if (item.account && item.account.currency !== defaultCurrency) {
const amount = exchangeRatesStore.getExchangedAmount(item.amount, item.account.currency, defaultCurrency);

if (isNumber(amount)) {
item.amountInDefaultCurrency = Math.floor(amount);
}
} else if (item.account && item.account.currency === defaultCurrency) {
item.amountInDefaultCurrency = item.amount;
} else {
item.amountInDefaultCurrency = null;
}
}
}

state.transactionStatistics = statistics;
}
import {
isEquals,
isNumber,
isObject
} from '@/lib/common.js';

export const useStatisticsStore = defineStore('statistics', {
state: () => ({
Expand All @@ -70,10 +28,72 @@ export const useStatisticsStore = defineStore('statistics', {
filterAccountIds: {},
filterCategoryIds: {}
},
transactionStatistics: [],
transactionStatisticsData: {},
transactionStatisticsStateInvalid: true
}),
getters: {
transactionStatistics(state) {
const statistics = state.transactionStatisticsData;
const finalStatistics = {
startTime: statistics.startTime,
endTime: statistics.endTime,
items: []
};

if (statistics && statistics.items && statistics.items.length) {
const userStore = useUserStore();
const accountsStore = useAccountsStore();
const transactionCategoriesStore = useTransactionCategoriesStore();
const exchangeRatesStore = useExchangeRatesStore();

const defaultCurrency = userStore.currentUserDefaultCurrency;

for (let i = 0; i < statistics.items.length; i++) {
const dataItem = statistics.items[i];
const item = {
categoryId: dataItem.categoryId,
accountId: dataItem.accountId,
amount: dataItem.amount
};

if (item.accountId) {
item.account = accountsStore.allAccountsMap[item.accountId];
}

if (item.account && item.account.parentId !== '0') {
item.primaryAccount = accountsStore.allAccountsMap[item.account.parentId];
} else {
item.primaryAccount = item.account;
}

if (item.categoryId) {
item.category = transactionCategoriesStore.allTransactionCategoriesMap[item.categoryId];
}

if (item.category && item.category.parentId !== '0') {
item.primaryCategory = transactionCategoriesStore.allTransactionCategoriesMap[item.category.parentId];
} else {
item.primaryCategory = item.category;
}

if (item.account && item.account.currency !== defaultCurrency) {
const amount = exchangeRatesStore.getExchangedAmount(item.amount, item.account.currency, defaultCurrency);

if (isNumber(amount)) {
item.amountInDefaultCurrency = Math.floor(amount);
}
} else if (item.account && item.account.currency === defaultCurrency) {
item.amountInDefaultCurrency = item.amount;
} else {
item.amountInDefaultCurrency = null;
}

finalStatistics.items.push(item);
}
}

return finalStatistics;
},
statisticsItemsByTransactionStatisticsData(state) {
if (!state.transactionStatistics || !state.transactionStatistics.items) {
return null;
Expand Down Expand Up @@ -381,7 +401,7 @@ export const useStatisticsStore = defineStore('statistics', {
this.transactionStatisticsFilter.sortingType = filter.sortingType;
}
},
loadTransactionStatistics({ defaultCurrency }) {
loadTransactionStatistics({ force }) {
const self = this;

return new Promise((resolve, reject) => {
Expand All @@ -396,15 +416,17 @@ export const useStatisticsStore = defineStore('statistics', {
return;
}

loadTransactionStatistics(self, {
statistics: data.result,
defaultCurrency: defaultCurrency
});

if (self.transactionStatisticsStateInvalid) {
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);
}).catch(error => {
logger.error('failed to get transaction statistics', error);
Expand Down
4 changes: 2 additions & 2 deletions src/views/mobile/statistics/TransactionPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export default {
self.transactionCategoriesStore.loadAllCategories({ force: false })
]).then(() => {
return self.statisticsStore.loadTransactionStatistics({
defaultCurrency: self.defaultCurrency
force: false
});
}).then(() => {
self.loading = false;
Expand Down Expand Up @@ -507,7 +507,7 @@ export default {
self.query.chartDataType === self.allChartDataTypes.IncomeByPrimaryCategory.type ||
self.query.chartDataType === self.allChartDataTypes.IncomeBySecondaryCategory.type) {
dispatchPromise = self.statisticsStore.loadTransactionStatistics({
defaultCurrency: self.defaultCurrency
force: force
});
} else if (self.query.chartDataType === self.allChartDataTypes.AccountTotalAssets.type ||
self.query.chartDataType === self.allChartDataTypes.AccountTotalLiabilities.type) {
Expand Down

0 comments on commit ee399d8

Please sign in to comment.