From 713dc69ed7742e4f531dbb2b2a8d0ee7ae632d8c Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 22 Jul 2021 18:44:30 +0200 Subject: [PATCH] add check for two decimals and fix showing error for fiat tip --- ui/component/walletSendTip/view.jsx | 17 +++++++++++++---- ui/component/walletTipAmountSelector/view.jsx | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/ui/component/walletSendTip/view.jsx b/ui/component/walletSendTip/view.jsx index 981833ffe..8d035fdea 100644 --- a/ui/component/walletSendTip/view.jsx +++ b/ui/component/walletSendTip/view.jsx @@ -186,8 +186,7 @@ function WalletSendTip(props: Props) { React.useEffect(() => { // Regex for number up to 8 decimal places - const regexp = RegExp(/^(\d*([.]\d{0,8})?)$/); - const validTipInput = regexp.test(String(tipAmount)); + let regexp; let tipError; if (tipAmount === 0) { @@ -198,8 +197,13 @@ function WalletSendTip(props: Props) { // if it's not fiat, aka it's boost or lbc tip else if (activeTab !== TAB_FIAT) { + regexp = RegExp(/^(\d*([.]\d{0,8})?)$/); + const validTipInput = regexp.test(String(tipAmount)); + if (!validTipInput) { tipError = __('Amount must have no more than 8 decimal places'); + } else if (!validTipInput) { + tipError = __('Amount must have no more than 8 decimal places'); } else if (tipAmount === balance) { tipError = __('Please decrease the amount to account for transaction fees'); } else if (tipAmount > balance) { @@ -209,7 +213,12 @@ function WalletSendTip(props: Props) { } // if tip fiat tab } else { - if (tipAmount < 1) { + regexp = RegExp(/^(\d*([.]\d{0,2})?)$/); + const validTipInput = regexp.test(String(tipAmount)); + + if (!validTipInput) { + tipError = __('Amount must have no more than 2 decimal places'); + } else if (tipAmount < 1) { tipError = __('Amount must be at least one dollar'); } else if (tipAmount > 1000) { tipError = __('Amount cannot be over 1000 dollars'); @@ -544,7 +553,7 @@ function WalletSendTip(props: Props) { } className="form-field--price-amount" - error={tipError && activeTab !== TAB_FIAT} + error={tipError} min="0" step="any" type="number" diff --git a/ui/component/walletTipAmountSelector/view.jsx b/ui/component/walletTipAmountSelector/view.jsx index a0246ca47..98006abe7 100644 --- a/ui/component/walletTipAmountSelector/view.jsx +++ b/ui/component/walletTipAmountSelector/view.jsx @@ -120,9 +120,7 @@ function WalletTipAmountSelector(props: Props) { // setHasSavedCard(false); // setCanReceiveFiatTip(true); - const regexp = RegExp(/^(\d*([.]\d{0,8})?)$/); - const validTipInput = regexp.test(String(amount)); - let tipError = ''; + let regexp, tipError; if (amount === 0) { tipError = __('Amount must be a positive number'); @@ -132,6 +130,9 @@ function WalletTipAmountSelector(props: Props) { // if it's not fiat, aka it's boost or lbc tip else if (activeTab !== TAB_FIAT) { + regexp = RegExp(/^(\d*([.]\d{0,8})?)$/); + const validTipInput = regexp.test(String(amount)); + if (!validTipInput) { tipError = __('Amount must have no more than 8 decimal places'); } else if (amount === balance) { @@ -143,7 +144,12 @@ function WalletTipAmountSelector(props: Props) { } // if tip fiat tab } else { - if (amount < 1) { + regexp = RegExp(/^(\d*([.]\d{0,2})?)$/); + const validTipInput = regexp.test(String(amount)); + + if (!validTipInput) { + tipError = __('Amount must have no more than 2 decimal places'); + } else if (amount < 1) { tipError = __('Amount must be at least one dollar'); } else if (amount > 1000) { tipError = __('Amount cannot be over 1000 dollars'); @@ -154,8 +160,10 @@ function WalletTipAmountSelector(props: Props) { onTipErrorChange(tipError); }, [amount, balance, setTipError, activeTab]); + // parse number as float and sets it in the parent component function handleCustomPriceChange(amount: number) { const tipAmount = parseFloat(amount); + onChange(tipAmount); } @@ -229,6 +237,7 @@ function WalletTipAmountSelector(props: Props) { )} + {/* custom number input form */} {useCustomTip && (