add currency switcher to ripple send tab

pull/448/head
slowbackspace 5 years ago
parent 93bea9d195
commit 40f61002b6

@ -6,6 +6,7 @@ import * as BLOCKCHAIN from 'actions/constants/blockchain';
import { initialState } from 'reducers/SendFormRippleReducer'; import { initialState } from 'reducers/SendFormRippleReducer';
import * as reducerUtils from 'reducers/utils'; import * as reducerUtils from 'reducers/utils';
import { fromDecimalAmount } from 'utils/formatUtils'; import { fromDecimalAmount } from 'utils/formatUtils';
import { toFiatCurrency, fromFiatCurrency } from 'utils/fiatConverter';
import type { import type {
Dispatch, Dispatch,
@ -109,6 +110,9 @@ export const init = (): AsyncAction => async (
initialState.selectedFeeLevel initialState.selectedFeeLevel
); );
// initial local currency is set according to wallet settings
const { localCurrency } = getState().wallet;
dispatch({ dispatch({
type: SEND.INIT, type: SEND.INIT,
networkType: 'ripple', networkType: 'ripple',
@ -116,6 +120,7 @@ export const init = (): AsyncAction => async (
...initialState, ...initialState,
networkName: network.shortcut, networkName: network.shortcut,
networkSymbol: network.symbol, networkSymbol: network.symbol,
localCurrency,
feeLevels, feeLevels,
selectedFeeLevel, selectedFeeLevel,
fee: network.fee.defaultFee, fee: network.fee.defaultFee,
@ -154,6 +159,9 @@ export const onClear = (): AsyncAction => async (
initialState.selectedFeeLevel initialState.selectedFeeLevel
); );
// initial local currency is set according to wallet settings
const { localCurrency } = getState().wallet;
dispatch({ dispatch({
type: SEND.CLEAR, type: SEND.CLEAR,
networkType: 'ripple', networkType: 'ripple',
@ -161,6 +169,7 @@ export const onClear = (): AsyncAction => async (
...initialState, ...initialState,
networkName: network.shortcut, networkName: network.shortcut,
networkSymbol: network.symbol, networkSymbol: network.symbol,
localCurrency,
feeLevels, feeLevels,
selectedFeeLevel, selectedFeeLevel,
fee: network.fee.defaultFee, fee: network.fee.defaultFee,
@ -193,10 +202,10 @@ export const onAddressChange = (address: string): ThunkAction => (
/* /*
* Called from UI on "amount" field change * Called from UI on "amount" field change
*/ */
export const onAmountChange = (amount: string): ThunkAction => ( export const onAmountChange = (
dispatch: Dispatch, amount: string,
getState: GetState shouldUpdateLocalAmount: boolean = true
): void => { ): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
const state = getState().sendFormRipple; const state = getState().sendFormRipple;
dispatch({ dispatch({
type: SEND.CHANGE, type: SEND.CHANGE,
@ -209,6 +218,67 @@ export const onAmountChange = (amount: string): ThunkAction => (
amount, amount,
}, },
}); });
if (shouldUpdateLocalAmount) {
const { localCurrency } = getState().sendFormRipple;
const fiatRates = getState().fiat.find(f => f.network === state.networkName);
const localAmount = toFiatCurrency(amount, localCurrency, fiatRates);
dispatch(onLocalAmountChange(localAmount, false));
}
};
/*
* Called from UI on "localAmount" field change
*/
export const onLocalAmountChange = (
localAmount: string,
shouldUpdateAmount: boolean = true
): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
const state = getState().sendFormRipple;
const { localCurrency } = getState().sendFormRipple;
const fiatRates = getState().fiat.find(f => f.network === state.networkName);
const { network } = getState().selectedAccount;
// updates localAmount
dispatch({
type: SEND.CHANGE,
networkType: 'ripple',
state: {
...state,
untouched: false,
touched: { ...state.touched, localAmount: true },
setMax: false,
localAmount,
},
});
// updates amount
if (shouldUpdateAmount) {
if (!network) return;
// converts amount in local currency to crypto currency that will be sent
const amount = fromFiatCurrency(localAmount, localCurrency, fiatRates, network.decimals);
dispatch(onAmountChange(amount, false));
}
};
/*
* Called from UI on "localCurrency" selection change
*/
export const onLocalCurrencyChange = (localCurrency: {
value: string,
label: string,
}): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
const state = getState().sendFormRipple;
dispatch({
type: SEND.CHANGE,
networkType: 'ripple',
state: {
...state,
localCurrency: localCurrency.value,
},
});
// Recalculates amount with new currency rates
dispatch(onLocalAmountChange(state.localAmount));
}; };
/* /*
@ -434,6 +504,8 @@ export default {
toggleAdvanced, toggleAdvanced,
onAddressChange, onAddressChange,
onAmountChange, onAmountChange,
onLocalAmountChange,
onLocalCurrencyChange,
onSetMax, onSetMax,
onFeeLevelChange, onFeeLevelChange,
updateFeeLevels, updateFeeLevels,

@ -4,6 +4,7 @@ import BigNumber from 'bignumber.js';
import * as SEND from 'actions/constants/send'; import * as SEND from 'actions/constants/send';
import { findDevice, getPendingAmount } from 'reducers/utils'; import { findDevice, getPendingAmount } from 'reducers/utils';
import { toDecimalAmount } from 'utils/formatUtils'; import { toDecimalAmount } from 'utils/formatUtils';
import { toFiatCurrency } from 'utils/fiatConverter';
import type { import type {
Dispatch, Dispatch,
@ -103,6 +104,12 @@ const recalculateTotalAmount = ($state: State): PayloadAction<State> => (
.minus(account.reserve) .minus(account.reserve)
.minus(pendingAmount); .minus(pendingAmount);
state.amount = calculateMaxAmount(availableBalance, fee); state.amount = calculateMaxAmount(availableBalance, fee);
// calculate amount in local currency
const { localCurrency } = getState().sendFormRipple;
const fiatRates = getState().fiat.find(f => f.network === state.networkName);
const localAmount = toFiatCurrency(state.amount, localCurrency, fiatRates);
state.localAmount = localAmount;
} }
state.total = calculateTotal(state.amount, fee); state.total = calculateTotal(state.amount, fee);

@ -16,11 +16,13 @@ export type State = {
+networkSymbol: string, +networkSymbol: string,
// form fields // form fields
localCurrency: string,
advanced: boolean, advanced: boolean,
untouched: boolean, // set to true when user made any changes in form untouched: boolean, // set to true when user made any changes in form
touched: { [k: string]: boolean }, touched: { [k: string]: boolean },
address: string, address: string,
amount: string, amount: string,
localAmount: string,
minAmount: string, minAmount: string,
setMax: boolean, setMax: boolean,
feeLevels: Array<FeeLevel>, feeLevels: Array<FeeLevel>,
@ -41,12 +43,14 @@ export type State = {
export const initialState: State = { export const initialState: State = {
networkName: '', networkName: '',
networkSymbol: '', networkSymbol: '',
localCurrency: '',
advanced: false, advanced: false,
untouched: true, untouched: true,
touched: {}, touched: {},
address: '', address: '',
amount: '', amount: '',
localAmount: '',
minAmount: '0', minAmount: '0',
setMax: false, setMax: false,
feeLevels: [], feeLevels: [],

@ -9,7 +9,8 @@ import Input from 'components/inputs/Input';
import Icon from 'components/Icon'; import Icon from 'components/Icon';
import Link from 'components/Link'; import Link from 'components/Link';
import ICONS from 'config/icons'; import ICONS from 'config/icons';
import { FONT_SIZE, FONT_WEIGHT, TRANSITION } from 'config/variables'; import { FONT_SIZE, FONT_WEIGHT, TRANSITION, SCREEN_SIZE } from 'config/variables';
import { FIAT_CURRENCIES } from 'config/app';
import colors from 'config/colors'; import colors from 'config/colors';
import Title from 'views/Wallet/components/Title'; import Title from 'views/Wallet/components/Title';
import P from 'components/Paragraph'; import P from 'components/Paragraph';
@ -185,6 +186,53 @@ const QrButton = styled(Button)`
padding: 0 10px; padding: 0 10px;
`; `;
const LocalAmountWrapper = styled.div`
display: flex;
align-self: flex-start;
margin-top: 26px;
@media screen and (max-width: ${SCREEN_SIZE.MD}) {
flex: 1 0 100%;
justify-content: flex-end;
margin-top: 0px;
padding-top: 28px;
}
`;
const AmountRow = styled(InputRow)`
display: flex;
align-items: flex-end;
padding-bottom: 28px;
@media screen and (max-width: ${SCREEN_SIZE.MD}) {
flex-wrap: wrap;
}
`;
const LocalAmountInput = styled(Input)`
@media screen and (max-width: ${SCREEN_SIZE.MD}) {
flex: 1 1 100%;
}
`;
const LocalCurrencySelect = styled(Select)`
min-width: 77px;
height: 40px;
@media screen and (max-width: ${SCREEN_SIZE.MD}) {
flex: 1 1 0;
}
`;
const EqualsSign = styled.div`
align-self: center;
padding: 0 10px;
font-size: ${FONT_SIZE.BIGGER};
@media screen and (max-width: ${SCREEN_SIZE.MD}) {
display: none;
}
`;
// render helpers // render helpers
const getAddressInputState = ( const getAddressInputState = (
address: string, address: string,
@ -215,6 +263,10 @@ const getAmountInputState = (amountErrors: string, amountWarnings: string): stri
return state; return state;
}; };
const buildCurrencyOption = currency => {
return { value: currency, label: currency.toUpperCase() };
};
// stateless component // stateless component
const AccountSend = (props: Props) => { const AccountSend = (props: Props) => {
const device = props.wallet.selectedDevice; const device = props.wallet.selectedDevice;
@ -222,6 +274,8 @@ const AccountSend = (props: Props) => {
const { const {
address, address,
amount, amount,
localAmount,
localCurrency,
setMax, setMax,
feeLevels, feeLevels,
selectedFeeLevel, selectedFeeLevel,
@ -238,6 +292,8 @@ const AccountSend = (props: Props) => {
toggleAdvanced, toggleAdvanced,
onAddressChange, onAddressChange,
onAmountChange, onAmountChange,
onLocalAmountChange,
onLocalCurrencyChange,
onSetMax, onSetMax,
onFeeLevelChange, onFeeLevelChange,
updateFeeLevels, updateFeeLevels,
@ -307,7 +363,7 @@ const AccountSend = (props: Props) => {
]} ]}
/> />
</InputRow> </InputRow>
<InputRow> <AmountRow>
<Input <Input
state={getAmountInputState(errors.amount, warnings.amount)} state={getAmountInputState(errors.amount, warnings.amount)}
autoComplete="off" autoComplete="off"
@ -350,7 +406,29 @@ const AccountSend = (props: Props) => {
/>, />,
]} ]}
/> />
</InputRow> <LocalAmountWrapper>
<EqualsSign>=</EqualsSign>
<LocalAmountInput
// state={getAmountInputState(errors.amount, warnings.amount)}
autoComplete="off"
autoCorrect="off"
autoCapitalize="off"
spellCheck="false"
value={localAmount}
onChange={event => onLocalAmountChange(event.target.value)}
sideAddons={[
<LocalCurrencySelect
key="local-currency"
isSearchable={false}
isClearable={false}
onChange={option => onLocalCurrencyChange(option)}
value={buildCurrencyOption(localCurrency)}
options={FIAT_CURRENCIES.map(c => buildCurrencyOption(c))}
/>,
]}
/>
</LocalAmountWrapper>
</AmountRow>
<InputRow> <InputRow>
<FeeLabelWrapper> <FeeLabelWrapper>

Loading…
Cancel
Save