@ -1,13 +1,18 @@
import React , { PureComponent } from 'react'
import React , { PureComponent } from 'react'
import PropTypes from 'prop-types'
import PropTypes from 'prop-types'
import log from 'loglevel'
import Modal from '../../modal'
import Modal from '../../modal'
import Identicon from '../../../ui/identicon'
import Identicon from '../../../ui/identicon'
import TextField from '../../../ui/text-field'
import TextField from '../../../ui/text-field'
import { calcTokenAmount } from '../../../../helpers/utils/token-util'
import classnames from 'classnames'
import classnames from 'classnames'
import BigNumber from 'bignumber.js'
import BigNumber from 'bignumber.js'
const MAX _UNSIGNED _256 _INT = new BigNumber ( 2 ) . pow ( 256 ) . minus ( 1 ) . toString ( 10 )
export default class EditApprovalPermission extends PureComponent {
export default class EditApprovalPermission extends PureComponent {
static propTypes = {
static propTypes = {
decimals : PropTypes . number ,
hideModal : PropTypes . func . isRequired ,
hideModal : PropTypes . func . isRequired ,
selectedIdentity : PropTypes . object ,
selectedIdentity : PropTypes . object ,
tokenAmount : PropTypes . string ,
tokenAmount : PropTypes . string ,
@ -15,7 +20,7 @@ export default class EditApprovalPermission extends PureComponent {
tokenSymbol : PropTypes . string ,
tokenSymbol : PropTypes . string ,
tokenBalance : PropTypes . string ,
tokenBalance : PropTypes . string ,
setCustomAmount : PropTypes . func ,
setCustomAmount : PropTypes . func ,
origin : PropTypes . string ,
origin : PropTypes . string . isRequired ,
}
}
static contextTypes = {
static contextTypes = {
@ -27,7 +32,7 @@ export default class EditApprovalPermission extends PureComponent {
selectedOptionIsUnlimited : ! this . props . customTokenAmount ,
selectedOptionIsUnlimited : ! this . props . customTokenAmount ,
}
}
renderModalContent ( ) {
renderModalContent ( error ) {
const { t } = this . context
const { t } = this . context
const {
const {
hideModal ,
hideModal ,
@ -136,7 +141,6 @@ export default class EditApprovalPermission extends PureComponent {
< div className = "edit-approval-permission__edit-section__option-input" >
< div className = "edit-approval-permission__edit-section__option-input" >
< TextField
< TextField
type = "number"
type = "number"
min = "0"
placeholder = { ` ${ Number ( customTokenAmount || tokenAmount ) } ${ tokenSymbol } ` }
placeholder = { ` ${ Number ( customTokenAmount || tokenAmount ) } ${ tokenSymbol } ` }
onChange = { ( event ) => {
onChange = { ( event ) => {
this . setState ( { customSpendLimit : event . target . value } )
this . setState ( { customSpendLimit : event . target . value } )
@ -147,6 +151,7 @@ export default class EditApprovalPermission extends PureComponent {
fullWidth
fullWidth
margin = "dense"
margin = "dense"
value = { this . state . customSpendLimit }
value = { this . state . customSpendLimit }
error = { error }
/ >
/ >
< / d i v >
< / d i v >
< / d i v >
< / d i v >
@ -156,10 +161,44 @@ export default class EditApprovalPermission extends PureComponent {
)
)
}
}
validateSpendLimit ( ) {
const { t } = this . context
const { decimals } = this . props
const { selectedOptionIsUnlimited , customSpendLimit } = this . state
if ( selectedOptionIsUnlimited || ! customSpendLimit ) {
return
}
let customSpendLimitNumber
try {
customSpendLimitNumber = new BigNumber ( customSpendLimit )
} catch ( error ) {
log . debug ( ` Error converting ' ${ customSpendLimit } ' to BigNumber: ` , error )
return t ( 'spendLimitInvalid' )
}
if ( customSpendLimitNumber . isNegative ( ) ) {
return t ( 'spendLimitInvalid' )
}
const maxTokenAmount = calcTokenAmount ( MAX _UNSIGNED _256 _INT , decimals )
if ( customSpendLimitNumber . greaterThan ( maxTokenAmount ) ) {
return t ( 'spendLimitTooLarge' )
}
}
render ( ) {
render ( ) {
const { t } = this . context
const { t } = this . context
const { setCustomAmount , hideModal , customTokenAmount } = this . props
const { setCustomAmount , hideModal , customTokenAmount } = this . props
const { selectedOptionIsUnlimited , customSpendLimit } = this . state
const { selectedOptionIsUnlimited , customSpendLimit } = this . state
const error = this . validateSpendLimit ( )
const disabled = Boolean (
( customSpendLimit === customTokenAmount && ! selectedOptionIsUnlimited ) ||
error
)
return (
return (
< Modal
< Modal
onSubmit = { ( ) => {
onSubmit = { ( ) => {
@ -170,9 +209,9 @@ export default class EditApprovalPermission extends PureComponent {
submitType = "primary"
submitType = "primary"
contentClass = "edit-approval-permission-modal-content"
contentClass = "edit-approval-permission-modal-content"
containerClass = "edit-approval-permission-modal-container"
containerClass = "edit-approval-permission-modal-container"
submitDisabled = { ( customSpendLimit === customTokenAmount ) && ! selectedOptionIsUnlimited }
submitDisabled = { disabled }
>
>
{ this . renderModalContent ( ) }
{ this . renderModalContent ( error ) }
< / M o d a l >
< / M o d a l >
)
)
}
}