Refactors AVAILABLE_CURRENCIES as enum

Add types for currency values
This commit is contained in:
Agustin Pane 2020-06-04 10:09:33 -03:00
parent 51c0a0fe1e
commit 6858a8d6d6
8 changed files with 68 additions and 56 deletions

View File

@ -1,9 +1,7 @@
import axios from 'axios'
import { getExchangeRatesUrl } from 'src/config'
import { AVAILABLE_CURRENCIES } from '../store/model/currencyValues'
type Currency = keyof typeof AVAILABLE_CURRENCIES
import { Currency } from '../store/model/currencyValues'
const fetchCurrenciesRates = async (baseCurrency: Currency, targetCurrencyValue: Currency): Promise<number> => {
let rate = 0

View File

@ -1,17 +1,18 @@
import fetchCurrenciesRates from 'src/logic/currencyValues/api/fetchCurrenciesRates'
import { setCurrencyRate } from 'src/logic/currencyValues/store/actions/setCurrencyRate'
import { AVAILABLE_CURRENCIES } from 'src/logic/currencyValues/store/model/currencyValues'
import { AVAILABLE_CURRENCIES, Currency } from 'src/logic/currencyValues/store/model/currencyValues'
// eslint-disable-next-line max-len
const fetchCurrencyRate = (safeAddress, selectedCurrency) => async (dispatch) => {
const fetchCurrencyRate = (safeAddress: string, selectedCurrency: Currency) => async (dispatch) => {
if (AVAILABLE_CURRENCIES.USD === selectedCurrency) {
return dispatch(setCurrencyRate(safeAddress, 1))
}
const selectedCurrencyRateInBaseCurrency = await fetchCurrenciesRates(
AVAILABLE_CURRENCIES.USD as any,
selectedCurrency as any,
const selectedCurrencyRateInBaseCurrency: number = await fetchCurrenciesRates(
AVAILABLE_CURRENCIES.USD,
selectedCurrency,
)
dispatch(setCurrencyRate(safeAddress, selectedCurrencyRateInBaseCurrency))
}

View File

@ -4,12 +4,12 @@ import { batch } from 'react-redux'
import { setCurrencyBalances } from 'src/logic/currencyValues/store/actions/setCurrencyBalances'
import { setCurrencyRate } from 'src/logic/currencyValues/store/actions/setCurrencyRate'
import { setSelectedCurrency } from 'src/logic/currencyValues/store/actions/setSelectedCurrency'
import { AVAILABLE_CURRENCIES } from 'src/logic/currencyValues/store/model/currencyValues'
import { AVAILABLE_CURRENCIES, CurrencyRateValue } from 'src/logic/currencyValues/store/model/currencyValues'
import { loadCurrencyValues } from 'src/logic/currencyValues/store/utils/currencyValuesStorage'
export const fetchCurrencyValues = (safeAddress) => async (dispatch) => {
export const fetchCurrencyValues = (safeAddress: string) => async (dispatch) => {
try {
const storedCurrencies = await loadCurrencyValues()
const storedCurrencies: Map<string, CurrencyRateValue> | {} = await loadCurrencyValues()
const storedCurrency = storedCurrencies[safeAddress]
if (!storedCurrency) {
return batch(() => {
@ -23,7 +23,7 @@ export const fetchCurrencyValues = (safeAddress) => async (dispatch) => {
const safeAddr = kv[0]
const value = kv[1]
const { currencyRate, selectedCurrency }: any = value
const { currencyRate, selectedCurrency }: CurrencyRateValue = value
batch(() => {
dispatch(setSelectedCurrency(safeAddr, selectedCurrency || AVAILABLE_CURRENCIES.USD))
dispatch(setCurrencyRate(safeAddr, currencyRate))

View File

@ -3,7 +3,7 @@ import { createAction } from 'redux-actions'
export const SET_CURRENCY_RATE = 'SET_CURRENCY_RATE'
// eslint-disable-next-line max-len
export const setCurrencyRate = createAction(SET_CURRENCY_RATE, (safeAddress, currencyRate) => ({
export const setCurrencyRate = createAction(SET_CURRENCY_RATE, (safeAddress: string, currencyRate: number) => ({
safeAddress,
currencyRate,
}))

View File

@ -1,9 +1,13 @@
import { createAction } from 'redux-actions'
import { Currency } from '../model/currencyValues'
export const SET_CURRENT_CURRENCY = 'SET_CURRENT_CURRENCY'
// eslint-disable-next-line max-len
export const setSelectedCurrency = createAction(SET_CURRENT_CURRENCY, (safeAddress, selectedCurrency) => ({
export const setSelectedCurrency = createAction(
SET_CURRENT_CURRENCY,
(safeAddress: string, selectedCurrency: Currency) => ({
safeAddress,
selectedCurrency,
}))
}),
)

View File

@ -4,6 +4,7 @@ import { SET_CURRENCY_RATE } from 'src/logic/currencyValues/store/actions/setCur
import { SET_CURRENT_CURRENCY } from 'src/logic/currencyValues/store/actions/setSelectedCurrency'
import { currencyValuesSelector } from 'src/logic/currencyValues/store/selectors'
import { saveCurrencyValues } from 'src/logic/currencyValues/store/utils/currencyValuesStorage'
import { Currency, CurrencyRateValue } from '../model/currencyValues'
const watchedActions = [SET_CURRENT_CURRENCY, SET_CURRENCY_RATE, SET_CURRENCY_BALANCES]
@ -21,9 +22,9 @@ const currencyValuesStorageMiddleware = (store) => (next) => async (action) => {
case SET_CURRENCY_RATE:
case SET_CURRENCY_BALANCES: {
const currencyValues = currencyValuesSelector(state)
const currencyValuesWithoutBalances = currencyValues.map((currencyValue) => {
const currencyRate = currencyValue.get('currencyRate')
const selectedCurrency = currencyValue.get('selectedCurrency')
const currencyValuesWithoutBalances: Map<string, CurrencyRateValue> = currencyValues.map((currencyValue) => {
const currencyRate: number = currencyValue.get('currencyRate')
const selectedCurrency: Currency = currencyValue.get('selectedCurrency')
return {
currencyRate,
selectedCurrency,

View File

@ -1,39 +1,46 @@
import { Record } from 'immutable'
export const AVAILABLE_CURRENCIES = {
USD: 'USD',
EUR: 'EUR',
CAD: 'CAD',
HKD: 'HKD',
ISK: 'ISK',
PHP: 'PHP',
DKK: 'DKK',
HUF: 'HUF',
CZK: 'CZK',
AUD: 'AUD',
RON: 'RON',
SEK: 'SEK',
IDR: 'IDR',
INR: 'INR',
BRL: 'BRL',
RUB: 'RUB',
HRK: 'HRK',
JPY: 'JPY',
THB: 'THB',
CHF: 'CHF',
SGD: 'SGD',
PLN: 'PLN',
BGN: 'BGN',
TRY: 'TRY',
CNY: 'CNY',
NOK: 'NOK',
NZD: 'NZD',
ZAR: 'ZAR',
MXN: 'MXN',
ILS: 'ILS',
GBP: 'GBP',
KRW: 'KRW',
MYR: 'MYR',
export enum AVAILABLE_CURRENCIES {
USD = 'USD',
EUR = 'EUR',
CAD = 'CAD',
HKD = 'HKD',
ISK = 'ISK',
PHP = 'PHP',
DKK = 'DKK',
HUF = 'HUF',
CZK = 'CZK',
AUD = 'AUD',
RON = 'RON',
SEK = 'SEK',
IDR = 'IDR',
INR = 'INR',
BRL = 'BRL',
RUB = 'RUB',
HRK = 'HRK',
JPY = 'JPY',
THB = 'THB',
CHF = 'CHF',
SGD = 'SGD',
PLN = 'PLN',
BGN = 'BGN',
TRY = 'TRY',
CNY = 'CNY',
NOK = 'NOK',
NZD = 'NZD',
ZAR = 'ZAR',
MXN = 'MXN',
ILS = 'ILS',
GBP = 'GBP',
KRW = 'KRW',
MYR = 'MYR',
}
export type Currency = keyof typeof AVAILABLE_CURRENCIES
export type CurrencyRateValue = {
currencyRate: number
selectedCurrency: Currency
}
export const makeBalanceCurrency = Record({

View File

@ -1,7 +1,8 @@
import { loadFromStorage, saveToStorage } from 'src/utils/storage'
import { CurrencyRateValue } from '../model/currencyValues'
const CURRENCY_VALUES_STORAGE_KEY = 'CURRENCY_VALUES_STORAGE_KEY'
export const saveCurrencyValues = async (currencyValues) => {
export const saveCurrencyValues = async (currencyValues: Map<string, CurrencyRateValue>) => {
try {
await saveToStorage(CURRENCY_VALUES_STORAGE_KEY, currencyValues)
} catch (err) {
@ -9,6 +10,6 @@ export const saveCurrencyValues = async (currencyValues) => {
}
}
export const loadCurrencyValues = async () => {
export const loadCurrencyValues = async (): Promise<Map<string, CurrencyRateValue> | {}> => {
return (await loadFromStorage(CURRENCY_VALUES_STORAGE_KEY)) || {}
}