From 49bb74cc0d7f2e6039d5a9870badf54a73e981fd Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sat, 19 Mar 2022 16:38:05 -0500 Subject: [PATCH] Split BuyAmountInput out of AmountInput --- web/components/amount-input.tsx | 148 +++++++++++------- web/components/answers/answer-bet-panel.tsx | 4 +- .../answers/create-answer-panel.tsx | 4 +- web/components/bet-panel.tsx | 4 +- web/pages/create.tsx | 4 +- web/pages/make-predictions.tsx | 4 +- 6 files changed, 105 insertions(+), 63 deletions(-) diff --git a/web/components/amount-input.tsx b/web/components/amount-input.tsx index 8f20c0ab..50d00f88 100644 --- a/web/components/amount-input.tsx +++ b/web/components/amount-input.tsx @@ -10,6 +10,74 @@ import { InfoTooltip } from './info-tooltip' import { Spacer } from './layout/spacer' export function AmountInput(props: { + amount: number | undefined + onChange: (newAmount: number | undefined) => void + error: string | undefined + disabled?: boolean + className?: string + inputClassName?: string + // Needed to focus the amount input + inputRef?: React.MutableRefObject + children?: any +}) { + const { + amount, + onChange, + error, + disabled, + className, + inputClassName, + inputRef, + children, + } = props + + const onAmountChange = (str: string) => { + if (str.includes('-')) { + onChange(undefined) + return + } + const amount = parseInt(str.replace(/[^\d]/, '')) + + if (str && isNaN(amount)) return + if (amount >= 10 ** 9) return + + onChange(str ? amount : undefined) + } + + return ( + + + + + + {error && ( +
+ {error} +
+ )} + + {children} + + ) +} + +export function BuyAmountInput(props: { amount: number | undefined onChange: (newAmount: number | undefined) => void error: string | undefined @@ -45,62 +113,36 @@ export function AmountInput(props: { ? Math.min(amount ?? 0, MAX_LOAN_PER_CONTRACT - prevLoanAmount) : 0 - const onAmountChange = (str: string) => { - if (str.includes('-')) { - onChange(undefined) - return - } - const amount = parseInt(str.replace(/[^\d]/, '')) - - if (str && isNaN(amount)) return - if (amount >= 10 ** 9) return - - onChange(str ? amount : undefined) - - const loanAmount = contractIdForLoan - ? Math.min(amount, MAX_LOAN_PER_CONTRACT - prevLoanAmount) - : 0 - const amountNetLoan = amount - loanAmount - - if (user && user.balance < amountNetLoan) { - setError('Insufficient balance') - } else if (minimumAmount && amount < minimumAmount) { - setError('Minimum amount: ' + formatMoney(minimumAmount)) - } else { - setError(undefined) - } - } - const amountNetLoan = (amount ?? 0) - loanAmount const remainingBalance = Math.max(0, (user?.balance ?? 0) - amountNetLoan) + const onAmountChange = (amount: number | undefined) => { + onChange(amount) + + // Check for errors. + if (amount !== undefined) { + const amountNetLoan = amount - loanAmount + + if (user && user.balance < amountNetLoan) { + setError('Insufficient balance') + } else if (minimumAmount && amount < minimumAmount) { + setError('Minimum amount: ' + formatMoney(minimumAmount)) + } else { + setError(undefined) + } + } + } + return ( - - - - - - {error && ( -
- {error} -
- )} + {user && ( {contractIdForLoan && ( @@ -124,6 +166,6 @@ export function AmountInput(props: { )} - + ) } diff --git a/web/components/answers/answer-bet-panel.tsx b/web/components/answers/answer-bet-panel.tsx index 65758f0b..076884d3 100644 --- a/web/components/answers/answer-bet-panel.tsx +++ b/web/components/answers/answer-bet-panel.tsx @@ -5,7 +5,7 @@ import { XIcon } from '@heroicons/react/solid' import { Answer } from '../../../common/answer' import { DPM, FreeResponse, FullContract } from '../../../common/contract' -import { AmountInput } from '../amount-input' +import { BuyAmountInput } from '../amount-input' import { Col } from '../layout/col' import { placeBet } from '../../lib/firebase/api-call' import { Row } from '../layout/row' @@ -114,7 +114,7 @@ export function AnswerBetPanel(props: { )}
Amount
-
Buy amount
-
Amount
- - -