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 'web/components/layout/spacer' import { useUser } from 'web/hooks/use-user' import { Contract, contractPath } from 'web/lib/firebase/contracts' import { createContract } from 'web/lib/firebase/api-call' import { FIXED_ANTE, MINIMUM_ANTE } from 'common/antes' import { InfoTooltip } from 'web/components/info-tooltip' import { Page } from 'web/components/page' import { Title } from 'web/components/title' import { ProbabilitySelector } from 'web/components/probability-selector' import { Row } from 'web/components/layout/row' import { MAX_DESCRIPTION_LENGTH, outcomeType } from 'common/contract' import { formatMoney } from 'common/util/format' import { useHasCreatedContractToday } from 'web/hooks/use-has-created-contract-today' import { removeUndefinedProps } from '../../common/util/object' import { CATEGORIES } from 'common/categories' 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 [minString, setMinString] = useState('') const [maxString, setMaxString] = useState('') const [description, setDescription] = useState('') const [category, setCategory] = useState('') // const [tagText, setTagText] = useState(tag ?? '') // const tags = parseWordsAsTags(tagText) const [ante, setAnte] = useState(FIXED_ANTE) const deservesDailyFreeMarket = !useHasCreatedContractToday(creator) // useEffect(() => { // if (ante === null && creator) { // const initialAnte = creator.balance < 100 ? MINIMUM_ANTE : 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 min = minString ? parseFloat(minString) : undefined const max = maxString ? parseFloat(maxString) : undefined const isValid = initialProb > 0 && initialProb < 100 && question.length > 0 && ante !== undefined && ante !== null && ante >= MINIMUM_ANTE && (ante <= balance || deservesDailyFreeMarket) && // closeTime must be in the future closeTime && closeTime > Date.now() && (outcomeType !== 'NUMERIC' || (min !== undefined && max !== undefined && isFinite(min) && isFinite(max) && min < max && max - min > 0.01)) async function submit() { // TODO: Tell users why their contract is invalid if (!creator || !isValid) return setIsSubmitting(true) try { const result = await createContract( removeUndefinedProps({ question, outcomeType, description, initialProb, ante, closeTime, tags: category ? [category] : undefined, min, max, }) ) await router.push(contractPath(result as Contract)) } catch (e) { console.log('error creating contract', e) } } 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 setOutcomeType('NUMERIC')} /> Numeric (experimental) {outcomeType === 'BINARY' && ( Initial probability )} {outcomeType === 'NUMERIC' && ( Range e.stopPropagation()} onChange={(e) => setMinString(e.target.value)} min={Number.MIN_SAFE_INTEGER} max={Number.MAX_SAFE_INTEGER} disabled={isSubmitting} value={minString ?? ''} /> e.stopPropagation()} onChange={(e) => setMaxString(e.target.value)} min={Number.MIN_SAFE_INTEGER} max={Number.MAX_SAFE_INTEGER} disabled={isSubmitting} value={maxString} /> )} Description e.stopPropagation()} onChange={(e) => setDescription(e.target.value || '')} /> Category setCategory(e.currentTarget.value ?? '')} > (none) {Object.entries(CATEGORIES).map(([id, name]) => ( {name} ))} Market close e.stopPropagation()} onChange={(e) => setCloseDate(e.target.value || '')} min={Date.now()} disabled={isSubmitting} value={closeDate} /> Cost {!deservesDailyFreeMarket && ( )} {deservesDailyFreeMarket ? ( FREE ) : ( {formatMoney(ante)} )} {!deservesDailyFreeMarket && ante > balance && ( Insufficient balance (window.location.href = '/add-funds')} > Add funds )} {/* */} { e.preventDefault() submit() }} > {isSubmitting ? 'Creating...' : 'Create market'} ) }