2021-12-10 14:56:17 +00:00
|
|
|
import React from 'react'
|
2021-12-09 21:31:02 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2021-12-09 22:05:55 +00:00
|
|
|
import { useContract } from '../../hooks/use-contract'
|
2021-12-10 14:56:17 +00:00
|
|
|
import { Header } from '../../components/header'
|
|
|
|
import { ContractOverview } from '../../components/contract-overview'
|
|
|
|
import { BetPanel } from '../../components/bet-panel'
|
2021-12-10 18:52:11 +00:00
|
|
|
import { Col } from '../../components/layout/col'
|
2021-12-14 00:00:02 +00:00
|
|
|
import { useUser } from '../../hooks/use-user'
|
|
|
|
import { ResolutionPanel } from '../../components/resolution-panel'
|
2021-12-14 18:51:30 +00:00
|
|
|
import clsx from 'clsx'
|
2021-12-16 04:30:24 +00:00
|
|
|
import { ContractBetsTable, MyBetsSummary } from '../../components/bets-list'
|
|
|
|
import { useBets } from '../../hooks/use-bets'
|
|
|
|
import { Title } from '../../components/title'
|
|
|
|
import { Spacer } from '../../components/layout/spacer'
|
|
|
|
import { Contract } from '../../lib/firebase/contracts'
|
|
|
|
import { User } from '../../lib/firebase/users'
|
2021-12-09 21:31:02 +00:00
|
|
|
|
|
|
|
export default function ContractPage() {
|
2021-12-14 00:00:02 +00:00
|
|
|
const user = useUser()
|
|
|
|
|
2021-12-09 22:05:55 +00:00
|
|
|
const router = useRouter()
|
|
|
|
const { contractId } = router.query as { contractId: string }
|
|
|
|
|
|
|
|
const contract = useContract(contractId)
|
|
|
|
|
|
|
|
if (contract === 'loading') {
|
2021-12-10 14:56:17 +00:00
|
|
|
return <div />
|
2021-12-09 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 14:56:17 +00:00
|
|
|
if (!contract) {
|
2021-12-09 22:05:55 +00:00
|
|
|
return <div>Contract not found...</div>
|
|
|
|
}
|
|
|
|
|
2021-12-14 06:12:25 +00:00
|
|
|
const { creatorId, isResolved } = contract
|
|
|
|
const isCreator = user?.id === creatorId
|
2021-12-14 00:00:02 +00:00
|
|
|
|
2021-12-09 22:05:55 +00:00
|
|
|
return (
|
2021-12-14 18:51:30 +00:00
|
|
|
<Col className="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
2021-12-10 14:56:17 +00:00
|
|
|
<Header />
|
|
|
|
|
2021-12-14 18:51:30 +00:00
|
|
|
<Col
|
|
|
|
className={clsx(
|
|
|
|
'w-full items-start md:flex-row mt-4',
|
|
|
|
isResolved ? 'md:justify-center' : 'md:justify-between'
|
|
|
|
)}
|
|
|
|
>
|
2021-12-14 06:12:25 +00:00
|
|
|
<ContractOverview
|
|
|
|
contract={contract}
|
|
|
|
className="max-w-4xl w-full p-4"
|
|
|
|
/>
|
2021-12-10 14:56:17 +00:00
|
|
|
|
2021-12-14 18:51:30 +00:00
|
|
|
{!isResolved && (
|
|
|
|
<>
|
|
|
|
<div className="mt-12 md:mt-0 md:ml-8" />
|
2021-12-10 18:52:11 +00:00
|
|
|
|
2021-12-16 04:30:24 +00:00
|
|
|
<Col className="w-full sm:w-auto">
|
2021-12-14 18:51:30 +00:00
|
|
|
<BetPanel contract={contract} />
|
2021-12-15 00:29:58 +00:00
|
|
|
|
|
|
|
{isCreator && (
|
|
|
|
<ResolutionPanel creator={user} contract={contract} />
|
|
|
|
)}
|
|
|
|
</Col>
|
2021-12-14 18:51:30 +00:00
|
|
|
</>
|
2021-12-14 00:00:02 +00:00
|
|
|
)}
|
2021-12-12 22:14:52 +00:00
|
|
|
</Col>
|
2021-12-16 04:30:24 +00:00
|
|
|
|
2021-12-16 07:00:35 +00:00
|
|
|
<BetsSection contract={contract} user={user ?? null} />
|
2021-12-14 18:51:30 +00:00
|
|
|
</Col>
|
2021-12-09 22:05:55 +00:00
|
|
|
)
|
|
|
|
}
|
2021-12-16 04:30:24 +00:00
|
|
|
|
|
|
|
function BetsSection(props: { contract: Contract; user: User | null }) {
|
|
|
|
const { contract, user } = props
|
|
|
|
const bets = useBets(contract.id)
|
|
|
|
|
|
|
|
if (bets === 'loading' || bets.length === 0) return <></>
|
|
|
|
|
2021-12-16 04:40:48 +00:00
|
|
|
// Decending creation time.
|
|
|
|
bets.sort((bet1, bet2) => bet2.createdTime - bet1.createdTime)
|
|
|
|
|
2021-12-16 04:30:24 +00:00
|
|
|
const userBets = user && bets.filter((bet) => bet.userId === user.id)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="p-4">
|
|
|
|
{userBets && userBets.length > 0 && (
|
|
|
|
<>
|
|
|
|
<Title text="My bets" />
|
|
|
|
<MyBetsSummary className="ml-1" contract={contract} bets={userBets} />
|
|
|
|
<Spacer h={6} />
|
|
|
|
<ContractBetsTable contract={contract} bets={userBets} />
|
|
|
|
<Spacer h={6} />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Title text="All bets" />
|
|
|
|
<ContractBetsTable contract={contract} bets={bets} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|