import clsx from 'clsx' import React, { useState } from 'react' import { useUser } from '../hooks/use-user' import { Bet, saveBet } from '../lib/firebase/bets' import { Contract } from '../lib/firebase/contracts' import { Col } from './layout/col' import { Row } from './layout/row' import { Spacer } from './layout/spacer' import { YesNoSelector } from './yes-no-selector' export function BetPanel(props: { contract: Contract; className?: string }) { const { contract, className } = props const user = useUser() const [betChoice, setBetChoice] = useState<'YES' | 'NO'>('YES') const [betAmount, setBetAmount] = useState(undefined) const [isSubmitting, setIsSubmitting] = useState(false) const [wasSubmitted, setWasSubmitted] = useState(false) function onBetChange(str: string) { const amount = parseInt(str) setBetAmount(isNaN(amount) ? undefined : amount) } 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 return (
Pick outcome
Bet amount
onBetChange(e.target.value)} />
points
{!!betAmount && ( <>
Average price
{betChoice === 'YES' ? 0.57 : 0.43} points
Estimated winnings
{Math.floor(betAmount / (betChoice === 'YES' ? 0.57 : 0.43))} points
{wasSubmitted && (
Bet submitted!
)} )} ) }