From c0ec7ff3f1ced08b24cfab066f18b6bfc36390c0 Mon Sep 17 00:00:00 2001 From: jahooma Date: Tue, 14 Dec 2021 18:08:55 -0600 Subject: [PATCH] Show error when bet exceeds balance. Don't allow bet number to get into exponential notation. --- web/components/bet-panel.tsx | 62 ++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/web/components/bet-panel.tsx b/web/components/bet-panel.tsx index 67e7d476..c198929b 100644 --- a/web/components/bet-panel.tsx +++ b/web/components/bet-panel.tsx @@ -19,6 +19,7 @@ export function BetPanel(props: { contract: Contract; className?: string }) { const [betChoice, setBetChoice] = useState<'YES' | 'NO'>('YES') const [betAmount, setBetAmount] = useState(undefined) + const [error, setError] = useState() const [isSubmitting, setIsSubmitting] = useState(false) const [wasSubmitted, setWasSubmitted] = useState(false) @@ -28,15 +29,34 @@ export function BetPanel(props: { contract: Contract; className?: string }) { } function onBetChange(str: string) { - const amount = parseInt(str) - setBetAmount(isNaN(amount) ? undefined : amount) - setWasSubmitted(false) + + const amount = parseInt(str) + + if ( + (str && isNaN(amount)) || + // Don't update to amount that is rendered in exponential notation. + // e.g. '1e21' + amount.toString().includes('e') + ) { + return + } + + setBetAmount(str ? amount : undefined) + + if (user && user.balance < amount) setError('Balance insufficent') + else setError(undefined) } async function submitBet() { if (!user || !betAmount) return + if (user.balance < betAmount) { + setError('Balance insufficent') + return + } + + setError(undefined) setIsSubmitting(true) const result = await placeBet({ @@ -51,7 +71,7 @@ export function BetPanel(props: { contract: Contract; className?: string }) { setBetAmount(undefined) } - const betDisabled = isSubmitting || !betAmount + const betDisabled = isSubmitting || !betAmount || error const initialProb = getProbability(contract.pot, betChoice) const resultProb = getProbability(contract.pot, betChoice, betAmount) @@ -81,21 +101,29 @@ export function BetPanel(props: { contract: Contract; className?: string }) {
Bet amount
- -
- M$ + + +
+ M$ +
+ onBetChange(e.target.value)} + /> +
+
+ {error}
- onBetChange(e.target.value)} - /> - + - +
Implied probability