Skip to content

Commit fe780fb

Browse files
alextsgwhymarrh
authored andcommitted
Refactor BalanceComponent to jsx (#6048)
1 parent 0ad7797 commit fe780fb

File tree

6 files changed

+121
-250
lines changed

6 files changed

+121
-250
lines changed

ui/app/components/account-export.js

-138
This file was deleted.

ui/app/components/balance-component.js

-111
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { connect } from 'react-redux'
2+
import Balance from './balance.component'
3+
import {
4+
getNativeCurrency,
5+
getAssetImages,
6+
conversionRateSelector,
7+
getCurrentCurrency,
8+
getMetaMaskAccounts,
9+
} from '../../selectors'
10+
11+
const mapStateToProps = state => {
12+
const accounts = getMetaMaskAccounts(state)
13+
const network = state.metamask.network
14+
const selectedAddress = state.metamask.selectedAddress || Object.keys(accounts)[0]
15+
const account = accounts[selectedAddress]
16+
17+
return {
18+
account,
19+
network,
20+
nativeCurrency: getNativeCurrency(state),
21+
conversionRate: conversionRateSelector(state),
22+
currentCurrency: getCurrentCurrency(state),
23+
assetImages: getAssetImages(state),
24+
}
25+
}
26+
27+
export default connect(mapStateToProps)(Balance)

ui/app/components/balance/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './balance.container'

ui/app/components/wallet-view.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Identicon from './identicon'
1212
const Tooltip = require('./tooltip-v2.js').default
1313
const copyToClipboard = require('copy-to-clipboard')
1414
const actions = require('../actions')
15-
const BalanceComponent = require('./balance-component')
15+
import BalanceComponent from './balance'
1616
const TokenList = require('./token-list')
1717
const selectors = require('../selectors')
1818
const { ADD_TOKEN_ROUTE } = require('../routes')

0 commit comments

Comments
 (0)