From 8d2c09407cf2621b890c6295530bbab098fe8f03 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Wed, 31 Jan 2018 19:31:15 +0300 Subject: [PATCH] fiat prices --- react/src/actions/actionCreators.js | 1 + react/src/actions/actions/fiatRates.js | 32 ----------- react/src/actions/actions/prices.js | 57 +++++++++++++++++++ react/src/actions/storeType.js | 2 +- .../walletsBalance/walletsBalance.js | 54 +++++++++++++++++- .../walletsBalance/walletsBalance.render.js | 24 ++++---- .../walletsBalance/walletsBalance.scss | 2 +- .../dashboard/walletsMain/walletsMain.js | 18 +++++- react/src/components/overrides.scss | 4 ++ react/src/reducers/dashboard.js | 7 ++- react/src/styles/index.scss | 1 + 11 files changed, 150 insertions(+), 52 deletions(-) delete mode 100644 react/src/actions/actions/fiatRates.js create mode 100644 react/src/actions/actions/prices.js diff --git a/react/src/actions/actionCreators.js b/react/src/actions/actionCreators.js index 506f0fa..882d972 100644 --- a/react/src/actions/actionCreators.js +++ b/react/src/actions/actionCreators.js @@ -51,6 +51,7 @@ export * from './actions/electrum'; export * from './actions/mm'; export * from './actions/nativeNetwork'; export * from './actions/tools'; +export * from './actions/prices'; export function changeActiveAddress(address) { return { diff --git a/react/src/actions/actions/fiatRates.js b/react/src/actions/actions/fiatRates.js deleted file mode 100644 index 401a9b3..0000000 --- a/react/src/actions/actions/fiatRates.js +++ /dev/null @@ -1,32 +0,0 @@ -import { FIAT_RATES } from '../storeType'; -import { triggerToaster } from '../actionCreators'; -import Config from '../../config'; - -export function fiatRates() { - return dispatch => { - return fetch(`http://46.20.235.46:8111/api/rates/kmd`, { - method: 'GET', - }) - .catch((error) => { - console.log(error); - dispatch( - triggerToaster( - 'fiatRates', - 'Error', - 'error' - ) - ); - }) - .then(response => response.json()) - .then(json => { - dispatch(fiatRates(json)); - }); - } -} - -function fiatRates(json) { - return { - type: FIAT_RATES, - response: json, - } -} \ No newline at end of file diff --git a/react/src/actions/actions/prices.js b/react/src/actions/actions/prices.js new file mode 100644 index 0000000..177f58a --- /dev/null +++ b/react/src/actions/actions/prices.js @@ -0,0 +1,57 @@ +import { PRICES } from '../storeType'; +import { triggerToaster } from '../actionCreators'; +import Config from '../../config'; + +function fiatRates(pricesJson) { + return dispatch => { + return fetch(`http://atomicexplorer.com/api/rates/kmd`, { + method: 'GET', + }) + .catch((error) => { + console.log(error); + dispatch( + triggerToaster( + 'fiatRates', + 'Error', + 'error' + ) + ); + }) + .then(response => response.json()) + .then(json => { + let _coins = pricesJson.result; + _coins.fiat = json.result; + + dispatch(pricesState(_coins)); + }); + } +} + +export function prices() { + return dispatch => { + return fetch(`http://atomicexplorer.com/api/mm/prices`, { + method: 'GET', + }) + .catch((error) => { + console.log(error); + dispatch( + triggerToaster( + 'prices', + 'Error', + 'error' + ) + ); + }) + .then(response => response.json()) + .then(json => { + dispatch(fiatRates(json)); + }); + } +} + +function pricesState(json) { + return { + type: PRICES, + prices: json, + } +} \ No newline at end of file diff --git a/react/src/actions/storeType.js b/react/src/actions/storeType.js index 3c2fc37..43675f0 100644 --- a/react/src/actions/storeType.js +++ b/react/src/actions/storeType.js @@ -50,7 +50,7 @@ export const DISPLAY_ZCASH_PARAMS_FETCH = 'DISPLAY_ZCASH_PARAMS_FETCH'; export const DASHBOARD_REMOVE_COIN = 'DASHBOARD_REMOVE_COIN'; export const DASHBOARD_ACTIVE_COIN_NET_PEERS = 'DASHBOARD_ACTIVE_COIN_NET_PEERS'; export const DASHBOARD_ACTIVE_COIN_NET_TOTALS = 'DASHBOARD_ACTIVE_COIN_NET_TOTALS'; -export const FIAT_RATES = 'FIAT_RATES'; +export const PRICES = 'PRICES'; /* dex */ export const DEX_LOGIN = 'DEX_LOGIN'; diff --git a/react/src/components/dashboard/walletsBalance/walletsBalance.js b/react/src/components/dashboard/walletsBalance/walletsBalance.js index db69274..aa26db4 100755 --- a/react/src/components/dashboard/walletsBalance/walletsBalance.js +++ b/react/src/components/dashboard/walletsBalance/walletsBalance.js @@ -5,6 +5,10 @@ import { getDashboardUpdate, shepherdElectrumBalance, } from '../../../actions/actionCreators'; +import mainWindow from '../../../util/mainWindow'; +import Config from '../../../config'; +import { formatValue } from '../../../util/formatValue'; +import ReactTooltip from 'react-tooltip'; import Store from '../../../store'; @@ -65,7 +69,7 @@ class WalletsBalance extends React.Component { } } - renderBalance(type) { + renderBalance(type, returnFiatPrice) { const _mode = this.props.ActiveCoin.mode; let _balance = 0; @@ -102,7 +106,53 @@ class WalletsBalance extends React.Component { } } - return Number(_balance); + if (mainWindow.appConfig.fiatRates && + this.props.Dashboard.prices && + returnFiatPrice) { + const _prices = this.props.Dashboard.prices; + let _fiatPriceTotal = 0; + let _fiatPricePerCoin = 0; + + if (this.props.ActiveCoin.coin === 'KMD') { + if (_prices.fiat && + _prices.fiat.USD) { + _fiatPriceTotal = formatValue(_balance * _prices.fiat.USD); + _fiatPricePerCoin = _prices.fiat.USD; + } + } else { + if (_prices.fiat && + _prices.fiat.USD && + _prices[`${this.props.ActiveCoin.coin}/KMD`] && + _prices[`${this.props.ActiveCoin.coin}/KMD`].low) { + _fiatPriceTotal = _balance * _prices.fiat.USD * _prices[`${this.props.ActiveCoin.coin}/KMD`].low; + _fiatPricePerCoin = _prices.fiat.USD * _prices[`${this.props.ActiveCoin.coin}/KMD`].low; + } + } + + return ( +
+
{ _balance }
+ { _fiatPriceTotal > 0 && + _fiatPricePerCoin > 0 && +
${ formatValue(_fiatPriceTotal) }
+ } + { _fiatPriceTotal > 0 && + _fiatPricePerCoin > 0 && + + } +
+ ); + } else { + if (Config.roundValues) { + return formatValue(_balance); + } else { + return Number(_balance); + } + } } isActiveCoinMode(coinMode) { diff --git a/react/src/components/dashboard/walletsBalance/walletsBalance.render.js b/react/src/components/dashboard/walletsBalance/walletsBalance.render.js index 74f9d01..9d1c14c 100644 --- a/react/src/components/dashboard/walletsBalance/walletsBalance.render.js +++ b/react/src/components/dashboard/walletsBalance/walletsBalance.render.js @@ -1,9 +1,8 @@ import React from 'react'; import ReactTooltip from 'react-tooltip'; import { translate } from '../../../translate/translate'; -import { formatValue } from '../../../util/formatValue'; -import Config from '../../../config'; import Spinner from '../spinner/spinner'; +import Config from '../../../config'; const WalletsBalanceRender = function() { return ( @@ -26,7 +25,7 @@ const WalletsBalanceRender = function() { onClick={ this.refreshBalance }> }
-
+
{ this.props.ActiveCoin.coin !== 'CHIPS' && this.props.ActiveCoin.mode !== 'spv' && @@ -41,7 +40,7 @@ const WalletsBalanceRender = function() { - { Config.roundValues ? formatValue(this.renderBalance('transparent')) : this.renderBalance('transparent') } + { this.renderBalance('transparent', true) } 0 ? 'col-lg-3 col-xs-12' : 'hide' }>
-
+
{ translate('INDEX.Z_BALANCE') }
- { Config.roundValues ? formatValue(this.renderBalance('private')) : this.renderBalance('private') } + data-tip={ Config.roundValues ? this.renderBalance('private') : '' }> + { this.renderBalance('private', true) } +
@@ -74,7 +76,7 @@ const WalletsBalanceRender = function() {
-
+
{ translate('INDEX.INTEREST_EARNED') } @@ -82,7 +84,7 @@ const WalletsBalanceRender = function() { - { Config.roundValues ? formatValue(this.renderBalance('interest')) : this.renderBalance('interest') } + { this.renderBalance('interest', true) }
-
+
{ translate('INDEX.TOTAL_BALANCE') } @@ -105,7 +107,7 @@ const WalletsBalanceRender = function() { - { Config.roundValues ? formatValue(this.renderBalance('total')) : this.renderBalance('total') } + { this.renderBalance('total', true) } this.updateSocketsData(msg)); } + componentWillUnmount() { + if (this.pricesInterval) { + clearInterval(this.pricesInterval); + } + } + componentWillMount() { - console.warn('login'); + Store.dispatch(prices()); + this.pricesInterval = setInterval(() => { + Store.dispatch(prices()); + }, PRICES_UPDATE_INTERVAL); if (mainWindow.createSeed.triggered && !mainWindow.createSeed.secondaryLoginPH) { diff --git a/react/src/components/overrides.scss b/react/src/components/overrides.scss index a514195..ba4e207 100644 --- a/react/src/components/overrides.scss +++ b/react/src/components/overrides.scss @@ -324,4 +324,8 @@ select{ &.type-dark { background-color: #000; } +} + +.cursor-default { + cursor: default; } \ No newline at end of file diff --git a/react/src/reducers/dashboard.js b/react/src/reducers/dashboard.js index 58cd261..caf596b 100644 --- a/react/src/reducers/dashboard.js +++ b/react/src/reducers/dashboard.js @@ -8,7 +8,7 @@ import { DASHBOARD_ELECTRUM_COINS, ELECTRUM_SERVER_CHANGED, DISPLAY_ZCASH_PARAMS_FETCH, - FIAT_RATES, + PRICES, } from '../actions/storeType'; export function Dashboard(state = { @@ -21,6 +21,7 @@ export function Dashboard(state = { electrumCoins: {}, eletrumServerChanged: false, displayZcparamsModal: false, + prices: null, }, action) { switch (action.type) { case DASHBOARD_ELECTRUM_COINS: @@ -72,10 +73,10 @@ export function Dashboard(state = { ...state, eletrumServerChanged: action.eletrumServerChanged, }; - case FIAT_RATES: + case PRICES: return { ...state, - fiatRates: action.fiatRates, + prices: action.prices, }; default: return state; diff --git a/react/src/styles/index.scss b/react/src/styles/index.scss index 5d942cd..6e82ab4 100644 --- a/react/src/styles/index.scss +++ b/react/src/styles/index.scss @@ -44,6 +44,7 @@ @import '../components/dashboard/navbar/navbar.scss'; @import '../components/dashboard/settings/settings.scss'; @import '../components/dashboard/walletsData/walletsData.scss'; +@import '../components/dashboard/walletsBalance/walletsBalance.scss'; @import '../components/dashboard/claimInterestModal/claimInterestModal.scss'; @import '../components/dashboard/importKeyModal/importKeyModal.scss'; @import '../components/dashboard/jumblr/jumblr.scss';