From d7dc37041c8f2291177b34d342f940b7dced8b3a Mon Sep 17 00:00:00 2001 From: Vit Stanislav Date: Wed, 11 Sep 2019 11:49:40 +0200 Subject: [PATCH] :recycle: Move getAmountFeedbackAndError to FormBase to reuse getMaxAmount --- src/components/send/form/formBase.js | 36 ++++++++++++++++++++-------- src/utils/validators.js | 19 +-------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/components/send/form/formBase.js b/src/components/send/form/formBase.js index bf6e8784c1..516a2d97d3 100644 --- a/src/components/send/form/formBase.js +++ b/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'; @@ -33,7 +34,7 @@ class FormBase extends React.Component { title: '', }, amount: amount ? { - ...getAmountFeedbackAndError({ value: amount, ...props }), + ...this.getAmountFeedbackAndError(amount, props), value: amount, } : { error: false, @@ -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 }) { @@ -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); } @@ -156,7 +172,7 @@ class FormBase extends React.Component {
{t('Amount')} diff --git a/src/utils/validators.js b/src/utils/validators.js index 942c65ae72..5adfd58b2f 100644 --- a/src/utils/validators.js +++ b/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'; @@ -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 }), @@ -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 }; -}