Skip to content

Open source personal finance application

update currencies

Ilya Komichev53ae51e6afa9
diff --git a/src/components/amount/Amount.vue b/src/components/amount/Amount.vue
index 3b2203d5..076ea017 100644
--- a/src/components/amount/Amount.vue
+++ b/src/components/amount/Amount.vue
@@ -47,7 +47,7 @@ export default {
},
amountValueBaseCurrency () {
- const currencies = this.$store.state.currencies.items
+ const currencies = this.$store.state.currencies.rates
const baseValue = this.value / currencies[this.currency]
return this.formatAmount(baseValue)
},
diff --git a/src/components/currencies/CurrencyModal.vue b/src/components/currencies/CurrencyModal.vue
index a822abc3..66eb8a09 100644
--- a/src/components/currencies/CurrencyModal.vue
+++ b/src/components/currencies/CurrencyModal.vue
@@ -24,7 +24,7 @@ export default {
.inputText
.currencies
.currencies__item(
- v-for="(item, currency) in $store.state.currencies.items"
+ v-for="(item, currency) in $store.state.currencies.rates"
:class="{ _active: $store.state.currencies.base === currency }"
@click="handleCurrencySelect(currency)"
) {{ currency }}
diff --git a/src/components/currencies/store/actions.js b/src/components/currencies/store/actions.js
index e26fa9e7..480ed434 100644
--- a/src/components/currencies/store/actions.js
+++ b/src/components/currencies/store/actions.js
@@ -1,46 +1,36 @@
import localforage from 'localforage'
import moment from 'moment'
import { db } from '@/firebase'
-import { getCurrencies } from './api'
+import { getRatesOf } from './api'
export default {
async initCurrencies ({ rootState, commit }) {
const uid = rootState.user.user.uid
-
- // base currency
- const userBaseCurrency = await db.ref(`users/${uid}/settings/baseCurrency`).once('value')
- const baseCurrency = userBaseCurrency.val() || 'RUB'
- const currenciesRef = `currencies/${baseCurrency}`
-
- // currencies
- const currenciesSnapshot = await db.ref(currenciesRef).once('value')
- let currenciesValue = currenciesSnapshot.val()
- let isTodayCurrencies = true
-
- // have currencies for today?
- if (currenciesValue) isTodayCurrencies = moment().isSame(currenciesValue.date, 'day')
-
- // get currencies from rest api
- if (!currenciesValue || !isTodayCurrencies) {
- const currenciesResponse = await getCurrencies(baseCurrency)
- if (currenciesResponse.data) {
- currenciesValue = {
- ...currenciesValue,
- date: moment().valueOf(),
- items: currenciesResponse.data.rates
- }
- db.ref(currenciesRef).set(currenciesValue)
- }
- }
-
- currenciesValue = {
- base: baseCurrency,
- date: currenciesValue.date,
- items: currenciesValue.items
+ const today = moment().valueOf()
+
+ // user base currency in DB
+ const userCurrencyReq = await db.ref(`users/${uid}/settings/baseCurrency`).once('value')
+ const userCurrency = userCurrencyReq.val() || 'RUB'
+
+ // rates of base currency
+ const currencyRateReq = await db.ref(`currencies/${userCurrency}`).once('value')
+ const currencyRate = currencyRateReq.val() || {}
+
+ let rates = currencyRate.rates
+ const isCurrencyUpdatedToday = moment(today).isSame(currencyRate.date, 'day')
+
+ // get new rates
+ if (!currencyRate.rates || !isCurrencyUpdatedToday) {
+ rates = await getRatesOf(userCurrency)
+ db.ref(`currencies/${userCurrency}`).set({
+ rates,
+ date: today
+ })
}
- commit('setCurrencies', currenciesValue)
- localforage.setItem('next.currencies', currenciesValue)
+ const currencies = { base: userCurrency, rates }
+ commit('setCurrencies', currencies)
+ localforage.setItem('next.currencies', currencies)
},
setBaseCurrency ({ rootState, dispatch }, baseCurrency) {
diff --git a/src/components/currencies/store/api.js b/src/components/currencies/store/api.js
index 3e2494f7..5c7b0f28 100644
--- a/src/components/currencies/store/api.js
+++ b/src/components/currencies/store/api.js
@@ -1,11 +1,18 @@
import axios from 'axios'
-export const getCurrencies = async (baseCurrency) => {
- console.log('api: getCurrencies', baseCurrency)
+const serviceUrl = 'https://api.openrates.io'
+
+export const getRatesOf = async (baseCurrency) => {
if (!baseCurrency) {
console.error('No base currency')
return
}
- const currencies = await axios.get(`https://api.openrates.io/latest?base=${baseCurrency}`)
- return currencies
+
+ const currencies = await axios.get(`${serviceUrl}/latest?base=${baseCurrency}`)
+ if (currencies && currencies.data) {
+ return currencies.data.rates
+ }
+ else {
+ console.log('api unavaliable')
+ }
}
diff --git a/src/components/currencies/store/getters.js b/src/components/currencies/store/getters.js
index 35e31923..09ad4b1d 100644
--- a/src/components/currencies/store/getters.js
+++ b/src/components/currencies/store/getters.js
@@ -1,7 +1,7 @@
export default {
getAmountInBaseCurrency: (state, getters, rootState) => ({ amount, currency }) => {
const fixed = rootState.currencies.base === 'RUB' ? 0 : 2
- const baseValue = (amount / rootState.currencies.items[currency]).toFixed(fixed)
+ const baseValue = (amount / rootState.currencies.rates[currency]).toFixed(fixed)
return Number(baseValue)
}
}
diff --git a/src/components/currencies/store/index.js b/src/components/currencies/store/index.js
index 6edc59f8..5ad006f8 100644
--- a/src/components/currencies/store/index.js
+++ b/src/components/currencies/store/index.js
@@ -7,7 +7,7 @@ import mutations from './mutations'
const state = {
base: 'RUB',
date: moment().valueOf(),
- items: {},
+ rates: {},
modal: {
show: false
}
diff --git a/src/components/currencies/store/mutations.js b/src/components/currencies/store/mutations.js
index a9444a07..43c39960 100644
--- a/src/components/currencies/store/mutations.js
+++ b/src/components/currencies/store/mutations.js
@@ -1,15 +1,15 @@
import isEqual from 'lodash/isEqual'
export default {
- setCurrencies (state, { base, date, items }) {
+ setCurrencies (state, { base, date, rates }) {
if (!isEqual(state.base, base)) {
state.base = base
}
if (!isEqual(state.date, date)) {
state.date = date
}
- if (!isEqual(state.items, items)) {
- state.items = items
+ if (!isEqual(state.rates, rates)) {
+ state.rates = rates
}
},
diff --git a/src/components/trns/store/getters.js b/src/components/trns/store/getters.js
index bf3f2558..c4fa4f10 100644
--- a/src/components/trns/store/getters.js
+++ b/src/components/trns/store/getters.js
@@ -21,7 +21,7 @@ export default {
*/
getTotalOfTrnsIds: (state, getters, rootState, rootGetters) => (trnsIds, inculdeTrnasfers = false) => {
const trns = rootState.trns.items
- const currencies = rootState.currencies.items
+ const currencies = rootState.currencies.rates
const wallets = rootState.wallets.items
const baseCurrency = rootState.currencies.base
const transferCategoryId = rootGetters.transferCategoryId
diff --git a/src/components/wallets/form/WalletForm.vue b/src/components/wallets/form/WalletForm.vue
index 48e330be..fa36b1f0 100644
--- a/src/components/wallets/form/WalletForm.vue
+++ b/src/components/wallets/form/WalletForm.vue
@@ -211,7 +211,7 @@ ComponentWrap
.currencies__item(
:class="{ _active: wallet.currency === currency }"
@click="handleCurrencySelect(currency)"
- v-for="(item, currency) in $store.state.currencies.items"
+ v-for="(item, currency) in $store.state.currencies.rates"
) {{ currency }}
template(slot="bottom")