code refactor

This commit is contained in:
MaysWind
2023-07-18 00:41:26 +08:00
parent 99e55e730e
commit a372d1fb60
3 changed files with 49 additions and 72 deletions
+47
View File
@@ -1,10 +1,12 @@
import { defineStore } from 'pinia';
import { useSettingsStore } from './setting.js';
import { useUserStore } from './user.js';
import { useAccountsStore } from './account.js';
import { useTransactionCategoriesStore } from './transactionCategory.js';
import { useExchangeRatesStore } from './exchangeRates.js';
import datetimeConstants from '@/consts/datetime.js';
import statisticsConstants from '@/consts/statistics.js';
import categoryConstants from '@/consts/category.js';
import iconConstants from '@/consts/icon.js';
@@ -16,6 +18,9 @@ import {
isNumber,
isObject
} from '@/lib/common.js';
import {
getDateRangeByDateType
} from '@/lib/datetime.js';
export const useStatisticsStore = defineStore('statistics', {
state: () => ({
@@ -413,6 +418,48 @@ export const useStatisticsStore = defineStore('statistics', {
this.transactionStatisticsStateInvalid = true;
},
initTransactionStatisticsFilter(filter) {
if (!filter) {
const settingsStore = useSettingsStore();
const userStore = useUserStore();
let defaultChartType = settingsStore.appSettings.statistics.defaultChartType;
if (defaultChartType !== statisticsConstants.allChartTypes.Pie && defaultChartType !== statisticsConstants.allChartTypes.Bar) {
defaultChartType = statisticsConstants.defaultChartType;
}
let defaultChartDataType = settingsStore.appSettings.statistics.defaultChartDataType;
if (defaultChartDataType < statisticsConstants.allChartDataTypes.ExpenseByAccount.type || defaultChartDataType > statisticsConstants.allChartDataTypes.AccountTotalLiabilities.type) {
defaultChartDataType = statisticsConstants.defaultChartDataType;
}
let defaultDateRange = settingsStore.appSettings.statistics.defaultDataRangeType;
if (defaultDateRange < datetimeConstants.allDateRanges.All.type || defaultDateRange >= datetimeConstants.allDateRanges.Custom.type) {
defaultDateRange = statisticsConstants.defaultDataRangeType;
}
let defaultSortType = settingsStore.appSettings.statistics.defaultSortingType;
if (defaultSortType < statisticsConstants.allSortingTypes.Amount.type || defaultSortType > statisticsConstants.allSortingTypes.Name.type) {
defaultSortType = statisticsConstants.defaultSortingType;
}
const dateRange = getDateRangeByDateType(defaultDateRange, userStore.currentUserFirstDayOfWeek);
filter = {
dateType: dateRange ? dateRange.dateType : undefined,
startTime: dateRange ? dateRange.minTime : undefined,
endTime: dateRange ? dateRange.maxTime : undefined,
chartType: defaultChartType,
chartDataType: defaultChartDataType,
filterAccountIds: settingsStore.appSettings.statistics.defaultAccountFilter || {},
filterCategoryIds: settingsStore.appSettings.statistics.defaultTransactionCategoryFilter || {},
sortingType: defaultSortType,
};
}
if (filter && isNumber(filter.dateType)) {
this.transactionStatisticsFilter.dateType = filter.dateType;
} else {
@@ -365,42 +365,7 @@ export default {
created() {
const self = this;
let defaultChartType = self.settingsStore.appSettings.statistics.defaultChartType;
if (defaultChartType !== self.allChartTypes.Pie && defaultChartType !== self.allChartTypes.Bar) {
defaultChartType = statisticsConstants.defaultChartType;
}
let defaultChartDataType = self.settingsStore.appSettings.statistics.defaultChartDataType;
if (defaultChartDataType < self.allChartDataTypes.ExpenseByAccount.type || defaultChartDataType > self.allChartDataTypes.AccountTotalLiabilities.type) {
defaultChartDataType = statisticsConstants.defaultChartDataType;
}
let defaultDateRange = self.settingsStore.appSettings.statistics.defaultDataRangeType;
if (defaultDateRange < self.allDateRanges.All.type || defaultDateRange >= self.allDateRanges.Custom.type) {
defaultDateRange = statisticsConstants.defaultDataRangeType;
}
let defaultSortType = self.settingsStore.appSettings.statistics.defaultSortingType;
if (defaultSortType < self.allSortingTypes.Amount.type || defaultSortType > self.allSortingTypes.Name.type) {
defaultSortType = statisticsConstants.defaultSortingType;
}
const dateRange = getDateRangeByDateType(defaultDateRange, self.firstDayOfWeek);
self.statisticsStore.initTransactionStatisticsFilter({
dateType: dateRange ? dateRange.dateType : undefined,
startTime: dateRange ? dateRange.minTime : undefined,
endTime: dateRange ? dateRange.maxTime : undefined,
chartType: defaultChartType,
chartDataType: defaultChartDataType,
filterAccountIds: self.settingsStore.appSettings.statistics.defaultAccountFilter || {},
filterCategoryIds: self.settingsStore.appSettings.statistics.defaultTransactionCategoryFilter || {},
sortingType: defaultSortType,
});
self.statisticsStore.initTransactionStatisticsFilter();
Promise.all([
self.accountsStore.loadAllAccounts({ force: false }),
@@ -355,42 +355,7 @@ export default {
created() {
const self = this;
let defaultChartType = self.settingsStore.appSettings.statistics.defaultChartType;
if (defaultChartType !== self.allChartTypes.Pie && defaultChartType !== self.allChartTypes.Bar) {
defaultChartType = statisticsConstants.defaultChartType;
}
let defaultChartDataType = self.settingsStore.appSettings.statistics.defaultChartDataType;
if (defaultChartDataType < self.allChartDataTypes.ExpenseByAccount.type || defaultChartDataType > self.allChartDataTypes.AccountTotalLiabilities.type) {
defaultChartDataType = statisticsConstants.defaultChartDataType;
}
let defaultDateRange = self.settingsStore.appSettings.statistics.defaultDataRangeType;
if (defaultDateRange < self.allDateRanges.All.type || defaultDateRange >= self.allDateRanges.Custom.type) {
defaultDateRange = statisticsConstants.defaultDataRangeType;
}
let defaultSortType = self.settingsStore.appSettings.statistics.defaultSortingType;
if (defaultSortType < self.allSortingTypes.Amount.type || defaultSortType > self.allSortingTypes.Name.type) {
defaultSortType = statisticsConstants.defaultSortingType;
}
const dateRange = getDateRangeByDateType(defaultDateRange, self.firstDayOfWeek);
self.statisticsStore.initTransactionStatisticsFilter({
dateType: dateRange ? dateRange.dateType : undefined,
startTime: dateRange ? dateRange.minTime : undefined,
endTime: dateRange ? dateRange.maxTime : undefined,
chartType: defaultChartType,
chartDataType: defaultChartDataType,
filterAccountIds: self.settingsStore.appSettings.statistics.defaultAccountFilter || {},
filterCategoryIds: self.settingsStore.appSettings.statistics.defaultTransactionCategoryFilter || {},
sortingType: defaultSortType,
});
self.statisticsStore.initTransactionStatisticsFilter();
Promise.all([
self.accountsStore.loadAllAccounts({ force: false }),