From a2344492a2139ed8e31f415844b215cff628b4dd Mon Sep 17 00:00:00 2001 From: James Grugett Date: Wed, 6 Apr 2022 12:32:57 -0500 Subject: [PATCH] Simplify useContractWithPreload --- web/hooks/use-contract.ts | 20 ++++--------------- web/pages/[username]/[contractSlug].tsx | 2 +- web/pages/embed/[username]/[contractSlug].tsx | 8 ++++---- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/web/hooks/use-contract.ts b/web/hooks/use-contract.ts index ceaa125b..2ccc651a 100644 --- a/web/hooks/use-contract.ts +++ b/web/hooks/use-contract.ts @@ -1,9 +1,5 @@ import { useEffect, useState } from 'react' -import { - Contract, - getContractFromSlug, - listenForContract, -} from '../lib/firebase/contracts' +import { Contract, listenForContract } from '../lib/firebase/contracts' export const useContract = (contractId: string) => { const [contract, setContract] = useState( @@ -17,21 +13,13 @@ export const useContract = (contractId: string) => { return contract } -export const useContractWithPreload = ( - slug: string, - initial: Contract | null -) => { +export const useContractWithPreload = (initial: Contract | null) => { const [contract, setContract] = useState(initial) - const [contractId, setContractId] = useState( - initial?.id - ) + const contractId = initial?.id useEffect(() => { if (contractId) return listenForContract(contractId, setContract) - - if (contractId !== null && slug) - getContractFromSlug(slug).then((c) => setContractId(c?.id || null)) - }, [contractId, slug]) + }, [contractId]) return contract } diff --git a/web/pages/[username]/[contractSlug].tsx b/web/pages/[username]/[contractSlug].tsx index 9fc67ac9..f3887605 100644 --- a/web/pages/[username]/[contractSlug].tsx +++ b/web/pages/[username]/[contractSlug].tsx @@ -96,7 +96,7 @@ export default function ContractPage(props: { } const user = useUser() - const contract = useContractWithPreload(props.slug, props.contract) + const contract = useContractWithPreload(props.contract) const { bets, comments } = props // Sort for now to see if bug is fixed. diff --git a/web/pages/embed/[username]/[contractSlug].tsx b/web/pages/embed/[username]/[contractSlug].tsx index c8d4b046..e4f10929 100644 --- a/web/pages/embed/[username]/[contractSlug].tsx +++ b/web/pages/embed/[username]/[contractSlug].tsx @@ -67,7 +67,7 @@ export default function ContractEmbedPage(props: { slug: '', } - const contract = useContractWithPreload(props.slug, props.contract) + const contract = useContractWithPreload(props.contract) const { bets } = props bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) @@ -98,13 +98,13 @@ function ContractEmbed(props: { contract: Contract; bets: Bet[] }) { return ( -
+
-
+