From 6e19f790b2900ef9913661b588d59be862c3aaa0 Mon Sep 17 00:00:00 2001 From: Anthony Date: Mon, 26 Jul 2021 20:51:41 +0200 Subject: [PATCH] close to working perfectly --- ui/component/walletSendTip/view.jsx | 62 ++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/ui/component/walletSendTip/view.jsx b/ui/component/walletSendTip/view.jsx index c8acff5ea..43cf4aa02 100644 --- a/ui/component/walletSendTip/view.jsx +++ b/ui/component/walletSendTip/view.jsx @@ -313,21 +313,63 @@ function WalletSendTip(props: Props) { } } + var countDecimals = function(value) { + var text = value.toString(); + var index = text.indexOf('.'); + return (text.length - index - 1); + } + function handleCustomPriceChange(event: SyntheticInputEvent<*>) { - let tipAmount = parseFloat(event.target.value); - // allow maximum two decimalds + console.log(event.target.value); + + let tipAmountAsString = event.target.value; + + let tipAmount = parseFloat(tipAmountAsString); + + // allow maximum two decimals if (activeTab === TAB_FIAT) { - tipAmount = Math.round(tipAmount * 100) / 100; - } - // TODO: add limit to 4 digits - // can also do setTipError('Maximum 1000') that way - if(tipAmount.length > 5 && tipAmount > 1000){ - tipAmount.length = 4 - } + console.log(tipAmount); - setCustomTipAmount(tipAmount); + console.log(Number.isNaN(tipAmount)) + + if (Number.isNaN(tipAmount)) { + setCustomTipAmount(''); + } + + + const howManyDecimals = countDecimals(tipAmountAsString); + + console.log('how many decimals'); + console.log(howManyDecimals) + + if (howManyDecimals > 2) { + tipAmount = Math.floor(tipAmount * 100) / 100; + // setTipError('Value can only have two decimal places'); + } + // else { + // tipAmount = ((tipAmount * 100) / 100).toFixed(2); + // } + + + // console.log(howManyDecimals); + + console.log(tipAmount); + + const howManyDigits = Math.trunc(tipAmount).toString().length; + + if (howManyDigits > 4 && tipAmount !== 1000) { + setTipError('Value must be below 1000 dollars'); + } else if (tipAmount > 1000) { + setTipError('Value must be below 1000 dollars'); + setCustomTipAmount(tipAmount); + } else { + setCustomTipAmount(tipAmount); + } + } else { + setCustomTipAmount(tipAmount); + } } function buildButtonText() {