From af355e5b85d58d370a4a42bdc9c1d76117fbca39 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Wed, 29 Jan 2025 22:49:14 +0800 Subject: [PATCH] migrate account filter page to composition API and typescript --- .../settings/AccountFilterSettingPageBase.ts | 160 ++++++++ .../cards/AccountFilterSettingsCard.vue | 377 +++++++----------- .../settings/AccountFilterSettingsPage.vue | 373 +++++++---------- 3 files changed, 442 insertions(+), 468 deletions(-) create mode 100644 src/views/base/settings/AccountFilterSettingPageBase.ts diff --git a/src/views/base/settings/AccountFilterSettingPageBase.ts b/src/views/base/settings/AccountFilterSettingPageBase.ts new file mode 100644 index 00000000..77154645 --- /dev/null +++ b/src/views/base/settings/AccountFilterSettingPageBase.ts @@ -0,0 +1,160 @@ +import { ref, computed } from 'vue'; + +import { useSettingsStore } from '@/stores/setting.ts'; +import { useAccountsStore } from '@/stores/account.ts'; +import { useTransactionsStore } from '@/stores/transaction.ts'; +import { useStatisticsStore } from '@/stores/statistics.ts'; + +import type { Account, AccountCategoriesWithVisibleCount } from '@/models/account.ts'; + +import { copyObjectTo } from '@/lib/common.ts'; +import { + getCategorizedAccountsWithVisibleCount, + selectAccountOrSubAccounts, + isAccountOrSubAccountsAllChecked +} from '@/lib/account.ts'; + +export function useAccountFilterSettingPageBase(type?: string) { + const settingsStore = useSettingsStore(); + const accountsStore = useAccountsStore(); + const transactionsStore = useTransactionsStore(); + const statisticsStore = useStatisticsStore(); + + const loading = ref(true); + const showHidden = ref(false); + const filterAccountIds = ref>({}); + + const title = computed(() => { + if (type === 'statisticsDefault') { + return 'Default Account Filter'; + } else { + return 'Filter Accounts'; + } + }); + + const applyText = computed(() => { + if (type === 'statisticsDefault') { + return 'Save'; + } else { + return 'Apply'; + } + }); + + const allCategorizedAccounts = computed(() => getCategorizedAccountsWithVisibleCount(accountsStore.allCategorizedAccountsMap)); + const hasAnyAvailableAccount = computed(() => accountsStore.allAvailableAccountsCount > 0); + + const hasAnyVisibleAccount = computed(() => { + if (showHidden.value) { + return accountsStore.allAvailableAccountsCount > 0; + } else { + return accountsStore.allVisibleAccountsCount > 0; + } + }); + + function isAccountChecked(account: Account, filterAccountIds: Record): boolean { + return !filterAccountIds[account.id]; + } + + function loadFilterAccountIds(): boolean { + const allAccountIds: Record = {}; + + for (const accountId in accountsStore.allAccountsMap) { + if (!Object.prototype.hasOwnProperty.call(accountsStore.allAccountsMap, accountId)) { + continue; + } + + const account = accountsStore.allAccountsMap[accountId]; + + if (type === 'transactionListCurrent' && transactionsStore.allFilterAccountIdsCount > 0) { + allAccountIds[account.id] = true; + } else { + allAccountIds[account.id] = false; + } + } + + if (type === 'statisticsDefault') { + filterAccountIds.value = copyObjectTo(settingsStore.appSettings.statistics.defaultAccountFilter, allAccountIds) as Record; + return true; + } else if (type === 'statisticsCurrent') { + filterAccountIds.value = copyObjectTo(statisticsStore.transactionStatisticsFilter.filterAccountIds, allAccountIds) as Record; + return true; + } else if (type === 'transactionListCurrent') { + for (const accountId in transactionsStore.allFilterAccountIds) { + if (!Object.prototype.hasOwnProperty.call(transactionsStore.allFilterAccountIds, accountId)) { + continue; + } + + const account = accountsStore.allAccountsMap[accountId]; + + if (account) { + selectAccountOrSubAccounts(allAccountIds, account, false); + } + } + filterAccountIds.value = allAccountIds; + return true; + } else { + return false; + } + } + + function saveFilterAccountIds(): boolean { + const filteredAccountIds: Record = {}; + let isAllSelected = true; + let finalAccountIds = ''; + let changed = true; + + for (const accountId in filterAccountIds.value) { + if (!Object.prototype.hasOwnProperty.call(filterAccountIds.value, accountId)) { + continue; + } + + const account = accountsStore.allAccountsMap[accountId]; + + if (!isAccountOrSubAccountsAllChecked(account, filterAccountIds.value)) { + filteredAccountIds[accountId] = true; + isAllSelected = false; + } else { + if (finalAccountIds.length > 0) { + finalAccountIds += ','; + } + + finalAccountIds += accountId; + } + } + + if (type === 'statisticsDefault') { + settingsStore.setStatisticsDefaultAccountFilter(filteredAccountIds); + } else if (type === 'statisticsCurrent') { + changed = statisticsStore.updateTransactionStatisticsFilter({ + filterAccountIds: filteredAccountIds + }); + } else if (type === 'transactionListCurrent') { + changed = transactionsStore.updateTransactionListFilter({ + accountIds: isAllSelected ? '' : finalAccountIds + }); + + if (changed) { + transactionsStore.updateTransactionListInvalidState(true); + } + } + + return changed; + } + + return { + // states + loading, + showHidden, + filterAccountIds, + // computed states + title, + applyText, + allCategorizedAccounts, + hasAnyAvailableAccount, + hasAnyVisibleAccount, + // functions + isAccountChecked, + loadFilterAccountIds, + saveFilterAccountIds + }; +} diff --git a/src/views/desktop/common/cards/AccountFilterSettingsCard.vue b/src/views/desktop/common/cards/AccountFilterSettingsCard.vue index ad402764..6f999173 100644 --- a/src/views/desktop/common/cards/AccountFilterSettingsCard.vue +++ b/src/views/desktop/common/cards/AccountFilterSettingsCard.vue @@ -3,7 +3,7 @@