diff options
author | kumavis <aaron@kumavis.me> | 2019-03-29 11:54:12 +0800 |
---|---|---|
committer | kumavis <aaron@kumavis.me> | 2019-03-29 11:54:12 +0800 |
commit | 781a39c039500fcfe8fce3c3d75081ff781c4cf1 (patch) | |
tree | 5f7ba6db12b471ddd82e6be1d6e7dde0560d2cfd /ui/app/helpers/higher-order-components/with-token-tracker/with-token-tracker.component.js | |
parent | 07c974525859eba81471ec7591821ea010addf5a (diff) | |
parent | 649a1d483a574dcff902708f95b37329a02709a8 (diff) | |
download | tangerine-wallet-browser-781a39c039500fcfe8fce3c3d75081ff781c4cf1.tar.gz tangerine-wallet-browser-781a39c039500fcfe8fce3c3d75081ff781c4cf1.tar.zst tangerine-wallet-browser-781a39c039500fcfe8fce3c3d75081ff781c4cf1.zip |
Merge branch 'develop' into clearNotices
Diffstat (limited to 'ui/app/helpers/higher-order-components/with-token-tracker/with-token-tracker.component.js')
-rw-r--r-- | ui/app/helpers/higher-order-components/with-token-tracker/with-token-tracker.component.js | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/ui/app/helpers/higher-order-components/with-token-tracker/with-token-tracker.component.js b/ui/app/helpers/higher-order-components/with-token-tracker/with-token-tracker.component.js new file mode 100644 index 000000000..36f6a6efd --- /dev/null +++ b/ui/app/helpers/higher-order-components/with-token-tracker/with-token-tracker.component.js @@ -0,0 +1,106 @@ +import React, { Component } from 'react' +import PropTypes from 'prop-types' +import TokenTracker from 'eth-token-tracker' + +export default function withTokenTracker (WrappedComponent) { + return class TokenTrackerWrappedComponent extends Component { + static propTypes = { + userAddress: PropTypes.string.isRequired, + token: PropTypes.object.isRequired, + } + + constructor (props) { + super(props) + + this.state = { + string: '', + symbol: '', + error: null, + } + + this.tracker = null + this.updateBalance = this.updateBalance.bind(this) + this.setError = this.setError.bind(this) + } + + componentDidMount () { + this.createFreshTokenTracker() + } + + componentDidUpdate (prevProps) { + const { userAddress: newAddress, token: { address: newTokenAddress } } = this.props + const { userAddress: oldAddress, token: { address: oldTokenAddress } } = prevProps + + if ((oldAddress === newAddress) && (oldTokenAddress === newTokenAddress)) { + return + } + + if ((!oldAddress || !newAddress) && (!oldTokenAddress || !newTokenAddress)) { + return + } + + this.createFreshTokenTracker() + } + + componentWillUnmount () { + this.removeListeners() + } + + createFreshTokenTracker () { + this.removeListeners() + + if (!global.ethereumProvider) { + return + } + + const { userAddress, token } = this.props + + this.tracker = new TokenTracker({ + userAddress, + provider: global.ethereumProvider, + tokens: [token], + pollingInterval: 8000, + }) + + this.tracker.on('update', this.updateBalance) + this.tracker.on('error', this.setError) + + this.tracker.updateBalances() + .then(() => this.updateBalance(this.tracker.serialize())) + .catch(error => this.setState({ error: error.message })) + } + + setError (error) { + this.setState({ error }) + } + + updateBalance (tokens = []) { + if (!this.tracker.running) { + return + } + const [{ string, symbol }] = tokens + this.setState({ string, symbol, error: null }) + } + + removeListeners () { + if (this.tracker) { + this.tracker.stop() + this.tracker.removeListener('update', this.updateBalance) + this.tracker.removeListener('error', this.setError) + } + } + + render () { + const { string, symbol, error } = this.state + + return ( + <WrappedComponent + { ...this.props } + string={string} + symbol={symbol} + error={error} + /> + ) + } + } +} |