diff --git a/src/components/admin-lte/configure/index.js b/src/components/admin-lte/configure/index.js index 333566f..43542d4 100644 --- a/src/components/admin-lte/configure/index.js +++ b/src/components/admin-lte/configure/index.js @@ -38,6 +38,7 @@ class Configure extends React.Component { setWalletInfo={_this.props.setWalletInfo} walletInfo={_this.props.walletInfo} setBchWallet={_this.props.setBchWallet} + updateBalance={_this.props.updateBalance} /> - +

@@ -194,6 +198,11 @@ class Servers extends React.Component { } handleUpdateServer () { + // Show loader spinner + _this.setState({ + inFetch: true + }) + _this.handleNewServerUrl() // await for state update delay setTimeout(() => { @@ -224,12 +233,21 @@ class Servers extends React.Component { bchWalletLib.tokens.utxos.bchjs = new bchWalletLib.BCHJS(bchjsOptions) _this.props.setBchWallet(bchWalletLib) + + // update server current price + _this.handleUpdateBalance(bchWalletLib) + } else { + // Hide loader spinner + _this.setState({ + inFetch: false + }) } _this.saveServer() } catch (error) { console.warn(error) _this.setState({ - errMsg: error.message + errMsg: error.message, + inFetch: false }) } } @@ -263,6 +281,44 @@ class Servers extends React.Component { } } + // Get wallet balance + async handleUpdateBalance (bchWallet) { + try { + const { mnemonic } = _this.props.walletInfo + if (mnemonic && bchWallet) { + const bchWalletLib = bchWallet + await bchWalletLib.walletInfoPromise + const myBalance = await bchWalletLib.getBalance() + + const bchjs = bchWalletLib.bchjs + + let currentRate + + if (bchjs.restURL.includes('abc.fullstack')) { + currentRate = await bchjs.Price.getBchaUsd() * 100 + } else { + // BCHN price. + currentRate = (await bchjs.Price.getUsd()) * 100 + } + + _this.setState({ + currentRate: currentRate + }) + _this.props.updateBalance({ myBalance, currentRate }) + } + // Hide loader spinner + _this.setState({ + inFetch: false + }) + } catch (error) { + console.error(error) + // Hide loader spinner + _this.setState({ + inFetch: false + }) + } + } + // clean the text form field resetForm () { _this.setState({ @@ -273,7 +329,8 @@ class Servers extends React.Component { Servers.propTypes = { setWalletInfo: PropTypes.func.isRequired, walletInfo: PropTypes.object.isRequired, - setBchWallet: PropTypes.func.isRequired + setBchWallet: PropTypes.func.isRequired, + updateBalance: PropTypes.func.isRequired } export default Servers diff --git a/src/components/admin-lte/index.js b/src/components/admin-lte/index.js index ff7abc9..be4437f 100644 --- a/src/components/admin-lte/index.js +++ b/src/components/admin-lte/index.js @@ -132,7 +132,16 @@ class AdminLTEPage extends React.Component { const myBalance = await bchWalletLib.getBalance() const bchjs = bchWalletLib.bchjs - const currentRate = await bchjs.Price.getUsd() * 100 + + let currentRate + + if (bchjs.restURL.includes('abc.fullstack')) { + currentRate = await bchjs.Price.getBchaUsd() * 100 + } else { + // BCHN price. + currentRate = (await bchjs.Price.getUsd()) * 100 + } + _this.setState({ currentRate: currentRate }) @@ -188,6 +197,7 @@ class AdminLTEPage extends React.Component { _this.changeSection(_this.defaultSection) } } + _this.updateState() } // Update component state when props change @@ -199,7 +209,11 @@ class AdminLTEPage extends React.Component { } if (_this.props.bchBalance.bchBalance !== _this.state.bchBalance) { _this.setState({ - bchBalance: _this.props.bchBalance.bchBalance, + bchBalance: _this.props.bchBalance.bchBalance + }) + } + if (_this.props.bchBalance.usdBalance !== _this.state.usdBalance) { + _this.setState({ usdBalance: _this.props.bchBalance.usdBalance }) } @@ -341,7 +355,7 @@ class AdminLTEPage extends React.Component { return '' }) ) - } catch (err) { } + } catch (err) {} } getInvisibleMenuItem () { @@ -371,10 +385,10 @@ class AdminLTEPage extends React.Component { const servers = [server1, server2] + // Default server is BCHN. let selectedServer = server1 - // Assign the second server if the url path - // is different of 'wallet.fullstack.cash' - if (accessLocation !== 'wallet.fullstack.cash') { + + if (accessLocation === 'wallet.fullstack.cash') { selectedServer = server1 } @@ -387,6 +401,11 @@ class AdminLTEPage extends React.Component { selectedServer = server2 } + // Split uses BCHN by default + if (accessLocation === 'splitbch.com') { + selectedServer = server1 + } + walletInfo.selectedServer = selectedServer walletInfo.servers = servers diff --git a/src/components/admin-lte/wallet/import.js b/src/components/admin-lte/wallet/import.js index 92c52d7..cbb3ecd 100644 --- a/src/components/admin-lte/wallet/import.js +++ b/src/components/admin-lte/wallet/import.js @@ -148,8 +148,19 @@ class ImportWallet extends React.Component { const myBalance = await bchWalletLib.getBalance() const bchjs = bchWalletLib.bchjs - const currentRate = await bchjs.Price.getUsd() * 100 + let currentRate + if (bchjs.restURL.includes('abc.fullstack')) { + currentRate = await bchjs.Price.getBchaUsd() * 100 + } else { + // BCHN price. + currentRate = (await bchjs.Price.getUsd()) * 100 + } + + _this.setState({ + currentRate: currentRate + }) + _this.props.updateBalance({ myBalance, currentRate }) // Update redux state _this.props.setWalletInfo(currentWallet) _this.props.updateBalance({ myBalance, currentRate }) diff --git a/src/redux/createStore.js b/src/redux/createStore.js index 728017b..c654e13 100644 --- a/src/redux/createStore.js +++ b/src/redux/createStore.js @@ -28,6 +28,7 @@ const reducer = (state, action) => { if (action.type === 'UPDATE_BALANCE') { // Convert satoshis to bch const { myBalance, currentRate } = action.value + console.log(`currentRate: ${currentRate}`) const satoshis = myBalance const bch = satoshis / 100000000