2021-12-14 01:07:28 +00:00
|
|
|
import router from 'next/router'
|
2021-12-16 03:58:28 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-16 02:31:14 +00:00
|
|
|
import { ContractsList } from '../components/contracts-list'
|
|
|
|
import { Header } from '../components/header'
|
|
|
|
import { Spacer } from '../components/layout/spacer'
|
|
|
|
import { Title } from '../components/title'
|
|
|
|
import { useUser } from '../hooks/use-user'
|
|
|
|
import { createContract } from '../lib/service/create-contract'
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
// Allow user to create a new contract
|
|
|
|
export default function NewContract() {
|
|
|
|
const creator = useUser()
|
|
|
|
|
2021-12-16 03:58:28 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!creator) router.push('/')
|
|
|
|
})
|
|
|
|
|
2021-12-14 01:07:28 +00:00
|
|
|
const [initialProb, setInitialProb] = useState(50)
|
|
|
|
const [question, setQuestion] = useState('')
|
|
|
|
const [description, setDescription] = useState('')
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
|
|
|
async function submit() {
|
|
|
|
// TODO: add more rigorous error handling for question, description
|
|
|
|
if (!creator || !question || !description) return
|
|
|
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2021-12-14 07:30:09 +00:00
|
|
|
const contract = await createContract(
|
|
|
|
question,
|
|
|
|
description,
|
|
|
|
initialProb,
|
|
|
|
creator
|
|
|
|
)
|
2021-12-14 01:07:28 +00:00
|
|
|
await router.push(`contract/${contract.id}`)
|
2021-12-10 17:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const descriptionPlaceholder = `e.g. This market will resolve to “Yes” if, by June 2, 2021, 11:59:59 PM ET, Paxlovid (also known under PF-07321332)...`
|
|
|
|
|
2021-12-16 03:58:28 +00:00
|
|
|
if (!creator) return <></>
|
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Header />
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-11 00:06:51 +00:00
|
|
|
<div className="max-w-4xl py-12 lg:mx-auto px-4">
|
2021-12-11 03:35:21 +00:00
|
|
|
<Title text="Create a new prediction market" />
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-14 04:32:33 +00:00
|
|
|
<div className="w-full bg-gray-100 rounded-lg shadow-xl px-6 py-4">
|
2021-12-10 17:54:16 +00:00
|
|
|
{/* Create a Tailwind form that takes in all the fields needed for a new contract */}
|
|
|
|
{/* When the form is submitted, create a new contract in the database */}
|
|
|
|
<form>
|
|
|
|
<div className="form-control">
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text">Question</span>
|
|
|
|
</label>
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="e.g. Will the FDA approve Paxlovid before Jun 2nd, 2022?"
|
|
|
|
className="input"
|
2021-12-14 01:07:28 +00:00
|
|
|
value={question}
|
2021-12-14 07:30:09 +00:00
|
|
|
onChange={(e) => setQuestion(e.target.value || '')}
|
2021-12-10 17:54:16 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2021-12-14 04:32:33 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
<div className="form-control">
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text">Description</span>
|
|
|
|
</label>
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
<textarea
|
|
|
|
className="textarea h-24 textarea-bordered"
|
|
|
|
placeholder={descriptionPlaceholder}
|
2021-12-14 01:07:28 +00:00
|
|
|
value={description}
|
2021-12-14 07:30:09 +00:00
|
|
|
onChange={(e) => setDescription(e.target.value || '')}
|
2021-12-10 17:54:16 +00:00
|
|
|
></textarea>
|
|
|
|
</div>
|
|
|
|
|
2021-12-14 04:32:33 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
2021-12-14 01:07:28 +00:00
|
|
|
<div className="form-control">
|
|
|
|
<label className="label">
|
2021-12-14 07:30:09 +00:00
|
|
|
<span className="label-text">
|
|
|
|
Initial probability: {initialProb}%
|
|
|
|
</span>
|
2021-12-14 01:07:28 +00:00
|
|
|
</label>
|
2021-12-10 17:54:16 +00:00
|
|
|
|
2021-12-14 01:07:28 +00:00
|
|
|
<input
|
|
|
|
type="range"
|
2021-12-14 08:35:56 +00:00
|
|
|
className="range range-lg range-primary"
|
2021-12-14 01:07:28 +00:00
|
|
|
min="1"
|
|
|
|
max={99}
|
|
|
|
value={initialProb}
|
2021-12-14 07:30:09 +00:00
|
|
|
onChange={(e) => setInitialProb(parseInt(e.target.value))}
|
2021-12-14 01:07:28 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2021-12-10 17:54:16 +00:00
|
|
|
|
2021-12-14 04:32:33 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="flex justify-end my-4">
|
2021-12-10 17:54:16 +00:00
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-primary"
|
2021-12-14 01:07:28 +00:00
|
|
|
disabled={isSubmitting}
|
2021-12-10 17:54:16 +00:00
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault()
|
2021-12-14 01:07:28 +00:00
|
|
|
submit()
|
2021-12-10 17:54:16 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
Create market
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
|
2021-12-11 00:06:51 +00:00
|
|
|
<Spacer h={10} />
|
|
|
|
|
2021-12-11 03:35:21 +00:00
|
|
|
<Title text="Your markets" />
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-16 01:34:36 +00:00
|
|
|
<ContractsList creator={creator} />
|
2021-12-10 17:54:16 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|