2021-12-14 01:29:02 +00:00
|
|
|
import dayjs from 'dayjs'
|
2021-12-10 17:54:16 +00:00
|
|
|
import Link from 'next/link'
|
2021-12-14 01:07:28 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { useUser } from '../hooks/use-user'
|
|
|
|
import { Contract, deleteContract, listContracts } from '../lib/firebase/contracts'
|
2021-12-10 17:54:16 +00:00
|
|
|
|
|
|
|
function ContractCard(props: { contract: Contract }) {
|
|
|
|
const { contract } = props
|
2021-12-14 01:23:57 +00:00
|
|
|
|
|
|
|
// only show delete button if there's not bets
|
|
|
|
const showDelete = contract.pot.YES === contract.seedAmounts.YES
|
|
|
|
&& contract.pot.NO === contract.seedAmounts.NO
|
|
|
|
|
|
|
|
const [isDeleted, setIsDeleted] = useState(false) // temporary fix until we stream changes
|
|
|
|
|
|
|
|
if (isDeleted)
|
|
|
|
return <></>
|
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
return (
|
|
|
|
<li>
|
|
|
|
<Link href={`/contract/${contract.id}`}>
|
2021-12-14 01:29:02 +00:00
|
|
|
<a className="block hover:bg-gray-200">
|
2021-12-10 17:54:16 +00:00
|
|
|
<div className="px-4 py-4 sm:px-6">
|
|
|
|
<div className="flex items-center justify-between">
|
2021-12-14 01:29:02 +00:00
|
|
|
<p className="text-sm font-medium text-indigo-700">
|
2021-12-10 17:54:16 +00:00
|
|
|
{contract.question}
|
|
|
|
</p>
|
|
|
|
</div>
|
2021-12-14 01:29:02 +00:00
|
|
|
<div className="mt-2 sm:flex sm:flex-col sm:justify-between text-gray-600">
|
2021-12-14 01:23:57 +00:00
|
|
|
{/* <div className="sm:flex">
|
2021-12-14 01:29:02 +00:00
|
|
|
<p className="flex items-center text-sm">{contract.id}</p>
|
|
|
|
<p className="mt-2 flex items-center text-sm">
|
2021-12-10 17:54:16 +00:00
|
|
|
{contract.description}
|
|
|
|
</p>
|
2021-12-14 01:23:57 +00:00
|
|
|
</div> */}
|
2021-12-14 01:29:02 +00:00
|
|
|
<div className="mt-2 flex flex-row items-center justify-between text-sm">
|
2021-12-10 17:54:16 +00:00
|
|
|
<p>
|
|
|
|
<time dateTime={`${contract.createdTime}`}>
|
2021-12-14 01:29:02 +00:00
|
|
|
{dayjs(contract.createdTime).format('MMM D')}
|
2021-12-10 17:54:16 +00:00
|
|
|
</time>
|
|
|
|
</p>
|
2021-12-14 01:23:57 +00:00
|
|
|
|
|
|
|
{showDelete &&
|
|
|
|
<button
|
|
|
|
className="btn btn-xs btn-error btn-outline ml-2"
|
|
|
|
onClick={async e => {
|
|
|
|
e.preventDefault()
|
|
|
|
await deleteContract(contract.id)
|
|
|
|
setIsDeleted(true)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</button>
|
|
|
|
}
|
2021-12-10 17:54:16 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-14 01:07:28 +00:00
|
|
|
export function ContractsList(props: {}) {
|
|
|
|
const creator = useUser()
|
|
|
|
|
|
|
|
const [contracts, setContracts] = useState<Contract[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (creator?.id) {
|
2021-12-14 01:23:57 +00:00
|
|
|
// TODO: stream changes from firestore
|
2021-12-14 01:07:28 +00:00
|
|
|
listContracts(creator.id).then(setContracts)
|
|
|
|
}
|
|
|
|
}, [creator])
|
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
return (
|
2021-12-14 01:29:02 +00:00
|
|
|
<div className="bg-gray-100 shadow-xl overflow-hidden sm:rounded-md max-w-4xl w-full">
|
|
|
|
<ul role="list" className="divide-y divide-gray-200">
|
2021-12-10 17:54:16 +00:00
|
|
|
{contracts.map((contract) => (
|
|
|
|
<ContractCard contract={contract} key={contract.id} />
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|