Skip to content
Permalink
Browse files

fix(ui): ensure fiat and crypto inputs in agroup are always updated

fix  #2602
  • Loading branch information...
korhaliv authored and mrfelton committed Jul 26, 2019
1 parent 7d748b5 commit 01ecaec12cb44b7019c42c2ad794ae6773b84ef7
Showing with 23 additions and 9 deletions.
  1. +23 −9 renderer/components/UI/CurrencyFieldGroup.js
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useRef } from 'react'
import PropTypes from 'prop-types'
import { Box, Flex } from 'rebass'
import { FormattedMessage } from 'react-intl'
@@ -34,26 +34,40 @@ const CurrencyFieldGroup = React.forwardRef(
},
ref
) => {
const shouldUpdate = useRef(true)

/**
* handleAmountCryptoChange - Set the amountFiat field whenever the crypto amount changes.
*
* @param {Event} e Event
*/
const handleAmountCryptoChange = e => {
const handleAmountCryptoChange = async value => {
const lastPrice = currentTicker[fiatCurrency]
const value = convert(cryptoUnit, 'fiat', e.target.value, lastPrice)
formApi.setValue('amountFiat', value)
const fiatValue = convert(cryptoUnit, 'fiat', value, lastPrice)
const upd = shouldUpdate.current
shouldUpdate.current = false
if (upd) {
formApi.setValue('amountFiat', fiatValue)
}
await Promise.resolve()
shouldUpdate.current = true
}

/**
* handleAmountFiatChange - Set the amountCrypto field whenever the fiat amount changes.
*
* @param {Event} e Event
*/
const handleAmountFiatChange = e => {
const handleAmountFiatChange = async value => {
const lastPrice = currentTicker[fiatCurrency]
const value = convert('fiat', cryptoUnit, e.target.value, lastPrice)
formApi.setValue('amountCrypto', value)
const cryptoValue = convert('fiat', cryptoUnit, value, lastPrice)
const upd = shouldUpdate.current
shouldUpdate.current = false
if (upd) {
formApi.setValue('amountCrypto', cryptoValue)
}
await Promise.resolve()
shouldUpdate.current = true
}

/**
@@ -88,7 +102,7 @@ const CurrencyFieldGroup = React.forwardRef(
isRequired={isRequired}
label={cryptoLabel || <FormattedMessage {...messages.amount} />}
name="amountCrypto"
onChange={handleAmountCryptoChange}
onValueChange={handleAmountCryptoChange}
validate={validate}
validateOnBlur={validateOnBlur}
validateOnChange={validateOnChange}
@@ -117,7 +131,7 @@ const CurrencyFieldGroup = React.forwardRef(
isDisabled={isDisabled}
label={fiatLabel || <Span>&nbsp;</Span>}
name="amountFiat"
onChange={handleAmountFiatChange}
onValueChange={handleAmountFiatChange}
width={150}
/>
</Box>

0 comments on commit 01ecaec

Please sign in to comment.
You can’t perform that action at this time.