|
| 1 | +import React, { PureComponent } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import TokenBalance from '../token-balance' |
| 4 | +import Identicon from '../identicon' |
| 5 | +import UserPreferencedCurrencyDisplay from '../user-preferenced-currency-display' |
| 6 | +import { PRIMARY, SECONDARY } from '../../constants/common' |
| 7 | +import { formatBalance } from '../../util' |
| 8 | + |
| 9 | +export default class Balance extends PureComponent { |
| 10 | + static propTypes = { |
| 11 | + account: PropTypes.object, |
| 12 | + assetImages: PropTypes.object, |
| 13 | + nativeCurrency: PropTypes.string, |
| 14 | + needsParse: PropTypes.bool, |
| 15 | + network: PropTypes.string, |
| 16 | + showFiat: PropTypes.bool, |
| 17 | + token: PropTypes.object, |
| 18 | + } |
| 19 | + |
| 20 | + static defaultProps = { |
| 21 | + needsParse: true, |
| 22 | + showFiat: true, |
| 23 | + } |
| 24 | + |
| 25 | + renderBalance () { |
| 26 | + const { account, nativeCurrency, needsParse, showFiat } = this.props |
| 27 | + const balanceValue = account && account.balance |
| 28 | + const formattedBalance = balanceValue |
| 29 | + ? formatBalance(balanceValue, 6, needsParse, nativeCurrency) |
| 30 | + : '...' |
| 31 | + |
| 32 | + if (formattedBalance === 'None' || formattedBalance === '...') { |
| 33 | + return ( |
| 34 | + <div className="flex-column balance-display"> |
| 35 | + <div className="token-amount"> |
| 36 | + { formattedBalance } |
| 37 | + </div> |
| 38 | + </div> |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + return ( |
| 43 | + <div className="flex-column balance-display"> |
| 44 | + <UserPreferencedCurrencyDisplay |
| 45 | + className="token-amount" |
| 46 | + value={balanceValue} |
| 47 | + type={PRIMARY} |
| 48 | + ethNumberOfDecimals={4} |
| 49 | + /> |
| 50 | + { |
| 51 | + showFiat && ( |
| 52 | + <UserPreferencedCurrencyDisplay |
| 53 | + value={balanceValue} |
| 54 | + type={SECONDARY} |
| 55 | + ethNumberOfDecimals={4} |
| 56 | + /> |
| 57 | + ) |
| 58 | + } |
| 59 | + </div> |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + renderTokenBalance () { |
| 64 | + const { token } = this.props |
| 65 | + |
| 66 | + return ( |
| 67 | + <div className="flex-column balance-display"> |
| 68 | + <div className="token-amount"> |
| 69 | + <TokenBalance token={token} /> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + render () { |
| 76 | + const { token, network, assetImages } = this.props |
| 77 | + const address = token && token.address |
| 78 | + const image = assetImages && address ? assetImages[token.address] : undefined |
| 79 | + |
| 80 | + return ( |
| 81 | + <div className="balance-container"> |
| 82 | + <Identicon |
| 83 | + diameter={50} |
| 84 | + address={address} |
| 85 | + network={network} |
| 86 | + image={image} |
| 87 | + /> |
| 88 | + { token ? this.renderTokenBalance() : this.renderBalance() } |
| 89 | + </div> |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
0 commit comments