Skip to content

Commit

Permalink
♻️ Move getAmountFeedbackAndError to FormBase to reuse getMaxAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed Sep 11, 2019
1 parent 3eda095 commit d7dc370
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
36 changes: 26 additions & 10 deletions src/components/send/form/formBase.js
@@ -1,10 +1,11 @@
import React from 'react';
import numeral from 'numeral';
import { Input } from '../../toolbox/inputs';
import { PrimaryButton, TertiaryButton } from '../../toolbox/buttons/button';
import { formatAmountBasedOnLocale } from '../../../utils/formattedNumber';
import { fromRawLsk } from '../../../utils/lsk';
import { getAmountFeedbackAndError } from '../../../utils/validators';
import { parseSearchParams } from '../../../utils/searchParams';
import { validateAmountFormat } from '../../../utils/validators';
import BookmarkAutoSuggest from './bookmarkAutoSuggest';
import Box from '../../toolbox/box';
import Converter from '../../converter';
Expand Down Expand Up @@ -33,7 +34,7 @@ class FormBase extends React.Component {
title: '',
},
amount: amount ? {
...getAmountFeedbackAndError({ value: amount, ...props }),
...this.getAmountFeedbackAndError(amount, props),
value: amount,
} : {
error: false,
Expand All @@ -53,7 +54,7 @@ class FormBase extends React.Component {
this.onGoNext = this.onGoNext.bind(this);
this.onInputChange = this.onInputChange.bind(this);
this.updateField = this.updateField.bind(this);
this.sendEntireBalance = this.sendEntireBalance.bind(this);
this.setEntireBalance = this.setEntireBalance.bind(this);
}

onInputChange({ target }) {
Expand All @@ -71,21 +72,36 @@ class FormBase extends React.Component {
value = leadingPoint.test(value) ? `0${value}` : value;

this.updateField('amount', {
...getAmountFeedbackAndError({ value, ...this.props }),
...this.getAmountFeedbackAndError(value),
value,
});
}

sendEntireBalance() {
getMaxAmount() {
const { account, fee } = this.props;
const getMaxAmount = () => formatAmountBasedOnLocale({
value: fromRawLsk(Math.max(0, account.balance - fee)),
return fromRawLsk(Math.max(0, account.balance - fee));
}

getAmountFeedbackAndError(value, props) {
const { token, t } = (props || this.props);
let { message: feedback } = validateAmountFormat({ value, token });

if (!feedback && parseFloat(this.getMaxAmount()) < numeral(value).value()) {
feedback = t('Provided amount is higher than your current balance.');
}
return { error: !!feedback, feedback };
}

setEntireBalance() {
const { fee } = this.props;
const value = formatAmountBasedOnLocale({
value: this.getMaxAmount(),
format: '0.[00000000]',
});
this.onInputChange({ target: { value: getMaxAmount(), name: 'amount' } });
this.onInputChange({ target: { value, name: 'amount' } });
setTimeout(() => {
if (fee !== this.props.fee) { // Because fee can change based on amount
this.sendEntireBalance();
this.setEntireBalance();
}
}, 1);
}
Expand Down Expand Up @@ -156,7 +172,7 @@ class FormBase extends React.Component {
<div className={`${styles.amountFieldHeader}`}>
<span className={`${styles.fieldLabel}`}>{t('Amount')}</span>
<TertiaryButton
onClick={this.sendEntireBalance}
onClick={this.setEntireBalance}
className="send-entire-balance-button"
size="xs"
>
Expand Down
19 changes: 1 addition & 18 deletions src/utils/validators.js
@@ -1,6 +1,5 @@
import * as bitcoin from 'bitcoinjs-lib';
import numeral from 'numeral';
import { fromRawLsk } from './lsk';
import { tokenMap } from '../constants/tokens';
import getBtcConfig from './api/btc/config';
import i18n from '../i18n';
Expand Down Expand Up @@ -55,7 +54,7 @@ export const validateAddress = (tokenType, address, netCode = 1) => {
export const validateAmountFormat = ({
value,
token = 'LSK',
locale = 'en',
locale = i18n.language,
}) => {
const errors = {
INVALID: i18n.t('Provide a correct amount of {{token}}', { token }),
Expand All @@ -71,19 +70,3 @@ export const validateAmountFormat = ({
message,
};
};

export function getAmountFeedbackAndError({
value, fee, account, token,
}) {
let { message: feedback } = validateAmountFormat({
value,
token,
locale: i18n.language,
});

const getMaxAmount = () => fromRawLsk(Math.max(0, account.balance - fee));
if (!feedback && parseFloat(getMaxAmount()) < numeral(value).value()) {
feedback = i18n.t('Provided amount is higher than your current balance.');
}
return { error: !!feedback, feedback };
}

0 comments on commit d7dc370

Please sign in to comment.