Add Token UI - hover/select state; fetch token address

This commit is contained in:
Chi Kei Chan 2017-09-20 22:57:36 -07:00
parent 0204aa2001
commit 04da22db08
3 changed files with 82 additions and 19 deletions

View File

@ -1,5 +1,6 @@
const inherits = require('util').inherits const inherits = require('util').inherits
const Component = require('react').Component const Component = require('react').Component
const classnames = require('classnames')
const h = require('react-hyperscript') const h = require('react-hyperscript')
const connect = require('react-redux').connect const connect = require('react-redux').connect
const Fuse = require('fuse.js') const Fuse = require('fuse.js')
@ -7,14 +8,14 @@ const contractMap = require('eth-contract-metadata')
const contractList = Object.entries(contractMap).map(([ _, tokenData]) => tokenData) const contractList = Object.entries(contractMap).map(([ _, tokenData]) => tokenData)
const fuse = new Fuse(contractList, { const fuse = new Fuse(contractList, {
shouldSort: true, shouldSort: true,
threshold: 0.3, threshold: 0.45,
location: 0, location: 0,
distance: 100, distance: 100,
maxPatternLength: 32, maxPatternLength: 32,
minMatchCharLength: 1, minMatchCharLength: 1,
keys: ['address', 'name', 'symbol'], keys: ['address', 'name', 'symbol'],
}) })
// const actions = require('./actions') const actions = require('./actions')
// const Tooltip = require('./components/tooltip.js') // const Tooltip = require('./components/tooltip.js')
@ -25,7 +26,7 @@ const EthContract = require('ethjs-contract')
const emptyAddr = '0x0000000000000000000000000000000000000000' const emptyAddr = '0x0000000000000000000000000000000000000000'
module.exports = connect(mapStateToProps)(AddTokenScreen) module.exports = connect(mapStateToProps, mapDispatchToProps)(AddTokenScreen)
function mapStateToProps (state) { function mapStateToProps (state) {
return { return {
@ -33,6 +34,12 @@ function mapStateToProps (state) {
} }
} }
function mapDispatchToProps (dispatch) {
return {
goHome: () => dispatch(actions.goHome()),
}
}
inherits(AddTokenScreen, Component) inherits(AddTokenScreen, Component)
function AddTokenScreen () { function AddTokenScreen () {
this.state = { this.state = {
@ -40,33 +47,63 @@ function AddTokenScreen () {
// address: null, // address: null,
// symbol: 'TOKEN', // symbol: 'TOKEN',
// decimals: 18, // decimals: 18,
customAddress: '',
customSymbol: '',
customDecimals: 0,
searchQuery: '', searchQuery: '',
isCollapsed: true, isCollapsed: true,
selectedToken: {},
} }
this.tokenAddressDidChange = this.tokenAddressDidChange.bind(this)
Component.call(this) Component.call(this)
} }
AddTokenScreen.prototype.toggleToken = function (symbol) {
const { selectedToken } = this.state
const { [symbol]: isSelected } = selectedToken
this.setState({
selectedToken: {
...selectedToken,
[symbol]: !isSelected,
},
})
}
AddTokenScreen.prototype.renderCustomForm = function () { AddTokenScreen.prototype.renderCustomForm = function () {
const { customAddress, customSymbol, customDecimals } = this.state
return !this.state.isCollapsed && ( return !this.state.isCollapsed && (
h('div.add-token__add-custom-form', [ h('div.add-token__add-custom-form', [
h('div.add-token__add-custom-field', [ h('div.add-token__add-custom-field', [
h('div.add-token__add-custom-label', 'Token Address'), h('div.add-token__add-custom-label', 'Token Address'),
h('input.add-token__add-custom-input', { type: 'text' }), h('input.add-token__add-custom-input', {
type: 'text',
onChange: this.tokenAddressDidChange,
value: customAddress,
}),
]), ]),
h('div.add-token__add-custom-field', [ h('div.add-token__add-custom-field', [
h('div.add-token__add-custom-label', 'Token Symbol'), h('div.add-token__add-custom-label', 'Token Symbol'),
h('input.add-token__add-custom-input', { type: 'text', disabled: true }), h('input.add-token__add-custom-input', {
type: 'text',
value: customSymbol,
disabled: true,
}),
]), ]),
h('div.add-token__add-custom-field', [ h('div.add-token__add-custom-field', [
h('div.add-token__add-custom-label', 'Decimals of Precision'), h('div.add-token__add-custom-label', 'Decimals of Precision'),
h('input.add-token__add-custom-input', { type: 'text', disabled: true }), h('input.add-token__add-custom-input', {
type: 'number',
value: customDecimals,
disabled: true,
}),
]), ]),
]) ])
) )
} }
AddTokenScreen.prototype.renderTokenList = function () { AddTokenScreen.prototype.renderTokenList = function () {
const { searchQuery = '' } = this.state const { searchQuery = '', selectedToken } = this.state
const results = searchQuery const results = searchQuery
? fuse.search(searchQuery) || [] ? fuse.search(searchQuery) || []
: contractList : contractList
@ -74,9 +111,13 @@ AddTokenScreen.prototype.renderTokenList = function () {
return Array(6).fill(undefined) return Array(6).fill(undefined)
.map((_, i) => { .map((_, i) => {
const { logo, symbol, name } = results[i] || {} const { logo, symbol, name } = results[i] || {}
console.log({ i, logo, symbol, name })
return Boolean(logo || symbol || name) && ( return Boolean(logo || symbol || name) && (
h('div.add-token__token-wrapper', [ h('div.add-token__token-wrapper', {
className: classnames('add-token__token-wrapper', {
'add-token__token-wrapper--selected': selectedToken[symbol],
}),
onClick: () => this.toggleToken(symbol),
}, [
h('div.add-token__token-icon', { h('div.add-token__token-icon', {
style: { style: {
backgroundImage: `url(images/contract/${logo})`, backgroundImage: `url(images/contract/${logo})`,
@ -93,6 +134,7 @@ AddTokenScreen.prototype.renderTokenList = function () {
AddTokenScreen.prototype.render = function () { AddTokenScreen.prototype.render = function () {
const { isCollapsed } = this.state const { isCollapsed } = this.state
const { goHome } = this.props
return ( return (
h('div.add-token', [ h('div.add-token', [
@ -124,7 +166,9 @@ AddTokenScreen.prototype.render = function () {
]), ]),
h('div.add-token__buttons', [ h('div.add-token__buttons', [
h('button.btn-secondary', 'Next'), h('button.btn-secondary', 'Next'),
h('button.btn-tertiary', 'Cancel'), h('button.btn-tertiary', {
onClick: goHome,
}, 'Cancel'),
]), ]),
]) ])
) )
@ -270,12 +314,16 @@ AddTokenScreen.prototype.componentWillMount = function () {
this.TokenContract = this.contract(abi) this.TokenContract = this.contract(abi)
} }
AddTokenScreen.prototype.tokenAddressDidChange = function (event) { AddTokenScreen.prototype.tokenAddressDidChange = function (e) {
const el = event.target const customAddress = e.target.value.trim()
const address = el.value.trim() this.setState({ customAddress })
if (ethUtil.isValidAddress(address) && address !== emptyAddr) { if (ethUtil.isValidAddress(customAddress) && customAddress !== emptyAddr) {
this.setState({ address }) this.attemptToAutoFillTokenParams(customAddress)
this.attemptToAutoFillTokenParams(address) } else {
this.setState({
customSymbol: '',
customDecimals: 0,
})
} }
} }
@ -330,6 +378,9 @@ AddTokenScreen.prototype.attemptToAutoFillTokenParams = async function (address)
const [ symbol, decimals ] = results const [ symbol, decimals ] = results
if (symbol && decimals) { if (symbol && decimals) {
this.setState({ symbol: symbol[0], decimals: decimals[0].toString() }) this.setState({
customSymbol: symbol[0],
customDecimals: decimals[0].toString(),
})
} }
} }

View File

@ -140,11 +140,22 @@
} }
&__token-wrapper { &__token-wrapper {
transition: 200ms ease-in-out;
display: flex; display: flex;
flex-flow: row nowrap; flex-flow: row nowrap;
flex: 0 0 50%; flex: 0 0 45%;
align-items: center; align-items: center;
padding: 24px 0 24px 24px; padding: 12px;
margin: 2.5%;
box-sizing: border-box;
border-radius: 10px;
cursor: pointer;
border: 2px solid transparent;
&:hover,
&--selected {
border: 2px solid $malibu-blue;
}
} }
&__token-name { &__token-name {

View File

@ -38,6 +38,7 @@ $crimson: #e91550;
$blue-lagoon: #038789; $blue-lagoon: #038789;
$purple: #690496; $purple: #690496;
$tulip-tree: #ebb33f; $tulip-tree: #ebb33f;
$malibu-blue: #7ac9fd;
/* /*
Z-Indicies Z-Indicies