2017-04-21 04:07:09 +02:00
|
|
|
const Component = require('react').Component
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
|
|
|
const TokenTracker = require('eth-token-tracker')
|
|
|
|
const TokenCell = require('./token-cell.js')
|
2017-06-14 02:47:56 +02:00
|
|
|
|
2017-04-21 04:07:09 +02:00
|
|
|
module.exports = TokenList
|
|
|
|
|
|
|
|
inherits(TokenList, Component)
|
|
|
|
function TokenList () {
|
2017-06-16 01:22:53 +02:00
|
|
|
this.state = {
|
2017-06-20 17:58:25 +02:00
|
|
|
tokens: [],
|
2017-06-16 01:22:53 +02:00
|
|
|
isLoading: true,
|
|
|
|
network: null,
|
|
|
|
}
|
2017-04-21 04:07:09 +02:00
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
TokenList.prototype.render = function () {
|
2017-06-14 02:47:56 +02:00
|
|
|
const state = this.state
|
2017-06-20 04:11:55 +02:00
|
|
|
const { tokens, isLoading, error } = state
|
2017-06-16 01:22:53 +02:00
|
|
|
const { userAddress, network } = this.props
|
2017-06-14 02:47:56 +02:00
|
|
|
|
2017-06-15 00:30:03 +02:00
|
|
|
if (isLoading) {
|
2017-06-15 03:08:03 +02:00
|
|
|
return this.message('Loading')
|
2017-06-15 00:30:03 +02:00
|
|
|
}
|
2017-06-14 02:47:56 +02:00
|
|
|
|
2017-06-20 04:11:55 +02:00
|
|
|
if (error) {
|
|
|
|
log.error(error)
|
2017-08-16 21:53:53 +02:00
|
|
|
return h('.hotFix', {
|
|
|
|
style: {
|
|
|
|
padding: '80px',
|
|
|
|
},
|
|
|
|
}, [
|
|
|
|
'We had trouble loading your token balances. You can view them ',
|
|
|
|
h('span.hotFix', {
|
|
|
|
style: {
|
|
|
|
color: 'rgba(247, 134, 28, 1)',
|
|
|
|
cursor: 'pointer',
|
|
|
|
},
|
|
|
|
onClick: () => {
|
|
|
|
global.platform.openWindow({
|
|
|
|
url: `https://ethplorer.io/address/${userAddress}`,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}, 'here'),
|
|
|
|
])
|
2017-06-20 04:11:55 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 18:01:51 +02:00
|
|
|
const tokenViews = tokens.map((tokenData) => {
|
2017-04-24 22:55:19 +02:00
|
|
|
tokenData.network = network
|
2017-06-14 02:47:56 +02:00
|
|
|
tokenData.userAddress = userAddress
|
2017-04-21 18:01:51 +02:00
|
|
|
return h(TokenCell, tokenData)
|
|
|
|
})
|
|
|
|
|
2017-07-26 03:22:31 +02:00
|
|
|
return h('.full-flex-height', [
|
|
|
|
this.renderTokenStatusBar(),
|
|
|
|
|
|
|
|
h('ol.full-flex-height.flex-column', {
|
2017-06-14 02:47:56 +02:00
|
|
|
style: {
|
|
|
|
overflowY: 'auto',
|
2017-06-15 05:42:48 +02:00
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
2017-06-14 02:47:56 +02:00
|
|
|
},
|
2017-06-15 05:42:48 +02:00
|
|
|
}, [
|
|
|
|
h('style', `
|
|
|
|
|
|
|
|
li.token-cell {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
align-items: center;
|
|
|
|
padding: 10px;
|
2017-07-26 03:22:31 +02:00
|
|
|
min-height: 50px;
|
2017-06-15 05:42:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
li.token-cell > h3 {
|
|
|
|
margin-left: 12px;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.token-cell:hover {
|
|
|
|
background: white;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
`),
|
|
|
|
...tokenViews,
|
2017-07-26 03:22:31 +02:00
|
|
|
h('.flex-grow'),
|
2017-06-15 05:42:48 +02:00
|
|
|
]),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2017-07-26 03:22:31 +02:00
|
|
|
TokenList.prototype.renderTokenStatusBar = function () {
|
|
|
|
const { tokens } = this.state
|
|
|
|
|
|
|
|
let msg
|
2017-07-28 03:07:42 +02:00
|
|
|
if (tokens.length === 1) {
|
|
|
|
msg = `You own 1 token`
|
2017-08-30 02:54:01 +02:00
|
|
|
} else if (tokens.length > 1) {
|
2017-07-26 03:22:31 +02:00
|
|
|
msg = `You own ${tokens.length} tokens`
|
|
|
|
} else {
|
|
|
|
msg = `No tokens found`
|
|
|
|
}
|
|
|
|
|
|
|
|
return h('div', {
|
|
|
|
style: {
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'center',
|
|
|
|
minHeight: '70px',
|
|
|
|
padding: '10px',
|
|
|
|
},
|
|
|
|
}, [
|
|
|
|
h('span', msg),
|
|
|
|
h('button', {
|
2017-06-15 05:42:48 +02:00
|
|
|
key: 'reveal-account-bar',
|
2017-07-26 03:22:31 +02:00
|
|
|
onClick: (event) => {
|
|
|
|
event.preventDefault()
|
2017-06-15 05:42:48 +02:00
|
|
|
this.props.addToken()
|
|
|
|
},
|
|
|
|
style: {
|
|
|
|
display: 'flex',
|
|
|
|
height: '40px',
|
|
|
|
padding: '10px',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
|
|
|
}, [
|
2017-07-26 03:22:31 +02:00
|
|
|
'ADD TOKEN',
|
2017-06-15 05:42:48 +02:00
|
|
|
]),
|
|
|
|
])
|
2017-04-21 04:07:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-15 03:08:03 +02:00
|
|
|
TokenList.prototype.message = function (body) {
|
|
|
|
return h('div', {
|
|
|
|
style: {
|
|
|
|
display: 'flex',
|
|
|
|
height: '250px',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
2017-06-29 01:36:58 +02:00
|
|
|
padding: '30px',
|
2017-06-15 03:08:03 +02:00
|
|
|
},
|
|
|
|
}, body)
|
|
|
|
}
|
|
|
|
|
2017-04-21 04:07:09 +02:00
|
|
|
TokenList.prototype.componentDidMount = function () {
|
2017-06-15 00:17:46 +02:00
|
|
|
this.createFreshTokenTracker()
|
|
|
|
}
|
|
|
|
|
|
|
|
TokenList.prototype.createFreshTokenTracker = function () {
|
|
|
|
if (this.tracker) {
|
2017-06-20 04:11:55 +02:00
|
|
|
// Clean up old trackers when refreshing:
|
2017-06-15 00:17:46 +02:00
|
|
|
this.tracker.stop()
|
2017-06-20 04:11:55 +02:00
|
|
|
this.tracker.removeListener('update', this.balanceUpdater)
|
|
|
|
this.tracker.removeListener('error', this.showError)
|
2017-06-15 00:17:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-14 02:47:56 +02:00
|
|
|
if (!global.ethereumProvider) return
|
2017-04-21 04:07:09 +02:00
|
|
|
const { userAddress } = this.props
|
|
|
|
this.tracker = new TokenTracker({
|
|
|
|
userAddress,
|
2017-06-14 02:47:56 +02:00
|
|
|
provider: global.ethereumProvider,
|
2017-08-16 18:49:23 +02:00
|
|
|
tokens: this.props.tokens,
|
2017-06-14 02:47:56 +02:00
|
|
|
pollingInterval: 8000,
|
2017-04-21 04:07:09 +02:00
|
|
|
})
|
|
|
|
|
2017-06-20 04:11:55 +02:00
|
|
|
|
|
|
|
// Set up listener instances for cleaning up
|
|
|
|
this.balanceUpdater = this.updateBalances.bind(this)
|
|
|
|
this.showError = (error) => {
|
|
|
|
this.setState({ error, isLoading: false })
|
|
|
|
}
|
|
|
|
this.tracker.on('update', this.balanceUpdater)
|
|
|
|
this.tracker.on('error', this.showError)
|
|
|
|
|
2017-04-21 04:07:09 +02:00
|
|
|
this.tracker.updateBalances()
|
2017-06-14 23:21:50 +02:00
|
|
|
.then(() => {
|
|
|
|
this.updateBalances(this.tracker.serialize())
|
|
|
|
})
|
|
|
|
.catch((reason) => {
|
|
|
|
log.error(`Problem updating balances`, reason)
|
|
|
|
this.setState({ isLoading: false })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-15 00:17:46 +02:00
|
|
|
TokenList.prototype.componentWillUpdate = function (nextProps) {
|
|
|
|
if (nextProps.network === 'loading') return
|
|
|
|
const oldNet = this.props.network
|
|
|
|
const newNet = nextProps.network
|
|
|
|
|
|
|
|
if (oldNet && newNet && newNet !== oldNet) {
|
|
|
|
this.setState({ isLoading: true })
|
|
|
|
this.createFreshTokenTracker()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 17:58:25 +02:00
|
|
|
TokenList.prototype.updateBalances = function (tokens) {
|
|
|
|
const heldTokens = tokens.filter(token => {
|
|
|
|
return token.balance !== '0' && token.string !== '0.000'
|
2017-06-16 01:22:53 +02:00
|
|
|
})
|
2017-06-14 23:21:50 +02:00
|
|
|
this.setState({ tokens: heldTokens, isLoading: false })
|
2017-04-21 04:07:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TokenList.prototype.componentWillUnmount = function () {
|
2017-06-14 02:47:56 +02:00
|
|
|
if (!this.tracker) return
|
2017-04-21 04:07:09 +02:00
|
|
|
this.tracker.stop()
|
|
|
|
}
|
2017-06-14 02:47:56 +02:00
|
|
|
|