Skip to content

Open source personal finance application

feat(WalletTotal): refactor

ilkome823bfce39d7e
diff --git a/components/stat/Filter.vue b/components/stat/Filter.vue
index c64a3232..1d048110 100644
--- a/components/stat/Filter.vue
+++ b/components/stat/Filter.vue
@@ -56,7 +56,7 @@ const onClearFilter = () => {
.text-xs {{ walletItem.name }}
.text-sm.text-skin-item-base
Amount(
- :amount="$store.getters['wallets/walletsTotal'][walletId].base"
+ :amount="$store.getters['wallets/walletsTotal'][walletId]"
:currency="walletItem.currency"
:type="3"
:isShowBaseRate="false"
diff --git a/components/trns/getTotal.ts b/components/trns/getTotal.ts
index 15bd429b..782607ff 100644
--- a/components/trns/getTotal.ts
+++ b/components/trns/getTotal.ts
@@ -32,13 +32,13 @@ export function getTotal({
walletsIds,
walletsItems,
}: {
- baseRate?: string // TODO: add typings
- rates?: Record<string, number> // TODO: add typings
- transferCategoriesIds: CategoryID[]
trnsIds: TrnID[]
trnsItems: Record<TrnID, TrnItem>
- walletsIds?: WalletID[]
walletsItems: Record<WalletID, WalletItem>
+ transferCategoriesIds?: CategoryID[]
+ walletsIds?: WalletID[]
+ baseRate?: string // TODO: add typings
+ rates?: Record<string, number> // TODO: add typings
}) {
function getFormattedAmount(amount: number, currency: string) {
return getAmountInRate({
diff --git a/components/wallets/Item.vue b/components/wallets/Item.vue
index 69a26fb8..6d3f4b2d 100644
--- a/components/wallets/Item.vue
+++ b/components/wallets/Item.vue
@@ -13,7 +13,7 @@ export default defineComponent({
wallet() {
return {
...this.$store.state.wallets.items[this.id],
- total: this.$store.getters['wallets/walletsTotal'][this.id].base,
+ total: this.$store.getters['wallets/walletsTotal'][this.id],
}
},
},
diff --git a/components/stat/group/Sum2.vue b/components/wallets/ItemTotalSum.vue
similarity index 50%
rename from components/stat/group/Sum2.vue
rename to components/wallets/ItemTotalSum.vue
index aaf1c984..de2cac0a 100644
--- a/components/stat/group/Sum2.vue
+++ b/components/wallets/ItemTotalSum.vue
@@ -1,30 +1,48 @@
<script setup lang="ts">
+import { getTransferCategoriesIds } from '~/components/categories/getCategories'
+import { getTotal } from '~/components/trns/getTotal'
+import type { WalletID } from '~/components/wallets/types'
import type { TrnID } from '~/components/trns/types'
const props = withDefaults(defineProps<{
trnsIds: TrnID[]
+ walletId: WalletID
}>(), {
trnsIds: () => [],
})
const { $store } = useNuxtApp()
-const total = computed(() => $store.getters['trns/getTotalOfTrnsIds'](props.trnsIds))
+
+const wallet = computed(() => $store.state.wallets.items[props.walletId])
+
+const total = computed(() => {
+ const trnsItems = $store.state.trns.items
+ const walletsItems = $store.state.wallets.items
+ const categoriesItems = $store.state.categories.items
+ const transferCategoriesIds = getTransferCategoriesIds(categoriesItems)
+ return getTotal({
+ trnsIds: props.trnsIds,
+ trnsItems,
+ transferCategoriesIds,
+ walletsIds: [props.walletId],
+ walletsItems,
+ })
+})
</script>
<template lang="pug">
.py-3
.overflow-hidden.overflow-x-auto.scrollbar
- .flex.items-center.gap-6
+ .flex.gap-6
//- Expense
div
.pb-2.text-lg.leading-none.font-nunito.font-semibold.text-skin-item-base
| {{ $t('money.expense') }}
.text-xl
Amount(
- :amount="total.expense"
- :currency="$store.state.currencies.base"
+ :amount="total.expenseTransactions"
+ :currency="wallet.currency"
:type="0"
- :isShowBaseRate="false"
colorize="expense"
)
@@ -34,10 +52,9 @@ const total = computed(() => $store.getters['trns/getTotalOfTrnsIds'](props.trns
| {{ $t('money.income') }}
.text-xl
Amount(
- :amount="total.income"
- :currency="$store.state.currencies.base"
+ :amount="total.incomeTransactions"
+ :currency="wallet.currency"
:type="1"
- :isShowBaseRate="false"
colorize="income"
)
@@ -47,9 +64,8 @@ const total = computed(() => $store.getters['trns/getTotalOfTrnsIds'](props.trns
| {{ $t('money.sum') }}
.text-xl
Amount(
- :amount="total.sum"
- :currency="$store.state.currencies.base"
+ :amount="total.sumTransactions"
+ :currency="wallet.currency"
:type="3"
- :isShowBaseRate="false"
)
</template>
diff --git a/components/wallets/Total.vue b/components/wallets/Total.vue
index df7d3f7f..e3f0ee75 100644
--- a/components/wallets/Total.vue
+++ b/components/wallets/Total.vue
@@ -16,11 +16,11 @@ const totalInWallets = computed(() => {
for (const walletId in walletsItems) {
let walletTotal = 0
if (walletsItems[walletId].currency === $store.state.currencies.base) {
- walletTotal = walletsTotal[walletId].base
+ walletTotal = walletsTotal[walletId]
}
else {
walletTotal = getAmountInBaseRate({
- amount: walletsTotal[walletId].base,
+ amount: walletsTotal[walletId],
currency: walletsItems[walletId].currency,
noFormat: true,
})
@@ -33,6 +33,7 @@ const totalInWallets = computed(() => {
else
total.savings = total.savings + walletTotal
}
+
return total
})
diff --git a/components/wallets/page/id/Index.vue b/components/wallets/page/id/Index.vue
index 7e29ec99..bb3646f3 100644
--- a/components/wallets/page/id/Index.vue
+++ b/components/wallets/page/id/Index.vue
@@ -13,7 +13,7 @@ const wallet = computed(() => $store.state.wallets.items[walletId.value])
if (!wallet.value)
router.replace('/wallets')
-const total = computed(() => $store.getters['wallets/walletsTotal'][walletId.value].base)
+const total = computed(() => $store.getters['wallets/walletsTotal'][walletId.value])
const filter = reactive({ trnType: null })
const trnsItems = computed(() => $store.state.trns.items)
@@ -89,10 +89,15 @@ UiPage(v-if="wallet")
//- Stat
.overflow-hidden.relative.bg-gray-50.dark_bg-custom4.mx-3.mb-12.p-3.rounded-md
- SharedDate.text-xs.font-medium(class="-mb-1 dark_text-white/50")
+ SharedDate.text-xs.font-medium(
+ class="-mb-1 dark_text-white/50"
+ :date="$day().valueOf()"
+ :period="$store.state.filter.period"
+ )
div(class="-mb-3")
- StatGroupSum2(
+ WalletsItemTotalSum(
:trnsIds="periodTrnsIds"
+ :walletId="walletId"
)
//- History
diff --git a/components/wallets/useWallets.ts b/components/wallets/useWallets.ts
index 041824de..5d7b7506 100644
--- a/components/wallets/useWallets.ts
+++ b/components/wallets/useWallets.ts
@@ -24,7 +24,7 @@ export default function useWallets() {
...wallets,
[walletId]: {
...$store.state.wallets.items[walletId],
- amount: $store.getters['wallets/walletsTotal'][walletId].base,
+ amount: $store.getters['wallets/walletsTotal'][walletId],
},
}
}
diff --git a/store/wallets/getters.js b/store/wallets/getters.js
index 8f5df659..a040bc93 100644
--- a/store/wallets/getters.js
+++ b/store/wallets/getters.js
@@ -1,4 +1,5 @@
import { getTrnsIds } from '~/components/trns/getTrns'
+import { getTotal } from '~/components/trns/getTotal'
export default {
hasWallets(state, getters, rootState) {
@@ -9,27 +10,32 @@ export default {
return false
},
- // TODO: refactor
- walletsTotal(state, getters, rootState, rootGetters) {
+ /**
+ * Get total in every wallet
+ */
+ walletsTotal(_state, getters, rootState) {
if (!getters.hasWallets)
return {}
- const walletsTotal = {}
- const wallets = rootState.wallets.items
+
+ const walletsItems = rootState.wallets.items
const trnsItems = rootState.trns.items
const getWalletTotal = (walletId) => {
const trnsIds = getTrnsIds({ trnsItems, walletsIds: walletId })
- // @IncludeTransfers
- const { total } = rootGetters['trns/getTotalOfTrnsIds'](trnsIds, true, false, walletId)
- return total || 0
+ const { sumTransactions, sumTransfers } = getTotal({
+ trnsIds,
+ trnsItems,
+ walletsIds: [walletId],
+ walletsItems,
+ })
+
+ return sumTransactions + sumTransfers
}
- Object.keys(wallets).forEach((id) => {
- walletsTotal[id] = {
- base: getWalletTotal(id),
- currency: wallets[id].currency,
- }
- })
+ const walletsTotal = {}
+ Object.keys(walletsItems)
+ .forEach(walletId =>
+ walletsTotal[walletId] = getWalletTotal(walletId))
return walletsTotal
},