import router from 'next/router' import { useEffect, useState } from 'react' import clsx from 'clsx' import dayjs from 'dayjs' import Textarea from 'react-expanding-textarea' import { Spacer } from '../components/layout/spacer' import { useUser } from '../hooks/use-user' import { Contract, contractPath } from '../lib/firebase/contracts' import { createContract } from '../lib/firebase/api-call' import { AmountInput } from '../components/amount-input' import { MINIMUM_ANTE } from '../../common/antes' import { InfoTooltip } from '../components/info-tooltip' import { CREATOR_FEE } from '../../common/fees' import { Page } from '../components/page' import { Title } from '../components/title' import { ProbabilitySelector } from '../components/probability-selector' import { parseWordsAsTags } from '../../common/util/parse' import { TagsList } from '../components/tags-list' import { Row } from '../components/layout/row' import { outcomeType } from '../../common/contract' export default function Create() { const [question, setQuestion] = useState('') return ( Question setQuestion(e.target.value || '')} /> ) } // Allow user to create a new contract export function NewContract(props: { question: string; tag?: string }) { const { question, tag } = props const creator = useUser() useEffect(() => { if (creator === null) router.push('/') }, [creator]) useEffect(() => { createContract({}).catch() // warm up function }, []) const [outcomeType, setOutcomeType] = useState('BINARY') const [initialProb, setInitialProb] = useState(50) const [description, setDescription] = useState('') const [tagText, setTagText] = useState(tag ?? '') const tags = parseWordsAsTags(tagText) const [ante, setAnte] = useState(null) useEffect(() => { if (ante === null && creator) { const initialAnte = creator.balance < 100 ? 10 : 100 setAnte(initialAnte) } }, [ante, creator]) const [anteError, setAnteError] = useState() // By default, close the market a week from today const weekFromToday = dayjs().add(7, 'day').format('YYYY-MM-DDT23:59') const [closeDate, setCloseDate] = useState(weekFromToday) const [isSubmitting, setIsSubmitting] = useState(false) const closeTime = closeDate ? dayjs(closeDate).valueOf() : undefined const balance = creator?.balance || 0 const isValid = initialProb > 0 && initialProb < 100 && question.length > 0 && ante !== undefined && ante !== null && ante >= MINIMUM_ANTE && ante <= balance && // closeTime must be in the future closeTime && closeTime > Date.now() async function submit() { // TODO: Tell users why their contract is invalid if (!creator || !isValid) return setIsSubmitting(true) const result: any = await createContract({ question, outcomeType, description, initialProb, ante, closeTime, tags, }).then((r) => r.data || {}) if (result.status !== 'success') { console.log('error creating contract', result) return } await router.push(contractPath(result.contract as Contract)) } const descriptionPlaceholder = outcomeType === 'BINARY' ? `e.g. This market resolves to "YES" if, two weeks after closing, the...` : `e.g. I will choose the answer according to...` if (!creator) return <>> return ( Answer type setOutcomeType('BINARY')} /> Yes / No setOutcomeType('FREE_RESPONSE')} /> Free response {outcomeType === 'BINARY' && ( Initial probability )} Description e.stopPropagation()} onChange={(e) => setDescription(e.target.value || '')} /> Tags setTagText(e.target.value || '')} /> Market close e.stopPropagation()} onChange={(e) => setCloseDate(e.target.value || '')} min={Date.now()} disabled={isSubmitting} value={closeDate} /> Market ante { e.preventDefault() submit() }} > {isSubmitting ? 'Creating...' : 'Create market'} ) }