import clsx from 'clsx' import dayjs from 'dayjs' import { useEffect, useState } from 'react' import { DuplicateIcon } from '@heroicons/react/outline' import { Col } from '../layout/col' import { Row } from '../layout/row' import { Title } from '../title' import { User } from 'common/user' import { Modal } from 'web/components/layout/modal' import { Button } from '../button' import { createChallenge, getChallengeUrl } from 'web/lib/firebase/challenges' import { BinaryContract } from 'common/contract' import { CopyLinkButton } from 'web/components/copy-link-button' import { SiteLink } from 'web/components/site-link' import { formatMoney } from 'common/util/format' import { Spacer } from '../layout/spacer' import { NoLabel, YesLabel } from '../outcome-label' import { QRCode } from '../qr-code' type challengeInfo = { amount: number expiresTime: number | null message: string outcome: 'YES' | 'NO' | number acceptorAmount: number } export function CreateChallengeButton(props: { user: User | null | undefined contract: BinaryContract }) { const { user, contract } = props const [open, setOpen] = useState(false) const [highlightedSlug, setHighlightedSlug] = useState('') return ( <> setOpen(newOpen)}> {/*// add a sign up to challenge button?*/} {user && ( { const challenge = await createChallenge({ creator: user, creatorAmount: newChallenge.amount, expiresTime: newChallenge.expiresTime, message: newChallenge.message, acceptorAmount: newChallenge.acceptorAmount, outcome: newChallenge.outcome, contract: contract, }) challenge && setHighlightedSlug(getChallengeUrl(challenge)) }} highlightedSlug={highlightedSlug} /> )} setOpen(true)} className="btn btn-outline mb-4 max-w-xs" > Challenge ️ > ) } function CreateChallengeForm(props: { user: User contract: BinaryContract onCreate: (m: challengeInfo) => Promise highlightedSlug: string }) { const { user, onCreate, contract, highlightedSlug } = props const [isCreating, setIsCreating] = useState(false) const [finishedCreating, setFinishedCreating] = useState(false) const [error, setError] = useState('') const [editingAcceptorAmount, setEditingAcceptorAmount] = useState(false) const defaultExpire = 'week' const defaultMessage = `${user.name} is challenging you to a bet! Do you think ${contract.question}` const [challengeInfo, setChallengeInfo] = useState({ expiresTime: dayjs().add(2, defaultExpire).valueOf(), outcome: 'YES', amount: 100, acceptorAmount: 100, message: defaultMessage, }) useEffect(() => { setError('') }, [challengeInfo]) return ( <> {!finishedCreating && ( { e.preventDefault() if (user.balance < challengeInfo.amount) { setError('You do not have enough mana to create this challenge') return } setIsCreating(true) onCreate(challengeInfo).finally(() => setIsCreating(false)) setFinishedCreating(true) }} > {/*Question:*/} {/*{contract.question}*/} You are betting: M$ setChallengeInfo((m: challengeInfo) => { return { ...m, amount: parseInt(e.target.value), acceptorAmount: editingAcceptorAmount ? m.acceptorAmount : parseInt(e.target.value), } }) } /> on setChallengeInfo((m: challengeInfo) => { return { ...m, outcome: e.target.value as 'YES' | 'NO', } }) } > YES NO They will bet: {editingAcceptorAmount ? ( M$ setChallengeInfo((m: challengeInfo) => { return { ...m, acceptorAmount: parseInt(e.target.value), } }) } /> ) : ( {formatMoney(challengeInfo.acceptorAmount)} )} on {challengeInfo.outcome === 'YES' ? : } {!editingAcceptorAmount && ( setEditingAcceptorAmount(!editingAcceptorAmount) } className={ 'flex flex-row gap-2 !bg-transparent py-1 hover:!bg-gray-100' } > Edit )} Continue {error} )} {finishedCreating && ( <> Share the challenge using the link. See your other challenges > )} > ) }