manifold/web/components/resolution-panel.tsx

121 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2021-12-14 00:00:02 +00:00
import clsx from 'clsx'
import React, { useEffect, useState } from 'react'
2021-12-14 00:00:02 +00:00
import { Contract } from '../lib/firebase/contracts'
import { Col } from './layout/col'
import { Title } from './title'
import { User } from '../lib/firebase/users'
import { YesNoCancelSelector } from './yes-no-selector'
import { Spacer } from './layout/spacer'
import { ConfirmationButton as ConfirmationButton } from './confirmation-button'
import { resolveMarket } from '../lib/firebase/api-call'
2021-12-14 07:02:50 +00:00
2021-12-14 00:00:02 +00:00
export function ResolutionPanel(props: {
creator: User
contract: Contract
className?: string
}) {
useEffect(() => {
// warm up cloud function
resolveMarket({}).catch()
}, [])
const { contract, className } = props
2021-12-14 00:00:02 +00:00
const [outcome, setOutcome] = useState<
'YES' | 'NO' | 'MKT' | 'CANCEL' | undefined
>()
2021-12-14 00:00:02 +00:00
const [isSubmitting, setIsSubmitting] = useState(false)
const [error, setError] = useState<string | undefined>(undefined)
2021-12-14 07:02:50 +00:00
const resolve = async () => {
setIsSubmitting(true)
2021-12-18 23:40:39 +00:00
const result = await resolveMarket({
outcome,
contractId: contract.id,
}).then((r) => r.data as any)
console.log('resolved', outcome, 'result:', result)
if (result?.status !== 'success') {
setError(result?.error || 'Error resolving market')
}
setIsSubmitting(false)
}
const submitButtonClass =
outcome === 'YES'
? 'btn-primary'
: outcome === 'NO'
2021-12-18 23:40:39 +00:00
? 'bg-red-400 hover:bg-red-500'
: outcome === 'CANCEL'
? 'bg-yellow-400 hover:bg-yellow-500'
: outcome === 'MKT'
? 'bg-blue-400 hover:bg-blue-500'
2021-12-18 23:40:39 +00:00
: 'btn-disabled'
2021-12-14 00:00:02 +00:00
return (
2021-12-30 18:52:05 +00:00
<Col className={clsx('bg-white shadow-md px-8 py-6 rounded-md', className)}>
2021-12-14 00:00:02 +00:00
<Title className="mt-0" text="Your market" />
2021-12-15 09:11:27 +00:00
<div className="pt-2 pb-1 text-sm text-gray-400">Resolve outcome</div>
2021-12-14 00:00:02 +00:00
<YesNoCancelSelector
2021-12-15 09:11:27 +00:00
className="mx-auto my-2"
2021-12-14 00:00:02 +00:00
selected={outcome}
onSelect={setOutcome}
btnClassName={isSubmitting ? 'btn-disabled' : ''}
2021-12-14 00:00:02 +00:00
/>
<Spacer h={3} />
2021-12-15 09:11:27 +00:00
<div>
2021-12-14 00:00:02 +00:00
{outcome === 'YES' ? (
<>
2021-12-30 21:55:50 +00:00
Winnings will be paid out to YES bettors. You earn 1% of the pool.
2021-12-14 00:00:02 +00:00
</>
) : outcome === 'NO' ? (
2021-12-30 21:55:50 +00:00
<>Winnings will be paid out to NO bettors. You earn 1% of the pool.</>
2021-12-14 00:00:02 +00:00
) : outcome === 'CANCEL' ? (
2021-12-30 21:55:50 +00:00
<>The pool will be returned to traders with no fees.</>
) : outcome === 'MKT' ? (
<>
Traders will be paid out at the current implied probability. You
earn 1% of the pool.
</>
2021-12-14 00:00:02 +00:00
) : (
2021-12-30 21:55:50 +00:00
<>Resolving this market will immediately pay out traders.</>
2021-12-14 00:00:02 +00:00
)}
</div>
<Spacer h={3} />
2021-12-18 23:40:39 +00:00
{!!error && <div className="text-red-500">{error}</div>}
<ConfirmationButton
id="resolution-modal"
openModelBtn={{
2021-12-15 09:11:27 +00:00
className: clsx(
'border-none self-start mt-2 w-full',
submitButtonClass,
isSubmitting && 'btn-disabled loading'
2021-12-15 09:11:27 +00:00
),
label: 'Resolve',
}}
cancelBtn={{
label: 'Back',
}}
submitBtn={{
label: 'Resolve',
className: submitButtonClass,
}}
onSubmit={resolve}
2021-12-14 00:00:02 +00:00
>
<p>Are you sure you want to resolve this market?</p>
</ConfirmationButton>
2021-12-14 00:00:02 +00:00
</Col>
)
}