From 0c0e7eb0baca33d66e11a53c23b68b3408b963d0 Mon Sep 17 00:00:00 2001 From: Vladimir Volek Date: Wed, 7 Nov 2018 17:14:06 +0100 Subject: [PATCH] Propagate rate to ui --- src/reducers/FiatRateReducer.js | 3 +- src/services/CoinmarketcapService.js | 62 ------------------- src/services/TickerService.js | 2 +- .../components/AccountMenu/index.js | 2 +- .../Summary/components/Balance/index.js | 5 +- 5 files changed, 5 insertions(+), 69 deletions(-) delete mode 100644 src/services/CoinmarketcapService.js diff --git a/src/reducers/FiatRateReducer.js b/src/reducers/FiatRateReducer.js index 2f63a69d..ccc3e793 100644 --- a/src/reducers/FiatRateReducer.js +++ b/src/reducers/FiatRateReducer.js @@ -8,7 +8,7 @@ import type { FiatRateAction } from 'services/TickerService'; export type Fiat = { +network: string; value: string; -} +}; export const initialState: Array = []; @@ -18,7 +18,6 @@ const update = (state: Array, action: FiatRateAction): Array => { network: action.network, value: action.rate.current_price.usd, }); - console.log('newState', newState); return newState; }; diff --git a/src/services/CoinmarketcapService.js b/src/services/CoinmarketcapService.js deleted file mode 100644 index a35b50b7..00000000 --- a/src/services/CoinmarketcapService.js +++ /dev/null @@ -1,62 +0,0 @@ -/* @flow */ - -import { httpRequest } from 'utils/networkUtils'; -import { resolveAfter } from 'utils/promiseUtils'; -import { READY } from 'actions/constants/localStorage'; - -import type { - Middleware, - MiddlewareAPI, - MiddlewareDispatch, - Dispatch, - Action, - AsyncAction, - GetState, -} from 'flowtype'; - -export const RATE_UPDATE: 'rate__update' = 'rate__update'; - -export type FiatRateAction = { - type: typeof RATE_UPDATE; - network: string; - rate: any; -} - -const loadRateAction = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { - const { config } = getState().localStorage; - if (!config) return; - - try { - config.fiatValueTickers.forEach(async (ticker) => { - // const rate: ?Array = await JSONRequest(`${ticker.url}?convert=USD`, 'json'); - const rate: ?Array = await httpRequest(`${ticker.url}?convert=USD`, 'json'); - console.log('rate'); - if (rate) { - dispatch({ - type: RATE_UPDATE, - network: ticker.network, - rate: rate[0], - }); - } - }); - } catch (error) { - // ignore error - } - - await resolveAfter(50000); -}; - -/** - * Middleware - */ -const CoinmarketcapService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => { - next(action); - - if (action.type === READY) { - api.dispatch(loadRateAction()); - } - - return action; -}; - -export default CoinmarketcapService; \ No newline at end of file diff --git a/src/services/TickerService.js b/src/services/TickerService.js index 42a71dd7..a6f7d2ce 100644 --- a/src/services/TickerService.js +++ b/src/services/TickerService.js @@ -30,7 +30,7 @@ const loadRateAction = (): AsyncAction => async (dispatch: Dispatch, getState: G config.fiatValueTickers.forEach(async (ticker) => { const response: ?Array = await httpRequest(`${ticker.url}?tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false`, 'json'); if (response) { - console.log({ + dispatch({ type: RATE_UPDATE, network: response.symbol, rate: response.market_data, diff --git a/src/views/Wallet/components/LeftNavigation/components/AccountMenu/index.js b/src/views/Wallet/components/LeftNavigation/components/AccountMenu/index.js index f41efa2a..e137f0ae 100644 --- a/src/views/Wallet/components/LeftNavigation/components/AccountMenu/index.js +++ b/src/views/Wallet/components/LeftNavigation/components/AccountMenu/index.js @@ -136,7 +136,7 @@ const AccountMenu = (props: Props) => { balance = `${availableBalance} ${network.symbol}`; if (fiatRate) { const accountBalance = new BigNumber(availableBalance); - const fiat = accountBalance.times(fiatRate.value).toFixed(2); + const fiat = accountBalance.times(parseInt(fiatRate.value, 10)).toFixed(2); balance = `${availableBalance} ${network.symbol} / $${fiat}`; } } diff --git a/src/views/Wallet/views/Account/Summary/components/Balance/index.js b/src/views/Wallet/views/Account/Summary/components/Balance/index.js index 1968abe1..d0614f40 100644 --- a/src/views/Wallet/views/Account/Summary/components/Balance/index.js +++ b/src/views/Wallet/views/Account/Summary/components/Balance/index.js @@ -107,17 +107,16 @@ class AccountBalance extends PureComponent { render() { const { network } = this.props; const fiatRate: any = this.props.fiat.find(f => f.network === network.shortcut); - let accountBalance = ''; let fiatRateValue = ''; let fiat = ''; if (fiatRate) { + const fiatValue = Math.round(fiatRate.value * 100) / 100; accountBalance = new BigNumber(this.props.balance); - fiatRateValue = new BigNumber(fiatRate.value).toFixed(2); + fiatRateValue = new BigNumber(fiatValue).toFixed(2); fiat = accountBalance.times(fiatRateValue).toFixed(2); } - return (