Skip to content

Open source personal finance application

Add ability to set base currency

Ilya Komichev89f277fe7592
diff --git a/src/components/currencies/CurrencyModal.vue b/src/components/currencies/CurrencyModal.vue
new file mode 100644
index 00000000..a822abc3
--- /dev/null
+++ b/src/components/currencies/CurrencyModal.vue
@@ -0,0 +1,35 @@
+<script>
+import ModalBottom from '@/components/modal/ModalBottom'
+
+export default {
+ components: {
+ ModalBottom
+ },
+
+ methods: {
+ handleCurrencySelect (currency) {
+ this.$store.commit('hideBaseCurrenciesModal')
+ this.$store.dispatch('setBaseCurrency', currency)
+ }
+ }
+}
+</script>
+
+<template lang="pug">
+ ModalBottom(
+ :center="true"
+ :show="$store.state.currencies.modal.show"
+ title="Select base currency"
+ v-on:onClose="$store.commit('hideBaseCurrenciesModal')")
+ .inputText
+ .currencies
+ .currencies__item(
+ v-for="(item, currency) in $store.state.currencies.items"
+ :class="{ _active: $store.state.currencies.base === currency }"
+ @click="handleCurrencySelect(currency)"
+ ) {{ currency }}
+</template>
+
+<style lang="stylus" scoped>
+
+</style>
diff --git a/src/components/currencies/currencies.styl b/src/components/currencies/currencies.styl
new file mode 100644
index 00000000..dad7a322
--- /dev/null
+++ b/src/components/currencies/currencies.styl
@@ -0,0 +1,18 @@
+.currencies
+ display flex
+ flex-wrap wrap
+
+ &__item
+ flex 1
+ text-align center
+ margin 0 -1px -1px 0
+ padding $m7 $m7
+ border 1px solid var(--c-bg-6)
+
+ &:hover
+ @media $media-laptop
+ background var(--c-bg-6)
+
+ &._active
+ color var(--c-font-1)
+ background var(--c-bg-8)
diff --git a/src/components/currencies/store/actions.js b/src/components/currencies/store/actions.js
index 76b30257..e26fa9e7 100644
--- a/src/components/currencies/store/actions.js
+++ b/src/components/currencies/store/actions.js
@@ -4,7 +4,7 @@ import { db } from '@/firebase'
import { getCurrencies } from './api'
export default {
- async initCurrencies ({ commit, rootState }) {
+ async initCurrencies ({ rootState, commit }) {
const uid = rootState.user.user.uid
// base currency
@@ -17,7 +17,7 @@ export default {
let currenciesValue = currenciesSnapshot.val()
let isTodayCurrencies = true
- // check date of currencies
+ // have currencies for today?
if (currenciesValue) isTodayCurrencies = moment().isSame(currenciesValue.date, 'day')
// get currencies from rest api
@@ -41,5 +41,11 @@ export default {
commit('setCurrencies', currenciesValue)
localforage.setItem('next.currencies', currenciesValue)
+ },
+
+ setBaseCurrency ({ rootState, dispatch }, baseCurrency) {
+ const uid = rootState.user.user.uid
+ db.ref(`users/${uid}/settings/baseCurrency`).set(baseCurrency)
+ dispatch('initCurrencies')
}
}
diff --git a/src/components/currencies/store/index.js b/src/components/currencies/store/index.js
index eb0f99bb..6edc59f8 100644
--- a/src/components/currencies/store/index.js
+++ b/src/components/currencies/store/index.js
@@ -7,7 +7,10 @@ import mutations from './mutations'
const state = {
base: 'RUB',
date: moment().valueOf(),
- items: {}
+ items: {},
+ modal: {
+ show: false
+ }
}
export default {
diff --git a/src/components/currencies/store/mutations.js b/src/components/currencies/store/mutations.js
index 46837d23..a9444a07 100644
--- a/src/components/currencies/store/mutations.js
+++ b/src/components/currencies/store/mutations.js
@@ -11,5 +11,13 @@ export default {
if (!isEqual(state.items, items)) {
state.items = items
}
+ },
+
+ showBaseCurrenciesModal (state) {
+ state.modal.show = true
+ },
+
+ hideBaseCurrenciesModal (state) {
+ state.modal.show = false
}
}
diff --git a/src/components/dashboard/DashboardNav.vue b/src/components/dashboard/DashboardNav.vue
index 47f61170..286c3594 100644
--- a/src/components/dashboard/DashboardNav.vue
+++ b/src/components/dashboard/DashboardNav.vue
@@ -56,6 +56,11 @@ export default {
v-on:onClick="$store.dispatch('toogleVisibilityStatItems')"
v-on:onClose="visibleContextMenu = !visibleContextMenu")
.context-menu-sep
+ ContextMenuItem(
+ icon="mdi mdi-currency-usd"
+ title="Change base currency"
+ v-on:onClick="$store.commit('showBaseCurrenciesModal')"
+ v-on:onClose="visibleContextMenu = !visibleContextMenu")
ContextMenuItem(
icon="mdi mdi-palette"
title="Change theme"
diff --git a/src/components/layout/LayoutMobile.vue b/src/components/layout/LayoutMobile.vue
index 58fd2fc6..15bb1edd 100644
--- a/src/components/layout/LayoutMobile.vue
+++ b/src/components/layout/LayoutMobile.vue
@@ -2,6 +2,7 @@
import CategoriesList from '@/components/categories/list/CategoriesList'
import CategoryForm from '@/components/categories/form/CategoryForm'
import CategoryModal from '@/components/categories/modal/CategoryModal'
+import CurrencyModal from '@/components/currencies/CurrencyModal'
import FilterModal from '@/components/filter/FilterModal'
import LayoutMobileMenu from '@/components/layout/LayoutMobileMenu'
import Settings from '@/components/settings/Settings'
@@ -21,6 +22,7 @@ export default {
CategoriesList,
CategoryForm,
CategoryModal,
+ CurrencyModal,
FilterModal,
LayoutMobileMenu,
Settings,
@@ -115,6 +117,7 @@ export default {
//- modals
CategoryModal
+ CurrencyModal
FilterModal
TrnForm
TrnModal
diff --git a/src/components/layout/LayoutMobileMenu.vue b/src/components/layout/LayoutMobileMenu.vue
index fa1bdc0c..cf5cfa19 100644
--- a/src/components/layout/LayoutMobileMenu.vue
+++ b/src/components/layout/LayoutMobileMenu.vue
@@ -111,6 +111,11 @@ export default {
v-on:onClick="$store.dispatch('signOut')"
v-on:onClose="visibleCustomizeMenu = !visibleCustomizeMenu")
.context-menu-sep
+ ContextMenuItem(
+ icon="mdi mdi-currency-usd"
+ title="Change base currency"
+ v-on:onClick="$store.commit('showBaseCurrenciesModal')"
+ v-on:onClose="visibleCustomizeMenu = !visibleCustomizeMenu")
ContextMenuItem(
icon="mdi mdi-palette"
title="Change theme"
diff --git a/src/components/layout/LayoutPc.vue b/src/components/layout/LayoutPc.vue
index c90e4ac8..e6867a37 100644
--- a/src/components/layout/LayoutPc.vue
+++ b/src/components/layout/LayoutPc.vue
@@ -3,10 +3,11 @@ import Button from '@/components/shared/button/Button'
import CategoriesList from '@/components/categories/list/CategoriesList'
import CategoryForm from '@/components/categories/form/CategoryForm'
import CategoryModal from '@/components/categories/modal/CategoryModal'
+import CurrencyModal from '@/components/currencies/CurrencyModal'
import Dashboard from '@/components/dashboard/Dashboard'
import DashboardNav from '@/components/dashboard/DashboardNav'
-import LayoutPcTab from '@/components/layout/LayoutPcTab'
import LayoutPcSidebar from '@/components/layout/LayoutPcSidebar'
+import LayoutPcTab from '@/components/layout/LayoutPcTab'
import Settings from '@/components/settings/Settings'
import TrnForm from '@/components/trnForm/TrnForm'
import TrnModal from '@/components/trns/modal/TrnModal'
@@ -20,6 +21,7 @@ export default {
CategoriesList,
CategoryForm,
CategoryModal,
+ CurrencyModal,
Dashboard,
DashboardNav,
LayoutPcTab,
@@ -58,6 +60,7 @@ export default {
//- modals
CategoryModal
+ CurrencyModal
TrnForm
TrnModal
WalletModal
diff --git a/src/components/wallets/store/actions.js b/src/components/wallets/store/actions.js
index 33a36263..103bf7d2 100644
--- a/src/components/wallets/store/actions.js
+++ b/src/components/wallets/store/actions.js
@@ -15,8 +15,7 @@ export default {
// set default currency based on first created wallet
if (!getters.hasWallets) {
- db.ref(`users/${uid}/settings/baseCurrency`).set(values.currency)
- dispatch('initCurrencies')
+ dispatch('setBaseCurrency', values.currency)
}
db.ref(`users/${uid}/accounts/${id}`).set(formatedValues)