2021-12-10 17:14:05 +00:00
|
|
|
import clsx from 'clsx'
|
2021-12-10 14:56:17 +00:00
|
|
|
import React, { useState } from 'react'
|
2021-12-10 17:14:05 +00:00
|
|
|
import { useUser } from '../hooks/use-user'
|
|
|
|
import { Bet, saveBet } from '../lib/firebase/bets'
|
2021-12-10 14:56:17 +00:00
|
|
|
import { Contract } from '../lib/firebase/contracts'
|
|
|
|
import { Col } from './layout/col'
|
2021-12-10 15:51:48 +00:00
|
|
|
import { Row } from './layout/row'
|
2021-12-10 14:56:17 +00:00
|
|
|
import { Spacer } from './layout/spacer'
|
|
|
|
import { YesNoSelector } from './yes-no-selector'
|
|
|
|
|
|
|
|
export function BetPanel(props: { contract: Contract; className?: string }) {
|
|
|
|
const { contract, className } = props
|
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
const user = useUser()
|
|
|
|
|
2021-12-10 14:56:17 +00:00
|
|
|
const [betChoice, setBetChoice] = useState<'YES' | 'NO'>('YES')
|
2021-12-10 15:51:48 +00:00
|
|
|
const [betAmount, setBetAmount] = useState<number | undefined>(undefined)
|
2021-12-10 14:56:17 +00:00
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
const [wasSubmitted, setWasSubmitted] = useState(false)
|
|
|
|
|
2021-12-10 16:04:59 +00:00
|
|
|
function onBetChange(str: string) {
|
|
|
|
const amount = parseInt(str)
|
|
|
|
setBetAmount(isNaN(amount) ? undefined : amount)
|
|
|
|
}
|
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
async function submitBet() {
|
|
|
|
if (!user || !betAmount) return
|
|
|
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
|
|
|
const bet: Bet = {
|
|
|
|
id: `${now}-${user.id}`,
|
|
|
|
userId: user.id,
|
|
|
|
contractId: contract.id,
|
|
|
|
createdTime: now,
|
|
|
|
outcome: betChoice,
|
|
|
|
amount: betAmount,
|
|
|
|
|
|
|
|
// Placeholder.
|
|
|
|
dpmWeight: betAmount,
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
|
|
|
await saveBet(bet)
|
|
|
|
|
|
|
|
setIsSubmitting(false)
|
|
|
|
setWasSubmitted(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
function newBet() {
|
|
|
|
setBetAmount(undefined)
|
|
|
|
setWasSubmitted(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
const betDisabled = isSubmitting || wasSubmitted
|
|
|
|
|
2021-12-10 14:56:17 +00:00
|
|
|
return (
|
|
|
|
<Col className={'bg-gray-600 p-6 rounded ' + className}>
|
|
|
|
<div className="p-2 font-medium">Pick outcome</div>
|
|
|
|
<YesNoSelector
|
|
|
|
className="p-2"
|
|
|
|
selected={betChoice}
|
|
|
|
onSelect={setBetChoice}
|
|
|
|
yesLabel="Yes 57"
|
|
|
|
noLabel="No 43"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
2021-12-10 15:51:48 +00:00
|
|
|
<div className="p-2 font-medium">Bet amount</div>
|
|
|
|
<Row className="p-2 items-center">
|
2021-12-10 14:56:17 +00:00
|
|
|
<input
|
|
|
|
className="input input-bordered input-md"
|
|
|
|
style={{ maxWidth: 80 }}
|
|
|
|
type="text"
|
2021-12-10 15:51:48 +00:00
|
|
|
placeholder="0"
|
2021-12-10 16:04:59 +00:00
|
|
|
value={betAmount ?? ''}
|
|
|
|
onChange={(e) => onBetChange(e.target.value)}
|
2021-12-10 14:56:17 +00:00
|
|
|
/>
|
2021-12-10 15:51:48 +00:00
|
|
|
<div className="ml-3">points</div>
|
|
|
|
</Row>
|
2021-12-10 14:56:17 +00:00
|
|
|
|
2021-12-10 15:51:48 +00:00
|
|
|
{!!betAmount && (
|
|
|
|
<>
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="p-2 font-medium">Average price</div>
|
2021-12-10 17:14:05 +00:00
|
|
|
<div className="px-2">{betChoice === 'YES' ? 0.57 : 0.43} points</div>
|
2021-12-10 15:51:48 +00:00
|
|
|
|
|
|
|
<Spacer h={2} />
|
2021-12-10 14:56:17 +00:00
|
|
|
|
2021-12-10 15:51:48 +00:00
|
|
|
<div className="p-2 font-medium">Estimated winnings</div>
|
|
|
|
<div className="px-2">
|
|
|
|
{Math.floor(betAmount / (betChoice === 'YES' ? 0.57 : 0.43))} points
|
|
|
|
</div>
|
2021-12-10 14:56:17 +00:00
|
|
|
|
2021-12-10 15:51:48 +00:00
|
|
|
<Spacer h={6} />
|
2021-12-10 14:56:17 +00:00
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
<button
|
|
|
|
className={clsx(
|
|
|
|
'btn',
|
|
|
|
betDisabled ? 'btn-disabled' : 'btn-primary'
|
|
|
|
)}
|
|
|
|
onClick={betDisabled ? undefined : submitBet}
|
|
|
|
>
|
|
|
|
Place bet
|
|
|
|
</button>
|
|
|
|
|
|
|
|
{wasSubmitted && (
|
|
|
|
<Col>
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div>Bet submitted!</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<button className="btn btn-primary btn-xs" onClick={newBet}>
|
|
|
|
New bet
|
|
|
|
</button>
|
|
|
|
</Col>
|
|
|
|
)}
|
2021-12-10 15:51:48 +00:00
|
|
|
</>
|
2021-12-10 14:56:17 +00:00
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|