From e2a4e0cb3fec4a8e184070d660894d1460495a14 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 27 Jan 2025 15:41:42 +0800 Subject: [PATCH] migrate account list page to composition API and typescript --- src/locales/helper.js | 4 +- src/stores/account.ts | 2 +- .../base/accounts/AccountListPageBase.ts | 75 ++ src/views/desktop/accounts/ListPage.vue | 642 ++++++++---------- src/views/mobile/accounts/ListPage.vue | 554 +++++++-------- 5 files changed, 639 insertions(+), 638 deletions(-) create mode 100644 src/views/base/accounts/AccountListPageBase.ts diff --git a/src/locales/helper.js b/src/locales/helper.js index c6424ded..758be445 100644 --- a/src/locales/helper.js +++ b/src/locales/helper.js @@ -765,7 +765,6 @@ export function translateError(message, translateFn) { export function i18nFunctions(i18nGlobal) { return { - getCurrencyName: (currencyCode) => getCurrencyName(currencyCode, i18nGlobal.t), getWeekdayShortName: (weekDay) => getWeekdayShortName(weekDay, i18nGlobal.t), getWeekdayLongName: (weekDay) => getWeekdayLongName(weekDay, i18nGlobal.t), getMultiMonthdayShortNames: (monthdays) => getMultiMonthdayShortNames(monthdays, i18nGlobal.t), @@ -787,7 +786,6 @@ export function i18nFunctions(i18nGlobal) { getAdaptiveAmountRate: (userStore, amount1, amount2, fromExchangeRate, toExchangeRate) => getAdaptiveAmountRate(amount1, amount2, fromExchangeRate, toExchangeRate, i18nGlobal.t, userStore), getAllTransactionTagFilterTypes: () => getAllTransactionTagFilterTypes(i18nGlobal.t), getAllSupportedImportFileTypes: () => getAllSupportedImportFileTypes(i18nGlobal, i18nGlobal.t), - getCategorizedAccountsWithDisplayBalance: (allVisibleAccounts, showAccountBalance, defaultCurrency, settingsStore, userStore, exchangeRatesStore) => getCategorizedAccountsWithDisplayBalance(allVisibleAccounts, showAccountBalance, defaultCurrency, userStore, settingsStore, exchangeRatesStore, i18nGlobal.t), - joinMultiText: (textArray) => joinMultiText(textArray, i18nGlobal.t) + getCategorizedAccountsWithDisplayBalance: (allVisibleAccounts, showAccountBalance, defaultCurrency, settingsStore, userStore, exchangeRatesStore) => getCategorizedAccountsWithDisplayBalance(allVisibleAccounts, showAccountBalance, defaultCurrency, userStore, settingsStore, exchangeRatesStore, i18nGlobal.t) }; } diff --git a/src/stores/account.ts b/src/stores/account.ts index 0fb803a4..f2e68ef2 100644 --- a/src/stores/account.ts +++ b/src/stores/account.ts @@ -943,7 +943,7 @@ export const useAccountsStore = defineStore('accounts', () => { }); } - function deleteAccount({ account, beforeResolve }: { account: Account, beforeResolve: BeforeResolveFunction }): Promise { + function deleteAccount({ account, beforeResolve }: { account: Account, beforeResolve?: BeforeResolveFunction }): Promise { return new Promise((resolve, reject) => { services.deleteAccount({ id: account.id diff --git a/src/views/base/accounts/AccountListPageBase.ts b/src/views/base/accounts/AccountListPageBase.ts new file mode 100644 index 00000000..81268d71 --- /dev/null +++ b/src/views/base/accounts/AccountListPageBase.ts @@ -0,0 +1,75 @@ +import { ref, computed } from 'vue'; + +import { useI18n } from '@/locales/helpers.ts'; + +import { useSettingsStore } from '@/stores/setting.ts'; +import { useUserStore } from '@/stores/user.ts'; +import { useAccountsStore } from '@/stores/account.ts'; + +import type { AccountCategory } from '@/core/account.ts'; +import type { Account, CategorizedAccount } from '@/models/account.ts'; + +export function useAccountListPageBaseBase() { + const { formatAmountWithCurrency } = useI18n(); + + const settingsStore = useSettingsStore(); + const userStore = useUserStore(); + const accountsStore = useAccountsStore(); + + const loading = ref(true); + const showHidden = ref(false); + const displayOrderModified = ref(false); + + const showAccountBalance = computed({ + get: () => settingsStore.appSettings.showAccountBalance, + set: (value) => settingsStore.setShowAccountBalance(value) + }); + + const defaultCurrency = computed(() => userStore.currentUserDefaultCurrency); + + const allAccounts = computed(() => accountsStore.allAccounts); + const allCategorizedAccountsMap = computed>(() => accountsStore.allCategorizedAccountsMap); + const allAccountCount = computed(() => accountsStore.allAvailableAccountsCount); + + const netAssets = computed(() => { + const netAssets = accountsStore.getNetAssets(showAccountBalance.value); + return formatAmountWithCurrency(netAssets, defaultCurrency.value); + }); + + const totalAssets = computed(() => { + const totalAssets = accountsStore.getTotalAssets(showAccountBalance.value); + return formatAmountWithCurrency(totalAssets, defaultCurrency.value); + }); + + const totalLiabilities = computed(() => { + const totalLiabilities = accountsStore.getTotalLiabilities(showAccountBalance.value); + return formatAmountWithCurrency(totalLiabilities, defaultCurrency.value); + }); + + function accountCategoryTotalBalance(accountCategory?: AccountCategory): string { + if (!accountCategory) { + return ''; + } + + const totalBalance = accountsStore.getAccountCategoryTotalBalance(showAccountBalance.value, accountCategory); + return formatAmountWithCurrency(totalBalance, defaultCurrency.value); + } + + return { + // states + loading, + showHidden, + displayOrderModified, + // computed states + showAccountBalance, + defaultCurrency, + allAccounts, + allCategorizedAccountsMap, + allAccountCount, + netAssets, + totalAssets, + totalLiabilities, + // functions + accountCategoryTotalBalance + }; +} diff --git a/src/views/desktop/accounts/ListPage.vue b/src/views/desktop/accounts/ListPage.vue index c6459fd7..d850d0a7 100644 --- a/src/views/desktop/accounts/ListPage.vue +++ b/src/views/desktop/accounts/ListPage.vue @@ -5,21 +5,21 @@
- {{ $t('Net assets') }} + {{ tt('Net assets') }} - {{ $t('Total liabilities') }} + {{ tt('Total liabilities') }} - {{ $t('Total assets') }} + {{ tt('Total assets') }}
{{ accountCategoryTotalBalance(accountCategory) }} @@ -39,7 +39,7 @@ - {{ $t(accountCategory.name) }} + {{ tt(accountCategory.name) }}
@@ -54,19 +54,19 @@ :ripple="false" :icon="true" @click="showNav = !showNav"> - {{ $t('Account List') }} + {{ tt('Account List') }} {{ $t('Add') }} + :disabled="loading" @click="add">{{ tt('Add') }} {{ $t('Save Display Order') }} + v-if="displayOrderModified">{{ tt('Save Display Order') }} + class="ml-2" :icon="true" :loading="loading" @click="reload(true)"> - {{ $t('Refresh') }} + {{ tt('Refresh') }} @@ -87,18 +87,18 @@ - {{ $t('Balance') }} - - {{ activeAccountCategoryTotalBalance }} + {{ tt('Balance') }} + + {{ activeAccountCategoryTotalBalance }} - {{ showAccountBalance ? $t('Hide Account Balance') : $t('Show Account Balance') }} + {{ showAccountBalance ? tt('Hide Account Balance') : tt('Show Account Balance') }} - +