From d88188c3b1bc413d4eba7da090b720d8b8cdc030 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Wed, 15 Jul 2020 18:56:05 -0400 Subject: [PATCH 1/2] feat(rate-limits): Display rate limit error to the users --- src/components/admin-lte/configure/index.js | 20 +- src/components/admin-lte/send-receive/send.js | 58 ++++-- src/components/admin-lte/tokens/index.js | 183 ++++++++++-------- .../admin-lte/tokens/send-tokens.js | 44 +++-- src/components/admin-lte/wallet/create.js | 48 +++-- src/components/admin-lte/wallet/import.js | 45 +++-- 6 files changed, 244 insertions(+), 154 deletions(-) diff --git a/src/components/admin-lte/configure/index.js b/src/components/admin-lte/configure/index.js index f3dc8e9..1fc4910 100644 --- a/src/components/admin-lte/configure/index.js +++ b/src/components/admin-lte/configure/index.js @@ -8,7 +8,7 @@ const { Text } = Inputs let _this class Configure extends React.Component { - constructor (props) { + constructor(props) { super(props) _this = this @@ -21,7 +21,7 @@ class Configure extends React.Component { _this.BchWallet = BchWallet } - render () { + render() { return ( @@ -113,11 +113,11 @@ class Configure extends React.Component { ) } - componentDidMount () { + componentDidMount() { _this.setJwt() } - setJwt () { + setJwt() { const { JWT } = _this.props.walletInfo if (JWT) { const jwtElem = document.getElementById('jwt') @@ -125,27 +125,26 @@ class Configure extends React.Component { } } - handleUpdate (event) { + handleUpdate(event) { const value = event.target.value _this.setState({ [event.target.name]: value }) - console.log(_this.state.JWT) } - async handleUpdateJWT () { + async handleUpdateJWT() { try { const { mnemonic, selectedServer } = _this.props.walletInfo const apiToken = _this.state.JWT // Update instance with JWT - if (mnemonic && apiToken) { + if (mnemonic) { const bchjsOptions = { apiToken: apiToken } if (selectedServer) { bchjsOptions.restURL = selectedServer } - console.log('bchjs options : ', bchjsOptions) + //console.log('bchjs options : ', bchjsOptions) const bchWalletLib = new _this.BchWallet(mnemonic, bchjsOptions) // Update bchjs instances of minimal-slp-wallet libraries @@ -161,7 +160,6 @@ class Configure extends React.Component { _this.props.setWalletInfo(walletInfo) } catch (error) { - console.warn(error) _this.setState({ errMsg: error.message }) @@ -169,7 +167,7 @@ class Configure extends React.Component { } // Reset form and component state - resetValues () { + resetValues() { _this.setState({ JWT: '', errMsg: '' diff --git a/src/components/admin-lte/send-receive/send.js b/src/components/admin-lte/send-receive/send.js index cb06c75..e5f9a1e 100644 --- a/src/components/admin-lte/send-receive/send.js +++ b/src/components/admin-lte/send-receive/send.js @@ -18,7 +18,8 @@ class Send extends React.Component { amountSat: '', errMsg: '', txId: '', - showScan: false + showScan: false, + inFetch: false } _this.BchWallet = BchWallet } @@ -30,7 +31,9 @@ class Send extends React.Component { - +

@@ -102,7 +105,6 @@ class Send extends React.Component { _this.setState({ [event.target.name]: value }) - // console.log(_this.state) } async handleSend () { @@ -124,6 +126,9 @@ class Send extends React.Component { if (!bchWalletLib) { throw new Error('Wallet not found') } + _this.setState({ + inFetch:true + }) // Ensure the wallet UTXOs are up-to-date. const walletAddr = bchWalletLib.walletInfo.address @@ -147,19 +152,8 @@ class Send extends React.Component { _this.resetValues() } catch (error) { - let errMsg - if (error.message) { - errMsg = error.message - } else if (error.error) { - errMsg = error.error - } - _this.setState(prevState => { - return { - ...prevState, - errMsg, - txId: '' - } - }) + + _this.handleError(error) } } @@ -168,7 +162,8 @@ class Send extends React.Component { _this.setState({ address: '', amountSat: '', - errMsg: '' + errMsg: '', + inFetch: false }) const amountEle = document.getElementById('amountToSend') amountEle.value = '' @@ -180,7 +175,6 @@ class Send extends React.Component { validateInputs () { const { address, amountSat } = _this.state const amountNumber = Number(amountSat) - console.log(_this.state) if (!address) { throw new Error('Address is required') @@ -255,7 +249,33 @@ class Send extends React.Component { } } - componentDidMount () {} + handleError(error){ + //console.error(error) + let errMsg = '' + if (error.message) { + errMsg = error.message + } + if (error.error) { + if (error.error.match('rate limits')) { + errMsg = + Rate limits exceeded, increase rate limits with a JWT token from + FullStack.cash + + } else { + errMsg = error.error + } + } + _this.setState(prevState => { + return { + ...prevState, + errMsg, + txId: '', + inFetch:false + } + }) + } } Send.propTypes = { updateBalance: PropTypes.func.isRequired, diff --git a/src/components/admin-lte/tokens/index.js b/src/components/admin-lte/tokens/index.js index 7c54551..35ba27f 100644 --- a/src/components/admin-lte/tokens/index.js +++ b/src/components/admin-lte/tokens/index.js @@ -9,7 +9,7 @@ import SendTokens from './send-tokens' let _this class Tokens extends React.Component { - constructor (props) { + constructor(props) { super(props) _this = this this.state = { @@ -17,13 +17,13 @@ class Tokens extends React.Component { selectedTokenToView: '', showModal: false, inFetch: true, - onEmptyTokensMsg: 'No tokens found on this wallet..', + errMsg: '', selectedTokenToSend: '', showForm: false } } - render () { + render() { const { JWT } = _this.props.walletInfo return ( @@ -46,59 +46,59 @@ class Tokens extends React.Component { Loading... ) : ( - - {!JWT && ( - - - - - + + {!JWT && ( + + + + + - - - You don't have a registered JWT, you could encounter - errors viewing some of your tokens. + + + You don't have a registered JWT, you could encounter + errors viewing some of your tokens. - - - - )} -
- {!_this.state.tokens.length && ( - - - - {_this.state.onEmptyTokensMsg} - - - - )} + +
+
+ )} +
+ {_this.state.errMsg && ( + + + + {_this.state.errMsg} + + + + )} - {_this.state.tokens.length > 0 && ( - <> - - {_this.state.tokens.map((val, i) => { - return ( - - - - ) - })} - - - )} -
- )} + {_this.state.tokens.length > 0 && ( + <> + + {_this.state.tokens.map((val, i) => { + return ( + + + + ) + })} + + + )} + + )} + Rate limits exceeded, increase rate limits with a JWT token from + FullStack.cash + + } else { + errMsg = error.error + } + } + _this.setState(prevState => { + return { + ...prevState, + errMsg: errMsg, + txId: '', + inFetch: false + } + }) + + } } Tokens.propTypes = { walletInfo: PropTypes.object.isRequired, // wallet info diff --git a/src/components/admin-lte/tokens/send-tokens.js b/src/components/admin-lte/tokens/send-tokens.js index c9585c9..74e7781 100644 --- a/src/components/admin-lte/tokens/send-tokens.js +++ b/src/components/admin-lte/tokens/send-tokens.js @@ -173,20 +173,7 @@ class SendTokens extends React.Component { _this.props.handleSend() }, 1000) } catch (error) { - let errMsg - if (error.message) { - errMsg = error.message - } else if (error.error) { - errMsg = error.error - } - _this.setState(prevState => { - return { - ...prevState, - errMsg, - txId: '', - inFetch: false - } - }) + _this.handleError(error) } } @@ -207,7 +194,6 @@ class SendTokens extends React.Component { validateInputs () { const { address, amountSat } = _this.state const amountNumber = Number(amountSat) - console.log(_this.state) if (!address) { throw new Error('Address is required') @@ -282,7 +268,33 @@ class SendTokens extends React.Component { } } - componentDidMount () {} + handleError(error){ + //console.error(error) + let errMsg = '' + if (error.message) { + errMsg = error.message + } + if (error.error) { + if (error.error.match('rate limits')) { + errMsg = + Rate limits exceeded, increase rate limits with a JWT token from + FullStack.cash + + } else { + errMsg = error.error + } + } + _this.setState(prevState => { + return { + ...prevState, + errMsg, + txId: '', + inFetch: false + } + }) + } } SendTokens.propTypes = { walletInfo: PropTypes.object.isRequired, // wallet info diff --git a/src/components/admin-lte/wallet/create.js b/src/components/admin-lte/wallet/create.js index cd70309..5d1daa8 100644 --- a/src/components/admin-lte/wallet/create.js +++ b/src/components/admin-lte/wallet/create.js @@ -6,7 +6,7 @@ import BchWallet from 'minimal-slp-wallet' let _this class NewWallet extends React.Component { - constructor (props) { + constructor(props) { super(props) _this = this this.state = { @@ -17,7 +17,7 @@ class NewWallet extends React.Component { _this.BchWallet = BchWallet } - render () { + render() { return ( <> @@ -38,11 +38,6 @@ class NewWallet extends React.Component { New Wallet

- - {_this.state.errMsg && ( -

{_this.state.errMsg}

- )} -