2022-01-11 03:41:42 +00:00
|
|
|
import clsx from 'clsx'
|
2022-03-02 03:31:48 +00:00
|
|
|
import _ from 'lodash'
|
2022-01-11 03:41:42 +00:00
|
|
|
import { useUser } from '../hooks/use-user'
|
2022-01-30 21:51:30 +00:00
|
|
|
import { formatMoney } from '../../common/util/format'
|
2022-01-11 03:41:42 +00:00
|
|
|
import { Col } from './layout/col'
|
|
|
|
import { Row } from './layout/row'
|
2022-03-02 03:31:48 +00:00
|
|
|
import { useUserContractBets } from '../hooks/use-user-bets'
|
|
|
|
import { MAX_LOAN_PER_CONTRACT } from '../../common/bet'
|
|
|
|
import { InfoTooltip } from './info-tooltip'
|
|
|
|
import { Spacer } from './layout/spacer'
|
2022-01-11 03:41:42 +00:00
|
|
|
|
|
|
|
export function AmountInput(props: {
|
|
|
|
amount: number | undefined
|
|
|
|
onChange: (newAmount: number | undefined) => void
|
|
|
|
error: string | undefined
|
|
|
|
setError: (error: string | undefined) => void
|
2022-03-02 03:31:48 +00:00
|
|
|
contractId: string | undefined
|
2022-01-14 23:39:17 +00:00
|
|
|
minimumAmount?: number
|
2022-01-11 03:41:42 +00:00
|
|
|
disabled?: boolean
|
|
|
|
className?: string
|
|
|
|
inputClassName?: string
|
2022-01-26 20:08:03 +00:00
|
|
|
// Needed to focus the amount input
|
|
|
|
inputRef?: React.MutableRefObject<any>
|
2022-01-11 03:41:42 +00:00
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
amount,
|
|
|
|
onChange,
|
|
|
|
error,
|
|
|
|
setError,
|
2022-03-02 03:31:48 +00:00
|
|
|
contractId,
|
2022-01-11 03:41:42 +00:00
|
|
|
disabled,
|
|
|
|
className,
|
|
|
|
inputClassName,
|
2022-01-14 23:39:17 +00:00
|
|
|
minimumAmount,
|
2022-01-26 20:08:03 +00:00
|
|
|
inputRef,
|
2022-01-11 03:41:42 +00:00
|
|
|
} = props
|
|
|
|
|
|
|
|
const user = useUser()
|
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
const userBets = useUserContractBets(user?.id, contractId) ?? []
|
|
|
|
const openUserBets = userBets.filter((bet) => !bet.isSold && !bet.sale)
|
|
|
|
const prevLoanAmount = _.sumBy(openUserBets, (bet) => bet.loanAmount ?? 0)
|
|
|
|
|
|
|
|
const loanAmount = Math.min(
|
|
|
|
amount ?? 0,
|
|
|
|
MAX_LOAN_PER_CONTRACT - prevLoanAmount
|
|
|
|
)
|
|
|
|
|
2022-01-11 03:41:42 +00:00
|
|
|
const onAmountChange = (str: string) => {
|
2022-03-02 03:31:48 +00:00
|
|
|
if (str.includes('-')) {
|
|
|
|
onChange(undefined)
|
|
|
|
return
|
|
|
|
}
|
2022-01-11 03:41:42 +00:00
|
|
|
const amount = parseInt(str.replace(/[^\d]/, ''))
|
|
|
|
|
|
|
|
if (str && isNaN(amount)) return
|
2022-03-02 03:31:48 +00:00
|
|
|
if (amount >= 10 ** 9) return
|
2022-01-11 03:41:42 +00:00
|
|
|
|
|
|
|
onChange(str ? amount : undefined)
|
|
|
|
|
2022-01-14 23:39:17 +00:00
|
|
|
if (user && user.balance < amount) {
|
|
|
|
setError('Insufficient balance')
|
|
|
|
} else if (minimumAmount && amount < minimumAmount) {
|
|
|
|
setError('Minimum amount: ' + formatMoney(minimumAmount))
|
|
|
|
} else {
|
|
|
|
setError(undefined)
|
|
|
|
}
|
2022-01-11 03:41:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
const amountNetLoan = (amount ?? 0) - loanAmount
|
|
|
|
const remainingBalance = Math.max(0, (user?.balance ?? 0) - amountNetLoan)
|
2022-01-11 03:41:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Col className={className}>
|
|
|
|
<label className="input-group">
|
2022-02-11 18:40:22 +00:00
|
|
|
<span className="bg-gray-200 text-sm">M$</span>
|
2022-01-11 03:41:42 +00:00
|
|
|
<input
|
|
|
|
className={clsx(
|
|
|
|
'input input-bordered',
|
|
|
|
error && 'input-error',
|
|
|
|
inputClassName
|
|
|
|
)}
|
2022-01-26 20:08:03 +00:00
|
|
|
ref={inputRef}
|
2022-02-01 01:05:01 +00:00
|
|
|
type="number"
|
2022-01-11 03:41:42 +00:00
|
|
|
placeholder="0"
|
|
|
|
maxLength={9}
|
|
|
|
value={amount ?? ''}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={(e) => onAmountChange(e.target.value)}
|
|
|
|
/>
|
|
|
|
</label>
|
2022-03-02 03:31:48 +00:00
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
2022-01-14 23:39:17 +00:00
|
|
|
{error && (
|
2022-03-02 03:31:48 +00:00
|
|
|
<div className="mb-2 mr-auto self-center whitespace-nowrap text-xs font-medium tracking-wide text-red-500">
|
2022-01-14 23:39:17 +00:00
|
|
|
{error}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{user && (
|
2022-03-02 03:31:48 +00:00
|
|
|
<Col className="gap-3 text-sm">
|
|
|
|
{contractId && (
|
|
|
|
<Row className="items-center justify-between gap-2 text-gray-500">
|
|
|
|
<Row className="items-center gap-2">
|
|
|
|
Amount loaned{' '}
|
|
|
|
<InfoTooltip
|
|
|
|
text={`In every market, you get an interest-free loan on the first ${formatMoney(
|
|
|
|
MAX_LOAN_PER_CONTRACT
|
|
|
|
)}.`}
|
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
<span className="text-neutral">{formatMoney(loanAmount)}</span>{' '}
|
|
|
|
</Row>
|
|
|
|
)}
|
|
|
|
<Row className="items-center justify-between gap-2 text-gray-500">
|
|
|
|
Remaining balance{' '}
|
|
|
|
<span className="text-neutral">
|
|
|
|
{formatMoney(Math.floor(remainingBalance))}
|
|
|
|
</span>
|
2022-01-14 23:39:17 +00:00
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
)}
|
2022-01-11 03:41:42 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|