Simplify useContractWithPreload

This commit is contained in:
James Grugett 2022-04-06 12:32:57 -05:00
parent dfa61be382
commit a2344492a2
3 changed files with 9 additions and 21 deletions

View File

@ -1,9 +1,5 @@
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { import { Contract, listenForContract } from '../lib/firebase/contracts'
Contract,
getContractFromSlug,
listenForContract,
} from '../lib/firebase/contracts'
export const useContract = (contractId: string) => { export const useContract = (contractId: string) => {
const [contract, setContract] = useState<Contract | null | 'loading'>( const [contract, setContract] = useState<Contract | null | 'loading'>(
@ -17,21 +13,13 @@ export const useContract = (contractId: string) => {
return contract return contract
} }
export const useContractWithPreload = ( export const useContractWithPreload = (initial: Contract | null) => {
slug: string,
initial: Contract | null
) => {
const [contract, setContract] = useState<Contract | null>(initial) const [contract, setContract] = useState<Contract | null>(initial)
const [contractId, setContractId] = useState<string | undefined | null>( const contractId = initial?.id
initial?.id
)
useEffect(() => { useEffect(() => {
if (contractId) return listenForContract(contractId, setContract) if (contractId) return listenForContract(contractId, setContract)
}, [contractId])
if (contractId !== null && slug)
getContractFromSlug(slug).then((c) => setContractId(c?.id || null))
}, [contractId, slug])
return contract return contract
} }

View File

@ -96,7 +96,7 @@ export default function ContractPage(props: {
} }
const user = useUser() const user = useUser()
const contract = useContractWithPreload(props.slug, props.contract) const contract = useContractWithPreload(props.contract)
const { bets, comments } = props const { bets, comments } = props
// Sort for now to see if bug is fixed. // Sort for now to see if bug is fixed.

View File

@ -67,7 +67,7 @@ export default function ContractEmbedPage(props: {
slug: '', slug: '',
} }
const contract = useContractWithPreload(props.slug, props.contract) const contract = useContractWithPreload(props.contract)
const { bets } = props const { bets } = props
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
@ -98,13 +98,13 @@ function ContractEmbed(props: { contract: Contract; bets: Bet[] }) {
return ( return (
<Col className="w-full flex-1 bg-white"> <Col className="w-full flex-1 bg-white">
<div className="flex flex-col relative pt-2" ref={setElem}> <div className="relative flex flex-col pt-2" ref={setElem}>
<SiteLink <SiteLink
className="absolute top-0 left-0 w-full h-full z-20" className="absolute top-0 left-0 z-20 h-full w-full"
href={href} href={href}
/> />
<div className="px-3 text-xl md:text-2xl text-indigo-700"> <div className="px-3 text-xl text-indigo-700 md:text-2xl">
<Linkify text={question} /> <Linkify text={question} />
</div> </div>