|
@ -4,18 +4,23 @@ import React, { PureComponent } from 'react' |
|
|
import { compose } from 'redux' |
|
|
import { compose } from 'redux' |
|
|
import { translate } from 'react-i18next' |
|
|
import { translate } from 'react-i18next' |
|
|
import styled from 'styled-components' |
|
|
import styled from 'styled-components' |
|
|
import { createStructuredSelector } from 'reselect' |
|
|
|
|
|
import { connect } from 'react-redux' |
|
|
import { connect } from 'react-redux' |
|
|
import type { Currency, Account } from '@ledgerhq/live-common/lib/types' |
|
|
import type { Currency, Account } from '@ledgerhq/live-common/lib/types' |
|
|
|
|
|
|
|
|
import type { T } from 'types/common' |
|
|
import type { T } from 'types/common' |
|
|
|
|
|
|
|
|
import { counterValueCurrencySelector, exchangeSettingsForAccountSelector } from 'reducers/settings' |
|
|
import { |
|
|
import { calculateCounterValueSelector, reverseCounterValueSelector } from 'reducers/counterValues' |
|
|
counterValueCurrencySelector, |
|
|
|
|
|
currencySettingsSelector, |
|
|
|
|
|
counterValueExchangeSelector, |
|
|
|
|
|
intermediaryCurrency, |
|
|
|
|
|
} from 'reducers/settings' |
|
|
|
|
|
import CounterValues from 'helpers/countervalues' |
|
|
|
|
|
|
|
|
import InputCurrency from 'components/base/InputCurrency' |
|
|
import InputCurrency from 'components/base/InputCurrency' |
|
|
import Button from 'components/base/Button' |
|
|
import Button from 'components/base/Button' |
|
|
import Box from 'components/base/Box' |
|
|
import Box from 'components/base/Box' |
|
|
|
|
|
import type { State } from 'reducers' |
|
|
|
|
|
|
|
|
const InputRight = styled(Box).attrs({ |
|
|
const InputRight = styled(Box).attrs({ |
|
|
ff: 'Rubik', |
|
|
ff: 'Rubik', |
|
@ -35,15 +40,7 @@ const InputCenter = styled(Box).attrs({ |
|
|
width: 30px; |
|
|
width: 30px; |
|
|
` |
|
|
` |
|
|
|
|
|
|
|
|
const mapStateToProps = createStructuredSelector({ |
|
|
type OwnProps = { |
|
|
exchange: exchangeSettingsForAccountSelector, |
|
|
|
|
|
// $FlowFixMe
|
|
|
|
|
|
rightCurrency: counterValueCurrencySelector, |
|
|
|
|
|
getCounterValue: calculateCounterValueSelector, |
|
|
|
|
|
getReverseCounterValue: reverseCounterValueSelector, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
type Props = { |
|
|
|
|
|
// translation
|
|
|
// translation
|
|
|
t: T, |
|
|
t: T, |
|
|
|
|
|
|
|
@ -58,21 +55,59 @@ type Props = { |
|
|
// change handler
|
|
|
// change handler
|
|
|
onChange: number => void, |
|
|
onChange: number => void, |
|
|
|
|
|
|
|
|
exchange: string, |
|
|
|
|
|
|
|
|
|
|
|
// used to determine the left input unit
|
|
|
// used to determine the left input unit
|
|
|
account: Account, |
|
|
account: Account, |
|
|
|
|
|
|
|
|
|
|
|
// display max button
|
|
|
|
|
|
withMax: boolean, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Props = OwnProps & { |
|
|
// used to determine the right input unit
|
|
|
// used to determine the right input unit
|
|
|
// retrieved via selector (take the chosen countervalue unit)
|
|
|
// retrieved via selector (take the chosen countervalue unit)
|
|
|
rightCurrency: Currency, |
|
|
rightCurrency: Currency, |
|
|
|
|
|
|
|
|
// used to calculate the opposite field value (right & left)
|
|
|
// used to calculate the opposite field value (right & left)
|
|
|
getCounterValue: *, |
|
|
getCounterValue: number => ?number, |
|
|
getReverseCounterValue: *, |
|
|
getReverseCounterValue: number => ?number, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// display max button
|
|
|
const mapStateToProps = (state: State, props: OwnProps) => { |
|
|
withMax: boolean, |
|
|
const { |
|
|
|
|
|
account: { currency }, |
|
|
|
|
|
} = props |
|
|
|
|
|
const counterValueCurrency = counterValueCurrencySelector(state) |
|
|
|
|
|
const fromExchange = currencySettingsSelector(state, { currency }).exchange |
|
|
|
|
|
const toExchange = counterValueExchangeSelector(state) |
|
|
|
|
|
const getCounterValue = value => |
|
|
|
|
|
fromExchange && |
|
|
|
|
|
toExchange && |
|
|
|
|
|
CounterValues.calculateWithIntermediarySelector(state, { |
|
|
|
|
|
from: currency, |
|
|
|
|
|
fromExchange, |
|
|
|
|
|
intermediary: intermediaryCurrency, |
|
|
|
|
|
toExchange, |
|
|
|
|
|
to: counterValueCurrency, |
|
|
|
|
|
value, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const getReverseCounterValue = value => |
|
|
|
|
|
fromExchange && |
|
|
|
|
|
toExchange && |
|
|
|
|
|
CounterValues.reverseWithIntermediarySelector(state, { |
|
|
|
|
|
from: currency, |
|
|
|
|
|
fromExchange, |
|
|
|
|
|
intermediary: intermediaryCurrency, |
|
|
|
|
|
toExchange, |
|
|
|
|
|
to: counterValueCurrency, |
|
|
|
|
|
value, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
rightCurrency: counterValueCurrency, |
|
|
|
|
|
getCounterValue, |
|
|
|
|
|
getReverseCounterValue, |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export class RequestAmount extends PureComponent<Props> { |
|
|
export class RequestAmount extends PureComponent<Props> { |
|
@ -90,18 +125,18 @@ export class RequestAmount extends PureComponent<Props> { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
handleChangeAmount = (changedField: string) => (val: number) => { |
|
|
handleChangeAmount = (changedField: string) => (val: number) => { |
|
|
const { rightCurrency, getReverseCounterValue, account, max, onChange, exchange } = this.props |
|
|
const { getReverseCounterValue, max, onChange } = this.props |
|
|
if (changedField === 'left') { |
|
|
if (changedField === 'left') { |
|
|
onChange(val > max ? max : val) |
|
|
onChange(val > max ? max : val) |
|
|
} else if (changedField === 'right') { |
|
|
} else if (changedField === 'right') { |
|
|
const leftVal = getReverseCounterValue(account.currency, rightCurrency, exchange)(val) || 0 |
|
|
const leftVal = getReverseCounterValue(val) || 0 |
|
|
onChange(leftVal > max ? max : leftVal) |
|
|
onChange(leftVal > max ? max : leftVal) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
renderInputs(containerProps: Object) { |
|
|
renderInputs(containerProps: Object) { |
|
|
const { value, account, rightCurrency, getCounterValue, exchange, canBeSpent } = this.props |
|
|
const { value, account, rightCurrency, getCounterValue, canBeSpent } = this.props |
|
|
const right = getCounterValue(account.currency, rightCurrency, exchange)(value) || 0 |
|
|
const right = getCounterValue(value) || 0 |
|
|
const rightUnit = rightCurrency.units[0] |
|
|
const rightUnit = rightCurrency.units[0] |
|
|
return ( |
|
|
return ( |
|
|
<Box horizontal grow shrink> |
|
|
<Box horizontal grow shrink> |
|
|