Skip to content

Open source personal finance application

feat(Amount): use getTotal

ilkome3e6d293a6ddc
diff --git a/components/amount/Amount.vue b/components/amount/Amount.vue
index 1768468a..86eed4a0 100644
--- a/components/amount/Amount.vue
+++ b/components/amount/Amount.vue
@@ -1,14 +1,7 @@
<script>
-import currency from 'currency.js'
import useAmount from '~/components/amount/useAmount'
-const baseAmountFormat = (value, separator) =>
- currency(value, { symbol: '', precision: 0, pattern: '#', separator })
- .format()
-
export default {
- name: 'Amount',
-
props: {
currency: { type: String, required: true },
isAltColor: { type: Boolean, default: false },
@@ -22,8 +15,11 @@ export default {
},
setup() {
- const { getAmountInBaseCurrency } = useAmount()
- return { getAmountInBaseCurrency }
+ const { getAmountInBaseCurrency, formatAmount } = useAmount()
+ return {
+ getAmountInBaseCurrency,
+ formatAmount,
+ }
},
computed: {
@@ -62,23 +58,19 @@ export default {
return currency
}
},
-
- formatAmount(amount) {
- return baseAmountFormat(amount, ' ')
- },
},
}
</script>
<template lang="pug">
.amount(:class="className")
- .amountItem._original.flex.items-baseline(:class="className")
+ .amountItem._original.flex.items-baseline.whitespace-nowrap(:class="className")
.amountItem__prefix(v-if="isShowPrefix && value !== 0") {{ type === 0 ? '-' : '+' }}
.amountItem__value.leading-none {{ formatAmount(value) }}
.amountItem__symbol.leading-none {{ getCurrencySymbol(currency) }}
template(v-if="showBase && (value !== 0) && (currency !== $store.state.currencies.base)")
- .amountItem._base.pt-1.flex.items-baseline(:class="className")
+ .amountItem._base.pt-1.flex.items-baseline.whitespace-nowrap(:class="className")
.amountItem__prefix(v-if="isShowPrefix && value !== 0") {{ type === 0 ? '-' : '+' }}
.amountItem__value.leading-none {{ amountInBaseCurrency }}
.amountItem__symbol.leading-none {{ getCurrencySymbol($store.state.currencies.base) }}
diff --git a/components/amount/Amount2.vue b/components/amount/Amount2.vue
index 324c8f38..ec851433 100644
--- a/components/amount/Amount2.vue
+++ b/components/amount/Amount2.vue
@@ -26,7 +26,7 @@ const classes = computed(() => ({
template(v-if="amount !== 0")
div(:class="[{ 'text-green-600 dark_text-green-500': colorize === 'incomes' && type === 1 }]")
//- Original
- .gap-1.flex.items-baseline(
+ .gap-1.flex.items-baseline.whitespace-nowrap(
:class="classes"
)
.text-md.leading-none {{ sign }}
@@ -34,7 +34,7 @@ const classes = computed(() => ({
.text-xs.leading-none {{ getCurrencySymbol(currency) }}
//- Base
- .text-neutral-400.gap-1.flex.items-baseline(
+ .text-neutral-400.gap-1.flex.items-baseline.whitespace-nowrap(
v-if="currency !== baseCurrency"
:class="classes"
)
diff --git a/components/amount/useAmount.ts b/components/amount/useAmount.ts
index ae944bf6..4ab442cc 100644
--- a/components/amount/useAmount.ts
+++ b/components/amount/useAmount.ts
@@ -1,4 +1,5 @@
import currency from 'currency.js'
+import { getAmountInBaseRate } from '~/components/trns/getTotal'
function getCurrencySymbol(currency) {
switch (currency) {
@@ -17,10 +18,10 @@ function getCurrencySymbol(currency) {
}
}
-const formatAmount = (value, separator = ' ') =>
+const formatAmount = (value, separator = ' ', precision = 2) =>
currency(value, {
symbol: '',
- precision: 0,
+ precision,
pattern: '#',
separator,
}).format()
@@ -29,11 +30,20 @@ export default function useAmount() {
const { $store } = useNuxtApp()
function getAmountInBaseCurrency({ amount, currency, noFormat }) {
- const fixed = $store.state.currencies.base === 'RUB' ? 0 : 2
- const baseValue = (amount / $store.state.currencies.rates[currency]).toFixed(fixed)
- if (!baseValue) return
- if (noFormat) return Number(baseValue)
- return formatAmount(Number(baseValue))
+ const rates = $store.state.currencies.rates
+ const baseRate = $store.state.currencies.base
+
+ const amountInBaseRate = getAmountInBaseRate({
+ amount,
+ currency,
+ rates,
+ baseRate,
+ })
+
+ if (noFormat)
+ return amountInBaseRate
+
+ return formatAmount(amountInBaseRate)
}
const baseCurrency = computed(() => $store.state.currencies.base)