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 { Row } from 'web/components/layout/row'
import { MAX_DESCRIPTION_LENGTH, outcomeType, resolution, resolutionType } 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'
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
import { CalculatorIcon } from '@heroicons/react/outline'
import { write } from 'fs'
export default function Create() {
const [question, setQuestion] = useState('')
return (
)
}
// 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 [resolutionType, setResolutionType] = useState('MANUAL')
const [automaticResolution, setAutomaticResolution] = useState('YES')
const [initialProb, setInitialProb] = useState(50)
const [minString, setMinString] = useState('')
const [maxString, setMaxString] = useState('')
const [description, setDescription] = useState('')
const [showCalendar, setShowCalendar] = useState(false)
const [showNumInput, setShowNumInput] = useState(false)
const [category, setCategory] = useState('')
// const [tagText, setTagText] = useState(tag ?? '')
// const tags = parseWordsAsTags(tagText)
const [ante, setAnte] = useState(FIXED_ANTE)
const mustWaitForDailyFreeMarketStatus = 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 [closeDate, setCloseDate] = useState(weekFrom(dayjs()))
const [resolutionDate, setResolutionDate] = useState(weekFrom(closeDate))
const [isSubmitting, setIsSubmitting] = useState(false)
const closeTime = closeDate ? dayjs(closeDate).valueOf() : undefined
const resolutionTime = resolutionDate ? dayjs(resolutionDate).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 ||
(mustWaitForDailyFreeMarketStatus != 'loading' &&
!mustWaitForDailyFreeMarketStatus)) &&
// 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))
function weekFrom(date: string | dayjs.Dayjs | undefined) {
return dayjs(date).add(7, 'day').format('YYYY-MM-DDT23:59')
}
function setCloseDateInDays(days: number) {
setShowCalendar(days === 0)
const newCloseDate = dayjs().add(days, 'day').format('YYYY-MM-DDT23:59')
setCloseDate(newCloseDate)
}
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,
automaticResolution,
resolutionTime
})
)
await router.push(contractPath(result as Contract))
} catch (e) {
console.log('error creating contract', e)
}
}
const descriptionPlaceholder =
outcomeType === 'BINARY'
? `e.g. This question resolves to "YES" if they receive the majority of votes...`
: `e.g. I will choose the answer according to...`
if (!creator) return <>>
return (
{outcomeType === 'BINARY' && (
How likely is it to happen? setShowNumInput(!showNumInput)}
/>
{showNumInput && (
<>
setInitialProb(parseInt(e.target.value.substring(0, 2)))
}
/>
%
>
)}
e.stopPropagation()}
onChange={(e) =>
setResolutionDate(
dayjs(e.target.value).format('YYYY-MM-DDT23:59') || ''
)
}
min={Date.parse(closeDate??"")} // TODO: Fix: Market can be created with dates in the past
disabled={isSubmitting}
value={dayjs(resolutionDate).format('YYYY-MM-DD')}
/>