Skip to content

Commit

Permalink
Refactor BalanceComponent to jsx (#6048)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextsg authored and whymarrh committed Jan 21, 2019
1 parent 0ad7797 commit fe780fb
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 250 deletions.
138 changes: 0 additions & 138 deletions ui/app/components/account-export.js

This file was deleted.

111 changes: 0 additions & 111 deletions ui/app/components/balance-component.js

This file was deleted.

92 changes: 92 additions & 0 deletions ui/app/components/balance/balance.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import TokenBalance from '../token-balance'
import Identicon from '../identicon'
import UserPreferencedCurrencyDisplay from '../user-preferenced-currency-display'
import { PRIMARY, SECONDARY } from '../../constants/common'
import { formatBalance } from '../../util'

export default class Balance extends PureComponent {
static propTypes = {
account: PropTypes.object,
assetImages: PropTypes.object,
nativeCurrency: PropTypes.string,
needsParse: PropTypes.bool,
network: PropTypes.string,
showFiat: PropTypes.bool,
token: PropTypes.object,
}

static defaultProps = {
needsParse: true,
showFiat: true,
}

renderBalance () {
const { account, nativeCurrency, needsParse, showFiat } = this.props
const balanceValue = account && account.balance
const formattedBalance = balanceValue
? formatBalance(balanceValue, 6, needsParse, nativeCurrency)
: '...'

if (formattedBalance === 'None' || formattedBalance === '...') {
return (
<div className="flex-column balance-display">
<div className="token-amount">
{ formattedBalance }
</div>
</div>
)
}

return (
<div className="flex-column balance-display">
<UserPreferencedCurrencyDisplay
className="token-amount"
value={balanceValue}
type={PRIMARY}
ethNumberOfDecimals={4}
/>
{
showFiat && (
<UserPreferencedCurrencyDisplay
value={balanceValue}
type={SECONDARY}
ethNumberOfDecimals={4}
/>
)
}
</div>
)
}

renderTokenBalance () {
const { token } = this.props

return (
<div className="flex-column balance-display">
<div className="token-amount">
<TokenBalance token={token} />
</div>
</div>
)
}

render () {
const { token, network, assetImages } = this.props
const address = token && token.address
const image = assetImages && address ? assetImages[token.address] : undefined

return (
<div className="balance-container">
<Identicon
diameter={50}
address={address}
network={network}
image={image}
/>
{ token ? this.renderTokenBalance() : this.renderBalance() }
</div>
)
}
}
27 changes: 27 additions & 0 deletions ui/app/components/balance/balance.container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { connect } from 'react-redux'
import Balance from './balance.component'
import {
getNativeCurrency,
getAssetImages,
conversionRateSelector,
getCurrentCurrency,
getMetaMaskAccounts,
} from '../../selectors'

const mapStateToProps = state => {
const accounts = getMetaMaskAccounts(state)
const network = state.metamask.network
const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0]
const account = accounts[selectedAddress]

return {
account,
network,
nativeCurrency: getNativeCurrency(state),
conversionRate: conversionRateSelector(state),
currentCurrency: getCurrentCurrency(state),
assetImages: getAssetImages(state),
}
}

export default connect(mapStateToProps)(Balance)
1 change: 1 addition & 0 deletions ui/app/components/balance/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './balance.container'
2 changes: 1 addition & 1 deletion ui/app/components/wallet-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Identicon from './identicon'
const Tooltip = require('./tooltip-v2.js').default
const copyToClipboard = require('copy-to-clipboard')
const actions = require('../actions')
const BalanceComponent = require('./balance-component')
import BalanceComponent from './balance'
const TokenList = require('./token-list')
const selectors = require('../selectors')
const { ADD_TOKEN_ROUTE } = require('../routes')
Expand Down

0 comments on commit fe780fb

Please sign in to comment.