Skip to content

Open source personal finance application

feat(Currencies): update configs and logic

ilkome50978ff6cc30
diff --git a/components/amount/Amount.vue b/components/amount/Amount.vue
index 102298ba..c58cf9de 100644
--- a/components/amount/Amount.vue
+++ b/components/amount/Amount.vue
@@ -42,7 +42,7 @@ const amountClasses = computed(() => ([{
:class="alignClasses"
)
.text-md.leading-none(v-if="isShowSign && sign === '-'") {{ sign }}
- .text-md.leading-none {{ formatAmount(amount) }}
+ .text-md.leading-none {{ formatAmount(amount, currency) }}
.text-xs.leading-none {{ getCurrencySymbol(currency) }}
//- Base
diff --git a/components/amount/useAmount.ts b/components/amount/useAmount.ts
index cfab2a81..48171814 100644
--- a/components/amount/useAmount.ts
+++ b/components/amount/useAmount.ts
@@ -1,30 +1,30 @@
-import currency from 'currency.js'
+import currencyjs from 'currency.js'
+import { currencies } from '~/components/currencies/currencies'
+import type { CurrencyCode } from '~/components/currencies/types'
import { getAmountInBaseRate } from '~/components/trns/getTotal'
-function getCurrencySymbol(currency) {
- switch (currency) {
- case 'USD':
- return '$'
- case 'RUB':
- return '₽'
- case 'EUR':
- return '€'
- case 'IDR':
- return 'Rp'
- case 'THB':
- return '฿'
- default:
- return currency
- }
+function getCurrencySymbol(currencyCode?: CurrencyCode) {
+ const currencySettings = currencies.find(c => c.code === currencyCode)
+
+ if (currencySettings?.symbol)
+ return currencySettings.symbol
+
+ return currencyCode
}
-const formatAmount = (value, separator = ' ', precision = 2) =>
- currency(value, {
+function formatAmount(amount: number, currencyCode?: CurrencyCode) {
+ let currencySettings = null
+
+ if (currencyCode && currencies.find(c => c.code === currencyCode))
+ currencySettings = currencies.find(c => c.code === currencyCode)
+
+ return currencyjs(amount, {
symbol: '',
- precision,
- pattern: '#',
- separator,
+ precision: currencySettings?.precision ?? 2,
+ pattern: currencySettings?.pattern ?? '#',
+ separator: currencySettings?.separator ?? ' ',
}).format()
+}
export default function useAmount() {
const { $store } = useNuxtApp()
@@ -47,7 +47,7 @@ export default function useAmount() {
if (noFormat)
return +amountInBaseRate
- return formatAmount(amountInBaseRate)
+ return formatAmount(amountInBaseRate, baseRate)
}
const baseCurrency = computed(() => $store.state.currencies.base)
diff --git a/components/currencies/currencies.ts b/components/currencies/currencies.ts
index d09446dc..5940fe2b 100644
--- a/components/currencies/currencies.ts
+++ b/components/currencies/currencies.ts
@@ -1,8 +1,10 @@
-import type { Currency } from '~/components/currencies/types'
+import type { CurrencyCode } from '~/components/currencies/types'
export const currencies: {
- code: Currency
+ code: CurrencyCode
name: string
+ symbol?: string
+ precision?: number
}[] = [{
code: 'CNH',
name: 'Chinese Yuan',
@@ -156,6 +158,7 @@ export const currencies: {
}, {
code: 'EUR',
name: 'Euro',
+ symbol: '€',
}, {
code: 'FJD',
name: 'Fiji Dollar',
@@ -204,6 +207,8 @@ export const currencies: {
}, {
code: 'IDR',
name: 'Rupiah',
+ symbol: 'Rp',
+ precision: 0,
}, {
code: 'ILS',
name: 'New Israeli Sheqel',
@@ -372,6 +377,8 @@ export const currencies: {
}, {
code: 'RUB',
name: 'Russian Ruble',
+ symbol: '₽',
+ precision: 0,
}, {
code: 'RWF',
name: 'Rwanda Franc',
@@ -423,6 +430,8 @@ export const currencies: {
}, {
code: 'THB',
name: 'Baht',
+ symbol: '฿',
+ precision: 0,
}, {
code: 'TJS',
name: 'Somoni',
@@ -456,6 +465,7 @@ export const currencies: {
}, {
code: 'USD',
name: 'US Dollar',
+ symbol: '$',
}, {
code: 'USN',
name: 'US Dollar (Next day)',
diff --git a/components/currencies/types.ts b/components/currencies/types.ts
index 622314e3..b34b8815 100644
--- a/components/currencies/types.ts
+++ b/components/currencies/types.ts
@@ -1 +1 @@
-export type Currency = string
+export type CurrencyCode = string
diff --git a/components/trns/getTotal.ts b/components/trns/getTotal.ts
index 44a8da9d..03fbbb8b 100644
--- a/components/trns/getTotal.ts
+++ b/components/trns/getTotal.ts
@@ -3,6 +3,7 @@ import type { TrnID, TrnItem } from '~/components/trns/types'
import type { WalletID, WalletItem } from '~/components/wallets/types'
import { TrnType } from '~/components/trns/types'
+// TODO: why is getAmountInBaseRate and getAmountInBaseCurrency
export function getAmountInBaseRate({
amount,
currency,
@@ -40,6 +41,7 @@ export function getTotal({
walletsIds?: WalletID[]
walletsItems: Record<WalletID, WalletItem>
}) {
+ // TODO: rename getFormatedAmount
function getFormatedAmount(amount: number, currency: string) {
return getAmountInBaseRate({
amount,
diff --git a/store/currencies/actions.ts b/store/currencies/actions.ts
index a45b1f54..65808cb8 100644
--- a/store/currencies/actions.ts
+++ b/store/currencies/actions.ts
@@ -10,18 +10,17 @@ export default {
// User base currency in DB
const userBaseCurrency = await getDataOnce(`users/${uid}/settings/baseCurrency`) || 'USD'
- // Rates with date
+ // Rates for today
const today = dayjs().startOf('day').valueOf()
let ratesBasedOnUsd = await getDataOnce(`ratesUsd/history/${today}`)
- // No rates for today
if (!ratesBasedOnUsd) {
ratesBasedOnUsd = await getRatesOfUSD(this.$config.ratesApiKey)
if (!ratesBasedOnUsd)
return
- saveData('ratesUsd/latest', ratesBasedOnUsd)
- saveData(`ratesUsd/history/${today}`, ratesBasedOnUsd)
+ await saveData('ratesUsd/latest', ratesBasedOnUsd)
+ await saveData(`ratesUsd/history/${today}`, ratesBasedOnUsd)
}
commit('setCurrencies', {
@@ -29,7 +28,7 @@ export default {
rates: ratesBasedOnUsd,
})
- localforage.setItem('finapp.currencies', {
+ await localforage.setItem('finapp.currencies', {
base: userBaseCurrency,
rates: ratesBasedOnUsd,
})