diff --git a/src/App.js b/src/App.js
index be39888..090fbad 100644
--- a/src/App.js
+++ b/src/App.js
@@ -65,6 +65,44 @@ function App (props) {
return false
}, [appData, addToModal])
+ /**
+ * Run background process to get bch and slp balance.
+ * Also update the background state process.
+ * On error this function should trigger a info modal notifying the errors.
+ */
+ const backgroundAsync = useCallback(async (asyncLoad, walletTemp) => {
+ try {
+ appData.setModalBody(['Getting BCH balance in background!.'])
+ // Get Wallet Balance
+ await asyncLoad.getWalletBchBalance(walletTemp, appData.updateBchWalletState, appData)
+ // Update Background state
+ appData.updateBackGroundInitState({ bchInitLoaded: true })
+
+ // Get SLP Balance
+ appData.setModalBody(['Getting SLP tokens in background!.'])
+ await asyncLoad.getSlpTokenBalances(walletTemp, appData.updateBchWalletState, appData)
+
+ // Update Background state
+ appData.updateBackGroundInitState({ slpInitLoaded: true, asyncBackgroundFinished: true })
+ } catch (err) {
+ console.log('App.js backgroundAsync() error!', err)
+
+ appData.updateBackGroundInitState({ asyncBackgroundFinished: true })
+
+ addToModal(`Error: ${err.message}`, appData)
+ addToModal('Try selecting a different back end server using the drop-down menu at the bottom of the app.\'', appData)
+
+ // Update Modal State
+ appData.setHideSpinner(true)
+ appData.setShowStartModal(true)
+ appData.setDenyClose(false)
+
+ // Update the startup state.
+ appData.setAsyncInitFinished(true)
+ appData.setAsyncInitSucceeded(false)
+ }
+ }, [appData, addToModal])
+
/** Load all required data before component start. */
useEffect(() => {
async function asyncEffect () {
@@ -95,14 +133,6 @@ function App (props) {
appData.setWallet(walletTemp)
// appData.updateBchWalletState({ walletObj: walletTemp.walletInfo, appData })
- // Get the BCH balance of the wallet.
- addToModal('Getting BCH balance', appData)
- await asyncLoad.getWalletBchBalance(walletTemp, appData.updateBchWalletState, appData)
-
- // Get the SLP tokens held by the wallet.
- addToModal('Getting SLP tokens', appData)
- await asyncLoad.getSlpTokenBalances(walletTemp, appData.updateBchWalletState, appData)
-
// Get the BCH spot price
addToModal('Getting BCH spot price in USD', appData)
await asyncLoad.getUSDExchangeRate(walletTemp, appData.updateBchWalletState, appData)
@@ -115,6 +145,13 @@ function App (props) {
appData.setAsyncInitFinished(true)
appData.setAsyncInitSucceeded(true)
console.log('App.js useEffect() startup finished successfully')
+
+ backgroundAsync(asyncLoad, walletTemp)
+ // Get the BCH balance of the wallet.
+ // addToModal('Getting BCH balance', appData)
+
+ // Get the SLP tokens held by the wallet.
+ // addToModal('Getting SLP tokens', appData)
} catch (err) {
const errModalBody = [
`Error: ${err.message}`,
@@ -134,7 +171,7 @@ function App (props) {
}
}
asyncEffect()
- }, [appData, addToModal, isSignleView])
+ }, [appData, addToModal, isSignleView, backgroundAsync])
return (
<>
diff --git a/src/components/app-body/bch-send/balance-card.js b/src/components/app-body/bch-send/balance-card.js
index 3fa541c..62b5a46 100644
--- a/src/components/app-body/bch-send/balance-card.js
+++ b/src/components/app-body/bch-send/balance-card.js
@@ -4,7 +4,7 @@
// Global npm libraries
import React from 'react'
-import { Container, Row, Col, Card } from 'react-bootstrap'
+import { Container, Row, Col, Card, Spinner } from 'react-bootstrap'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faCoins } from '@fortawesome/free-solid-svg-icons'
@@ -15,6 +15,11 @@ const BalanceCard = (props) => {
const sats = appData.bchWalletState.bchBalance
const bchBalance = bchjs.BitcoinCash.toBitcoinCash(sats)
const usdBalance = bchjs.Util.floor2(bchBalance * appData.bchWalletState.bchUsdPrice)
+ const { bchInitLoaded, asyncBackgroundFinished } = appData.asyncBackGroundInitState
+
+ // Background bch data loaded finished
+ const backgroundDataLoaded = bchInitLoaded || asyncBackgroundFinished
+ const backgroundDataError = !bchInitLoaded && asyncBackgroundFinished
return (
<>
@@ -25,25 +30,37 @@ const BalanceCard = (props) => {
-
-
-
- USD: ${usdBalance}
-
-
+ {bchInitLoaded && (
+
+
+
+ USD: ${usdBalance}
+
+
-
-
- BCH: {bchBalance}
-
-
+
+
+ BCH: {bchBalance}
+
+
-
-
- Satoshis: {sats}
-
-
-
+
+
+ Satoshis: {sats}
+
+
+ )}
+ {backgroundDataError && (
+
+ Balance could not be loaded!
+
+ )}
+
+ {!backgroundDataLoaded && (
+
+
+
+ )}
>
diff --git a/src/components/app-body/bch-send/refresh-balance.js b/src/components/app-body/bch-send/refresh-balance.js
index b95163f..ec33e89 100644
--- a/src/components/app-body/bch-send/refresh-balance.js
+++ b/src/components/app-body/bch-send/refresh-balance.js
@@ -50,6 +50,9 @@ export default function RefreshBchBalance (props) {
// Get the latest balance of the wallet.
const newBalance = await wallet.getBalance({ bchAddress: cashAddr })
+ // if bchInitLoaded is 'false', them set as true , to show the new balance.
+ appData.updateBackGroundInitState({ bchInitLoaded: true })
+
addToModal('Updating BCH per USD price...')
const bchUsdPrice = await wallet.getUsd()
diff --git a/src/components/app-body/bch-send/refresh-bch-balance-button.js b/src/components/app-body/bch-send/refresh-bch-balance-button.js
index 80d7044..d63c44f 100644
--- a/src/components/app-body/bch-send/refresh-bch-balance-button.js
+++ b/src/components/app-body/bch-send/refresh-bch-balance-button.js
@@ -16,6 +16,10 @@ import RefreshBchBalance from './refresh-balance'
function RefreshBchBalanceButton (props) {
// Dependency injections of props
const appData = props.appData
+ const { bchInitLoaded, asyncBackgroundFinished } = appData.asyncBackGroundInitState
+
+ // Background bch data loaded finished
+ const backgroundDataLoaded = bchInitLoaded || asyncBackgroundFinished
// Child function references
const refreshBchBalanceRef = useRef()
@@ -28,7 +32,7 @@ function RefreshBchBalanceButton (props) {
return (
<>
-