2022-01-11 03:41:42 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
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 { AddFundsButton } from './add-funds-button'
|
|
|
|
import { Col } from './layout/col'
|
|
|
|
import { Row } from './layout/row'
|
|
|
|
|
|
|
|
export function AmountInput(props: {
|
|
|
|
amount: number | undefined
|
|
|
|
onChange: (newAmount: number | undefined) => void
|
|
|
|
error: string | undefined
|
|
|
|
setError: (error: string | undefined) => void
|
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,
|
|
|
|
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()
|
|
|
|
|
|
|
|
const onAmountChange = (str: string) => {
|
|
|
|
const amount = parseInt(str.replace(/[^\d]/, ''))
|
|
|
|
|
|
|
|
if (str && isNaN(amount)) return
|
|
|
|
|
|
|
|
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-01-14 23:39:17 +00:00
|
|
|
const remainingBalance = Math.max(0, (user?.balance ?? 0) - (amount ?? 0))
|
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-01-14 23:39:17 +00:00
|
|
|
{error && (
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="mr-auto mt-4 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-02-11 18:40:22 +00:00
|
|
|
<Col className="mt-3 text-sm">
|
|
|
|
<div className="mb-2 whitespace-nowrap text-gray-500">
|
2022-01-14 23:39:17 +00:00
|
|
|
Remaining balance
|
2022-01-12 05:29:50 +00:00
|
|
|
</div>
|
2022-01-14 23:39:17 +00:00
|
|
|
<Row className="gap-4">
|
2022-02-17 03:14:12 +00:00
|
|
|
<div>{formatMoney(Math.floor(remainingBalance))}</div>
|
2022-01-14 23:39:17 +00:00
|
|
|
{user.balance !== 1000 && <AddFundsButton />}
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
)}
|
2022-01-11 03:41:42 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|