2017-10-09 18:25:23 +02:00
|
|
|
const Component = require('react').Component
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
2018-06-20 17:48:23 +02:00
|
|
|
const { conversionUtil, multiplyCurrencies } = require('../../../conversion-util')
|
|
|
|
const { removeLeadingZeroes } = require('../send.utils')
|
2018-04-09 18:44:09 +02:00
|
|
|
const currencyFormatter = require('currency-formatter')
|
2018-04-09 21:24:52 +02:00
|
|
|
const currencies = require('currency-formatter/currencies')
|
2018-05-22 10:10:06 +02:00
|
|
|
const ethUtil = require('ethereumjs-util')
|
2017-10-09 18:25:23 +02:00
|
|
|
|
|
|
|
module.exports = CurrencyDisplay
|
|
|
|
|
|
|
|
inherits(CurrencyDisplay, Component)
|
|
|
|
function CurrencyDisplay () {
|
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:13:20 +02:00
|
|
|
function toHexWei (value) {
|
|
|
|
return conversionUtil(value, {
|
2017-10-13 22:19:22 +02:00
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:41:23 +02:00
|
|
|
CurrencyDisplay.prototype.componentWillMount = function () {
|
|
|
|
this.setState({
|
|
|
|
valueToRender: this.getValueToRender(this.props),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrencyDisplay.prototype.componentWillReceiveProps = function (nextProps) {
|
|
|
|
const currentValueToRender = this.getValueToRender(this.props)
|
|
|
|
const newValueToRender = this.getValueToRender(nextProps)
|
|
|
|
if (currentValueToRender !== newValueToRender) {
|
|
|
|
this.setState({
|
|
|
|
valueToRender: newValueToRender,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 03:26:18 +02:00
|
|
|
CurrencyDisplay.prototype.getAmount = function (value) {
|
|
|
|
const { selectedToken } = this.props
|
|
|
|
const { decimals } = selectedToken || {}
|
|
|
|
const multiplier = Math.pow(10, Number(decimals || 0))
|
2017-10-21 03:57:59 +02:00
|
|
|
|
2018-05-28 18:41:23 +02:00
|
|
|
const sendAmount = multiplyCurrencies(value || '0', multiplier, {toNumericBase: 'hex'})
|
2017-10-21 03:57:59 +02:00
|
|
|
|
2017-10-21 03:26:18 +02:00
|
|
|
return selectedToken
|
|
|
|
? sendAmount
|
|
|
|
: toHexWei(value)
|
|
|
|
}
|
|
|
|
|
2018-06-02 06:28:01 +02:00
|
|
|
CurrencyDisplay.prototype.getValueToRender = function ({ selectedToken, conversionRate, value, readOnly }) {
|
|
|
|
if (value === '0x0') return readOnly ? '0' : ''
|
2017-11-08 02:17:14 +01:00
|
|
|
const { decimals, symbol } = selectedToken || {}
|
|
|
|
const multiplier = Math.pow(10, Number(decimals || 0))
|
|
|
|
|
|
|
|
return selectedToken
|
2018-05-22 10:10:06 +02:00
|
|
|
? conversionUtil(ethUtil.addHexPrefix(value), {
|
2017-11-08 02:17:14 +01:00
|
|
|
fromNumericBase: 'hex',
|
2018-06-18 18:19:21 +02:00
|
|
|
toNumericBase: 'dec',
|
2017-11-08 02:17:14 +01:00
|
|
|
toCurrency: symbol,
|
|
|
|
conversionRate: multiplier,
|
|
|
|
invertConversionRate: true,
|
|
|
|
})
|
2018-05-22 10:10:06 +02:00
|
|
|
: conversionUtil(ethUtil.addHexPrefix(value), {
|
2017-11-08 02:17:14 +01:00
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
2018-05-22 17:27:15 +02:00
|
|
|
numberOfDecimals: 9,
|
2017-11-08 02:17:14 +01:00
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-09 18:44:09 +02:00
|
|
|
CurrencyDisplay.prototype.getConvertedValueToRender = function (nonFormattedValue) {
|
|
|
|
const { primaryCurrency, convertedCurrency, conversionRate } = this.props
|
|
|
|
|
2018-07-13 22:11:43 +02:00
|
|
|
if (conversionRate == 0 || conversionRate == null || converstionRate == undefined && nonFormattedValue != 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2018-04-09 18:44:09 +02:00
|
|
|
let convertedValue = conversionUtil(nonFormattedValue, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromCurrency: primaryCurrency,
|
|
|
|
toCurrency: convertedCurrency,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
|
2018-07-13 22:11:43 +02:00
|
|
|
convertedValue == Number(convertedValue).toFixed(2)
|
|
|
|
const upperCaseCurrencyCode = convertedCurrency.toUpperCase()
|
|
|
|
return currencies.find(currency => currency.code === upperCaseCurrencyCode)
|
|
|
|
? currencyFormatter.format(Number(convertedValue), {
|
|
|
|
code: upperCaseCurrencyCode,
|
|
|
|
})
|
2018-07-11 21:00:14 +02:00
|
|
|
: convertedValue
|
|
|
|
}
|
2018-04-09 18:44:09 +02:00
|
|
|
|
2018-05-28 18:41:23 +02:00
|
|
|
CurrencyDisplay.prototype.handleChange = function (newVal) {
|
2018-06-18 18:20:21 +02:00
|
|
|
this.setState({ valueToRender: removeLeadingZeroes(newVal) })
|
2018-05-28 18:41:23 +02:00
|
|
|
this.props.onChange(this.getAmount(newVal))
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrencyDisplay.prototype.getInputWidth = function (valueToRender, readOnly) {
|
|
|
|
const valueString = String(valueToRender)
|
|
|
|
const valueLength = valueString.length || 1
|
2018-05-31 16:53:12 +02:00
|
|
|
const decimalPointDeficit = valueString.match(/\./) ? -0.5 : 0
|
|
|
|
return (valueLength + decimalPointDeficit + 0.75) + 'ch'
|
2018-05-28 18:41:23 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 22:11:43 +02:00
|
|
|
CurrencyDisplay.prototype.onlyRenderConversions = function (convertedValueToRender) {
|
|
|
|
const{
|
|
|
|
convertedBalanceClassName = 'currency-display__converted-value',
|
|
|
|
convertedCurrency,
|
|
|
|
} = this.props
|
|
|
|
|
|
|
|
return h('div', {
|
|
|
|
className: convertedBalanceClassName,
|
|
|
|
}, convertedValueToRender == null
|
|
|
|
? 'No Conversion Rate'
|
|
|
|
: `${convertedValueToRender} ${convertedCurrency.toUpperCase()}`
|
|
|
|
)
|
|
|
|
}
|
2018-07-11 21:00:14 +02:00
|
|
|
|
2017-10-09 18:25:23 +02:00
|
|
|
CurrencyDisplay.prototype.render = function () {
|
|
|
|
const {
|
2017-10-13 22:19:22 +02:00
|
|
|
className = 'currency-display',
|
|
|
|
primaryBalanceClassName = 'currency-display__input',
|
|
|
|
convertedBalanceClassName = 'currency-display__converted-value',
|
2017-10-09 18:25:23 +02:00
|
|
|
primaryCurrency,
|
|
|
|
convertedCurrency,
|
|
|
|
readOnly = false,
|
2017-10-17 22:13:20 +02:00
|
|
|
inError = false,
|
2018-05-28 18:41:23 +02:00
|
|
|
onBlur,
|
2018-06-25 14:24:49 +02:00
|
|
|
step,
|
2017-10-09 18:25:23 +02:00
|
|
|
} = this.props
|
2018-05-28 18:41:23 +02:00
|
|
|
const { valueToRender } = this.state
|
2017-10-13 22:19:22 +02:00
|
|
|
|
2018-04-09 18:44:09 +02:00
|
|
|
const convertedValueToRender = this.getConvertedValueToRender(valueToRender)
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2017-10-13 22:19:22 +02:00
|
|
|
return h('div', {
|
2017-10-09 18:25:23 +02:00
|
|
|
className,
|
2017-10-17 22:13:20 +02:00
|
|
|
style: {
|
|
|
|
borderColor: inError ? 'red' : null,
|
|
|
|
},
|
2018-05-29 20:54:44 +02:00
|
|
|
onClick: () => {
|
|
|
|
this.currencyInput && this.currencyInput.focus()
|
|
|
|
},
|
2017-10-09 18:25:23 +02:00
|
|
|
}, [
|
|
|
|
|
|
|
|
h('div.currency-display__primary-row', [
|
|
|
|
|
2017-10-11 15:18:27 +02:00
|
|
|
h('div.currency-display__input-wrapper', [
|
|
|
|
|
2018-05-29 20:54:44 +02:00
|
|
|
h('input', {
|
2017-10-13 22:19:22 +02:00
|
|
|
className: primaryBalanceClassName,
|
2017-11-08 00:36:26 +01:00
|
|
|
value: `${valueToRender}`,
|
2017-10-23 14:05:53 +02:00
|
|
|
placeholder: '0',
|
2018-05-29 20:54:44 +02:00
|
|
|
type: 'number',
|
2017-10-11 15:18:27 +02:00
|
|
|
readOnly,
|
2018-05-14 15:30:50 +02:00
|
|
|
...(!readOnly ? {
|
2018-05-29 20:54:44 +02:00
|
|
|
onChange: e => this.handleChange(e.target.value),
|
2018-05-28 18:41:23 +02:00
|
|
|
onBlur: () => onBlur(this.getAmount(valueToRender)),
|
2018-05-14 15:30:50 +02:00
|
|
|
} : {}),
|
2018-05-29 20:54:44 +02:00
|
|
|
ref: input => { this.currencyInput = input },
|
|
|
|
style: {
|
2018-06-22 18:13:39 +02:00
|
|
|
width: this.getInputWidth(valueToRender, readOnly),
|
2018-05-29 20:54:44 +02:00
|
|
|
},
|
|
|
|
min: 0,
|
2018-06-25 14:24:49 +02:00
|
|
|
step,
|
2017-10-11 15:18:27 +02:00
|
|
|
}),
|
|
|
|
|
2017-10-23 14:05:53 +02:00
|
|
|
h('span.currency-display__currency-symbol', primaryCurrency),
|
|
|
|
|
2017-10-11 15:18:27 +02:00
|
|
|
]),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
2018-07-13 22:11:43 +02:00
|
|
|
]), this.onlyRenderConversions(convertedValueToRender),
|
2017-10-09 18:25:23 +02:00
|
|
|
|
|
|
|
])
|
2017-11-02 13:15:59 +01:00
|
|
|
|
2017-10-09 18:25:23 +02:00
|
|
|
}
|
|
|
|
|