From 7b70b2db29a2645684797ae76d20e822a2a5cd0d Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 7 Jul 2024 11:47:48 +0800 Subject: [PATCH] only show categories with specified type in category filter dialog / page --- src/lib/category.js | 10 +++++++++- src/lib/common.js | 10 ++++++++++ .../common/cards/CategoryFilterSettingsCard.vue | 12 ++++++++++-- src/views/desktop/transactions/ListPage.vue | 9 ++++++++- .../mobile/settings/CategoryFilterSettingsPage.vue | 10 ++++++++-- src/views/mobile/transactions/ListPage.vue | 8 +++++++- 6 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/lib/category.js b/src/lib/category.js index b5b1afb9..cbe76195 100644 --- a/src/lib/category.js +++ b/src/lib/category.js @@ -62,8 +62,12 @@ export function getTransactionSecondaryCategoryName(categoryId, allCategories) { return ''; } -export function allVisibleTransactionCategories(allTransactionCategories) { +export function allVisibleTransactionCategories(allTransactionCategories, allowCategoryTypes) { const ret = {}; + const hasAllowCategoryTypes = allowCategoryTypes + && (allowCategoryTypes[categoryConstants.allCategoryTypes.Income.toString()] + || allowCategoryTypes[categoryConstants.allCategoryTypes.Expense.toString()] + || allowCategoryTypes[categoryConstants.allCategoryTypes.Transfer.toString()]); for (let key in categoryConstants.allCategoryTypes) { if (!Object.prototype.hasOwnProperty.call(categoryConstants.allCategoryTypes, key)) { @@ -76,6 +80,10 @@ export function allVisibleTransactionCategories(allTransactionCategories) { continue; } + if (hasAllowCategoryTypes && !allowCategoryTypes[categoryType]) { + continue; + } + const allCategories = allTransactionCategories[categoryType]; const visibleCategories = []; const allVisibleSubCategories = {}; diff --git a/src/lib/common.js b/src/lib/common.js index b6c5f23f..5a1851e9 100644 --- a/src/lib/common.js +++ b/src/lib/common.js @@ -373,6 +373,16 @@ export function objectToArray(object) { return ret; } +export function arrayItemToObjectField(array, value) { + const ret = {}; + + for (let i = 0; i < array.length; i++) { + ret[array[i]] = value; + } + + return ret; +} + export function categorizedArrayToPlainArray(object) { const ret = []; diff --git a/src/views/desktop/common/cards/CategoryFilterSettingsCard.vue b/src/views/desktop/common/cards/CategoryFilterSettingsCard.vue index e08f277a..481a6f58 100644 --- a/src/views/desktop/common/cards/CategoryFilterSettingsCard.vue +++ b/src/views/desktop/common/cards/CategoryFilterSettingsCard.vue @@ -127,7 +127,7 @@ import { useTransactionsStore } from '@/stores/transaction.js'; import { useStatisticsStore } from '@/stores/statistics.js'; import categoryConstants from '@/consts/category.js'; -import { copyObjectTo } from '@/lib/common.js'; +import { copyObjectTo, arrayItemToObjectField } from '@/lib/common.js'; import { allVisibleTransactionCategories, hasAnyAvailableCategory, @@ -151,6 +151,7 @@ import { export default { props: [ 'dialogMode', + 'categoryTypes', 'type', 'autoSave' ], @@ -190,8 +191,11 @@ export default { return 'Apply'; } }, + allowCategoryTypes() { + return this.categoryTypes ? arrayItemToObjectField(this.categoryTypes.split(','), true) : null; + }, allVisibleTransactionCategories() { - return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories); + return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories, this.allowCategoryTypes); }, hasAnyAvailableCategory() { return hasAnyAvailableCategory(this.allVisibleTransactionCategories); @@ -217,6 +221,10 @@ export default { const category = self.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; + if (self.allowCategoryTypes && !self.allowCategoryTypes[category.type]) { + continue; + } + if (this.type === 'transactionListCurrent' && self.transactionsStore.allFilterCategoryIdsCount > 0) { allCategoryIds[category.id] = true; } else { diff --git a/src/views/desktop/transactions/ListPage.vue b/src/views/desktop/transactions/ListPage.vue index c86f378a..349d21ee 100644 --- a/src/views/desktop/transactions/ListPage.vue +++ b/src/views/desktop/transactions/ListPage.vue @@ -413,7 +413,7 @@ - @@ -653,6 +653,13 @@ export default { queryMonthlyData() { return isDateRangeMatchOneMonth(this.query.minTime, this.query.maxTime); }, + allowCategoryTypes() { + if (this.allTransactionTypes.Income <= this.query.type && this.query.type <= this.allTransactionTypes.Transfer) { + return transactionTypeToCategoryType(this.query.type).toString(); + } + + return ''; + }, countPerPage: { get: function () { if (this.temporaryCountPerPage) { diff --git a/src/views/mobile/settings/CategoryFilterSettingsPage.vue b/src/views/mobile/settings/CategoryFilterSettingsPage.vue index dee19633..a7a5a8f2 100644 --- a/src/views/mobile/settings/CategoryFilterSettingsPage.vue +++ b/src/views/mobile/settings/CategoryFilterSettingsPage.vue @@ -130,7 +130,7 @@ import { useTransactionsStore } from '@/stores/transaction.js'; import { useStatisticsStore } from '@/stores/statistics.js'; import categoryConstants from '@/consts/category.js'; -import { copyObjectTo } from '@/lib/common.js'; +import { copyObjectTo, arrayItemToObjectField } from '@/lib/common.js'; import { allVisibleTransactionCategories, hasAnyAvailableCategory, @@ -156,6 +156,7 @@ export default { loading: true, loadingError: null, type: null, + allowCategoryTypes: null, filterCategoryIds: {}, collapseStates: self.getCollapseStates(), showMoreActionSheet: false @@ -178,7 +179,7 @@ export default { } }, allVisibleTransactionCategories() { - return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories); + return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories, this.allowCategoryTypes); }, hasAnyAvailableCategory() { return hasAnyAvailableCategory(this.allVisibleTransactionCategories); @@ -192,6 +193,7 @@ export default { const query = self.f7route.query; self.type = query.type; + self.allowCategoryTypes = query.allowCategoryTypes ? arrayItemToObjectField(query.allowCategoryTypes.split(','), true) : null; self.transactionCategoriesStore.loadAllCategories({ force: false @@ -207,6 +209,10 @@ export default { const category = self.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; + if (self.allowCategoryTypes && !self.allowCategoryTypes[category.type]) { + continue; + } + if (this.type === 'transactionListCurrent' && self.transactionsStore.allFilterCategoryIdsCount > 0) { allCategoryIds[category.id] = true; } else { diff --git a/src/views/mobile/transactions/ListPage.vue b/src/views/mobile/transactions/ListPage.vue index 9a398d7e..868de281 100644 --- a/src/views/mobile/transactions/ListPage.vue +++ b/src/views/mobile/transactions/ListPage.vue @@ -906,7 +906,13 @@ export default { } }, filterMultipleCategories() { - this.f7router.navigate('/settings/filter/category?type=transactionListCurrent'); + let navigateUrl = '/settings/filter/category?type=transactionListCurrent'; + + if (this.allTransactionTypes.Income <= this.query.type && this.query.type <= this.allTransactionTypes.Transfer) { + navigateUrl += '&allowCategoryTypes=' + transactionTypeToCategoryType(this.query.type); + } + + this.f7router.navigate(navigateUrl); }, filterMultipleAccounts() { this.f7router.navigate('/settings/filter/account?type=transactionListCurrent');