currency.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const state = {
  2. currencies: [],
  3. loadingCurrencies: true,
  4. selectedCurrency: null
  5. }
  6. const getters = {
  7. currencies(state) {
  8. return state.currencies
  9. },
  10. loadingCurrencies(state) {
  11. return state.loadingCurrencies
  12. },
  13. currencySymbols(state) {
  14. if (!state.currencies || !state.selectedCurrency) {
  15. return []
  16. }
  17. return state.currencies.filter(c => state.selectedCurrency.id !== c.id).map(c => c.symbol)
  18. },
  19. baseCurrency(state) {
  20. return state.currencies.find(c => c.base === true)
  21. },
  22. selectedCurrency(state) {
  23. return state.selectedCurrency
  24. },
  25. selectedCurrencySymbol(state) {
  26. return state.selectedCurrency && state.selectedCurrency.symbol
  27. }
  28. }
  29. const mutations = {
  30. setCurrencies(state, payload) {
  31. state.currencies = [...payload]
  32. },
  33. setLoadingCurrencies(state, payload) {
  34. state.loadingCurrencies = !!payload
  35. },
  36. autoSelectCurrency(state) {
  37. state.selectedCurrency = state.currencies.find(item => item.base == true)
  38. }
  39. }
  40. const actions = {
  41. initCurrencies({ commit }, payload) {
  42. commit('setCurrencies', payload)
  43. commit('autoSelectCurrency')
  44. commit('setLoadingCurrencies', false)
  45. },
  46. resetCurrency({ commit }, payload) {
  47. commit('setLoadingCurrencies', true)
  48. commit('setCurrencies', [])
  49. }
  50. }
  51. export default {
  52. state,
  53. getters,
  54. mutations,
  55. actions
  56. }