1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-28 05:12:18 +01:00
metamask-extension/ui/app/components/balance-component.js

96 lines
2.8 KiB
JavaScript
Raw Normal View History

const Component = require('react').Component
const connect = require('react-redux').connect
const h = require('react-hyperscript')
const inherits = require('util').inherits
const { formatBalance, generateBalanceObject } = require('../util')
module.exports = connect(mapStateToProps)(BalanceComponent)
function mapStateToProps (state) {
return {
conversionRate: state.metamask.conversionRate,
currentCurrency: state.metamask.currentCurrency,
}
}
inherits(BalanceComponent, Component)
function BalanceComponent () {
Component.call(this)
}
BalanceComponent.prototype.render = function () {
const props = this.props
const { balanceValue } = props
const needsParse = 'needsParse' in props ? props.needsParse : true
const formattedBalance = balanceValue ? formatBalance(balanceValue, 6, needsParse) : '...'
return h('div.balance-container', {}, [
// TODO: balance icon needs to be passed in
h('img.balance-icon', {
src: '../images/eth_logo.svg',
style: {},
}),
this.renderBalance(formattedBalance),
])
}
BalanceComponent.prototype.renderBalance = function (formattedBalance) {
const props = this.props
const { shorten } = props
const showFiat = 'showFiat' in props ? props.showFiat : true
if (formattedBalance === 'None' || formattedBalance === '...') {
return h('div.flex-column.balance-display', {}, [
h('div.token-amount', {
style: {},
}, formattedBalance),
])
}
return h('div.flex-column.balance-display', {}, [
h('div.token-amount', {
style: {},
2017-08-08 14:15:30 +02:00
}, this.getTokenBalance(formattedBalance, shorten)),
showFiat ? this.renderFiatValue(formattedBalance) : null,
])
}
BalanceComponent.prototype.renderFiatValue = function (formattedBalance) {
const { conversionRate, currentCurrency } = this.props
2017-08-08 14:15:30 +02:00
const fiatDisplayNumber = this.getFiatDisplayNumber(formattedBalance, conversionRate)
2017-08-08 14:15:30 +02:00
return this.renderFiatAmount(fiatDisplayNumber, currentCurrency)
}
BalanceComponent.prototype.renderFiatAmount = function (fiatDisplayNumber, fiatSuffix) {
if (fiatDisplayNumber === 'N/A') return null
2017-08-08 14:15:30 +02:00
return h('div.fiat-amount', {
style: {},
}, `${fiatDisplayNumber} ${fiatSuffix}`)
}
2017-08-08 14:15:30 +02:00
BalanceComponent.prototype.getTokenBalance = function (formattedBalance, shorten) {
const balanceObj = generateBalanceObject(formattedBalance, shorten ? 1 : 3)
const balanceValue = shorten ? balanceObj.shortBalance : balanceObj.balance
const label = balanceObj.label
return `${balanceValue} ${label}`
}
BalanceComponent.prototype.getFiatDisplayNumber = function (formattedBalance, conversionRate) {
if (formattedBalance === 'None') return formattedBalance
if (conversionRate === 0) return 'N/A'
const splitBalance = formattedBalance.split(' ')
return (Number(splitBalance[0]) * conversionRate).toFixed(2)
}