From eb16b7fbb835613b01119597818720be36aebf3c Mon Sep 17 00:00:00 2001 From: MaysWind Date: Thu, 23 Jan 2025 21:10:00 +0800 Subject: [PATCH] code refactor --- src/lib/account.ts | 76 ------------------------- src/models/account.ts | 76 +++++++++++++++++++++++++ src/views/desktop/accounts/ListPage.vue | 11 +--- 3 files changed, 79 insertions(+), 84 deletions(-) diff --git a/src/lib/account.ts b/src/lib/account.ts index 4b5ba753..ee818022 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -2,82 +2,6 @@ import { AccountType, AccountCategory } from '@/core/account.ts'; import { PARENT_ACCOUNT_CURRENCY_PLACEHOLDER } from '@/consts/currency.ts'; import { type AccountBalance, type CategorizedAccount, type AccountCategoriesWithVisibleCount, Account } from '@/models/account.ts'; -export function getAccountOrSubAccountId(account: Account, subAccountId: string): string | null { - if (account.type === AccountType.SingleAccount.type) { - return account.id; - } else if (account.type === AccountType.MultiSubAccounts.type && !subAccountId) { - return account.id; - } else if (account.type === AccountType.MultiSubAccounts.type && subAccountId) { - if (!account.childrenAccounts || !account.childrenAccounts.length) { - return null; - } - - for (let i = 0; i < account.childrenAccounts.length; i++) { - const subAccount = account.childrenAccounts[i]; - - if (subAccountId && subAccountId === subAccount.id) { - return subAccount.id; - } - } - - return null; - } else { - return null; - } -} - -export function getAccountOrSubAccountComment(account: Account, subAccountId: string): string | null { - if (account.type === AccountType.SingleAccount.type) { - return account.comment; - } else if (account.type === AccountType.MultiSubAccounts.type && !subAccountId) { - return account.comment; - } else if (account.type === AccountType.MultiSubAccounts.type && subAccountId) { - if (!account.childrenAccounts || !account.childrenAccounts.length) { - return null; - } - - for (let i = 0; i < account.childrenAccounts.length; i++) { - const subAccount = account.childrenAccounts[i]; - - if (subAccountId && subAccountId === subAccount.id) { - return subAccount.comment; - } - } - - return null; - } else { - return null; - } -} - -export function getSubAccountCurrencies(account: Account, showHidden: boolean, subAccountId: string): string[] { - if (!account.childrenAccounts || !account.childrenAccounts.length) { - return []; - } - - const subAccountCurrenciesMap: Record = {}; - const subAccountCurrencies: string[] = []; - - for (let i = 0; i < account.childrenAccounts.length; i++) { - const subAccount = account.childrenAccounts[i]; - - if (!showHidden && subAccount.hidden) { - continue; - } - - if (subAccountId && subAccountId === subAccount.id) { - return [subAccount.currency]; - } else { - if (!subAccountCurrenciesMap[subAccount.currency]) { - subAccountCurrenciesMap[subAccount.currency] = true; - subAccountCurrencies.push(subAccount.currency); - } - } - } - - return subAccountCurrencies; -} - export function getCategorizedAccountsMap(allAccounts: Account[]): Record { const ret: Record = {}; diff --git a/src/models/account.ts b/src/models/account.ts index 262215f0..0ae6537c 100644 --- a/src/models/account.ts +++ b/src/models/account.ts @@ -146,6 +146,82 @@ export class Account implements AccountInfoResponse { }; } + public getAccountOrSubAccountId(subAccountId: string): string | null { + if (this.type === AccountType.SingleAccount.type) { + return this.id; + } else if (this.type === AccountType.MultiSubAccounts.type && !subAccountId) { + return this.id; + } else if (this.type === AccountType.MultiSubAccounts.type && subAccountId) { + if (!this.childrenAccounts || !this.childrenAccounts.length) { + return null; + } + + for (let i = 0; i < this.childrenAccounts.length; i++) { + const subAccount = this.childrenAccounts[i]; + + if (subAccountId && subAccountId === subAccount.id) { + return subAccount.id; + } + } + + return null; + } else { + return null; + } + } + + public getAccountOrSubAccountComment(subAccountId: string): string | null { + if (this.type === AccountType.SingleAccount.type) { + return this.comment; + } else if (this.type === AccountType.MultiSubAccounts.type && !subAccountId) { + return this.comment; + } else if (this.type === AccountType.MultiSubAccounts.type && subAccountId) { + if (!this.childrenAccounts || !this.childrenAccounts.length) { + return null; + } + + for (let i = 0; i < this.childrenAccounts.length; i++) { + const subAccount = this.childrenAccounts[i]; + + if (subAccountId && subAccountId === subAccount.id) { + return subAccount.comment; + } + } + + return null; + } else { + return null; + } + } + + public getSubAccountCurrencies(showHidden: boolean, subAccountId: string): string[] { + if (!this.childrenAccounts || !this.childrenAccounts.length) { + return []; + } + + const subAccountCurrenciesMap: Record = {}; + const subAccountCurrencies: string[] = []; + + for (let i = 0; i < this.childrenAccounts.length; i++) { + const subAccount = this.childrenAccounts[i]; + + if (!showHidden && subAccount.hidden) { + continue; + } + + if (subAccountId && subAccountId === subAccount.id) { + return [subAccount.currency]; + } else { + if (!subAccountCurrenciesMap[subAccount.currency]) { + subAccountCurrenciesMap[subAccount.currency] = true; + subAccountCurrencies.push(subAccount.currency); + } + } + } + + return subAccountCurrencies; + } + public createNewSubAccount(currency: string, balanceTime: number): Account { return new Account( '', // id diff --git a/src/views/desktop/accounts/ListPage.vue b/src/views/desktop/accounts/ListPage.vue index 016247e7..c6459fd7 100644 --- a/src/views/desktop/accounts/ListPage.vue +++ b/src/views/desktop/accounts/ListPage.vue @@ -267,11 +267,6 @@ import { useExchangeRatesStore } from '@/stores/exchangeRates.ts'; import { AccountType, AccountCategory } from '@/core/account.ts'; import { isObject } from '@/lib/common.ts'; -import { - getSubAccountCurrencies, - getAccountOrSubAccountId, - getAccountOrSubAccountComment -} from '@/lib/account.ts'; import { mdiEyeOutline, @@ -450,10 +445,10 @@ export default { } }, accountOrSubAccountId(account) { - return getAccountOrSubAccountId(account, this.activeSubAccount[account.id]); + return account.getAccountOrSubAccountId(this.activeSubAccount[account.id]); }, accountComment(account) { - return getAccountOrSubAccountComment(account, this.activeSubAccount[account.id]); + return account.getAccountOrSubAccountComment(this.activeSubAccount[account.id]); }, accountCurrency(account) { const self = this; @@ -461,7 +456,7 @@ export default { if (account.type === self.allAccountTypes.SingleAccount.type) { return self.$locale.getCurrencyName(account.currency); } else if (account.type === self.allAccountTypes.MultiSubAccounts.type) { - const subAccountCurrencies = getSubAccountCurrencies(account, self.showHidden, self.activeSubAccount[account.id]) + const subAccountCurrencies = account.getSubAccountCurrencies(self.showHidden, self.activeSubAccount[account.id]) .map(currencyCode => self.$locale.getCurrencyName(currencyCode)); return self.$locale.joinMultiText(subAccountCurrencies); } else {