From eb59ca77efe5dd0b8f4a6fd76e53528d2c70770d Mon Sep 17 00:00:00 2001 From: fmtabbara Date: Fri, 23 Jul 2021 09:11:11 +0100 Subject: [PATCH] catch error caused by invalid values --- wallet-web/components/bond/BondNodeForm.tsx | 22 ++++++----- .../components/delegate/DelegateForm.tsx | 38 ++++++++++--------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/wallet-web/components/bond/BondNodeForm.tsx b/wallet-web/components/bond/BondNodeForm.tsx index 36d0eeebaf..1846a814f4 100644 --- a/wallet-web/components/bond/BondNodeForm.tsx +++ b/wallet-web/components/bond/BondNodeForm.tsx @@ -92,16 +92,20 @@ export default function BondNodeForm(props: TBondNodeFormProps) { if (isNaN(parsed)) { setIsValidAmount(false) } else { - const allocationCheck = checkAllocationSize( - +printableBalanceToNative(e.target.value), - balance - ) - if (allocationCheck.error) { - setAllocationWarning(allocationCheck.message) + try { + const allocationCheck = checkAllocationSize( + +printableBalanceToNative(e.target.value), + balance + ) + if (allocationCheck.error) { + setAllocationWarning(allocationCheck.message) + setIsValidAmount(false) + } else { + setAllocationWarning(allocationCheck.message) + setIsValidAmount(true) + } + } catch { setIsValidAmount(false) - } else { - setAllocationWarning(allocationCheck.message) - setIsValidAmount(true) } } } diff --git a/wallet-web/components/delegate/DelegateForm.tsx b/wallet-web/components/delegate/DelegateForm.tsx index 619d4fb684..57794c1c20 100644 --- a/wallet-web/components/delegate/DelegateForm.tsx +++ b/wallet-web/components/delegate/DelegateForm.tsx @@ -21,7 +21,7 @@ type DelegateFormProps = { export default function DelegateForm(props: DelegateFormProps) { const classes = makeBasicStyle(theme) - const [validAmount, setValidAmount] = useState(true) + const [isValidAmount, setIsValidAmount] = useState(true) const [validIdentity, setValidIdentity] = useState(true) const [allocationWarning, setAllocationWarning] = useState() const { getBalance, accountBalance } = useGetBalance() @@ -38,18 +38,22 @@ export default function DelegateForm(props: DelegateFormProps) { const balance = +accountBalance.amount if (isNaN(parsed)) { - setValidAmount(false) + setIsValidAmount(false) } else { - const allocationCheck = checkAllocationSize( - +printableBalanceToNative(event.target.value), - balance - ) - if (allocationCheck.error) { - setAllocationWarning(allocationCheck.message) - setValidAmount(false) - } else { - setAllocationWarning(allocationCheck.message) - setValidAmount(true) + try { + const allocationCheck = checkAllocationSize( + +printableBalanceToNative(event.target.value), + balance + ) + if (allocationCheck.error) { + setAllocationWarning(allocationCheck.message) + setIsValidAmount(false) + } else { + setAllocationWarning(allocationCheck.message) + setIsValidAmount(true) + } + } catch { + setIsValidAmount(false) } } } @@ -59,7 +63,7 @@ export default function DelegateForm(props: DelegateFormProps) { let validAmount = validateAmount(event.target.amount.value) setValidIdentity(validIdentity) - setValidAmount(validAmount) + setIsValidAmount(validAmount) return validIdentity && validAmount } @@ -101,8 +105,8 @@ export default function DelegateForm(props: DelegateFormProps) { id='amount' name='amount' label='Amount to delegate' - error={!validAmount} - helperText={validAmount ? '' : 'Please enter a valid amount'} + error={!isValidAmount} + helperText={isValidAmount ? '' : 'Please enter a valid amount'} onChange={handleAmountChange} fullWidth InputProps={{ @@ -114,7 +118,7 @@ export default function DelegateForm(props: DelegateFormProps) { {allocationWarning && ( - + {allocationWarning} @@ -139,7 +143,7 @@ export default function DelegateForm(props: DelegateFormProps) { color='primary' type='submit' className={classes.button} - disabled={!validAmount} + disabled={!isValidAmount} > Delegate stake