diff options
author | Alexander Tseung <alextsg@gmail.com> | 2018-07-31 03:02:55 +0800 |
---|---|---|
committer | Alexander Tseung <alextsg@gmail.com> | 2018-08-24 07:44:43 +0800 |
commit | 4e0693eaff0107d11bf93042db50cbb022cfeed8 (patch) | |
tree | 36d999929610b4f20646cb6265231105aa6c042f /ui/app/helpers | |
parent | 40d4ac9ae1ed9557d066c184abd90e51a380cf06 (diff) | |
download | tangerine-wallet-browser-4e0693eaff0107d11bf93042db50cbb022cfeed8.tar tangerine-wallet-browser-4e0693eaff0107d11bf93042db50cbb022cfeed8.tar.gz tangerine-wallet-browser-4e0693eaff0107d11bf93042db50cbb022cfeed8.tar.bz2 tangerine-wallet-browser-4e0693eaff0107d11bf93042db50cbb022cfeed8.tar.lz tangerine-wallet-browser-4e0693eaff0107d11bf93042db50cbb022cfeed8.tar.xz tangerine-wallet-browser-4e0693eaff0107d11bf93042db50cbb022cfeed8.tar.zst tangerine-wallet-browser-4e0693eaff0107d11bf93042db50cbb022cfeed8.zip |
Add withMethodData HOC, add higher-order-component folder
Diffstat (limited to 'ui/app/helpers')
-rw-r--r-- | ui/app/helpers/with-token-tracker.js | 108 |
1 files changed, 0 insertions, 108 deletions
diff --git a/ui/app/helpers/with-token-tracker.js b/ui/app/helpers/with-token-tracker.js deleted file mode 100644 index 8608b15f4..000000000 --- a/ui/app/helpers/with-token-tracker.js +++ /dev/null @@ -1,108 +0,0 @@ -import React, { Component } from 'react' -import PropTypes from 'prop-types' -import TokenTracker from 'eth-token-tracker' - -const 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} - /> - ) - } - } -} - -module.exports = withTokenTracker |