2021-12-09 22:05:55 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2021-12-17 23:16:42 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
getContractFromSlug,
|
|
|
|
listenForContract,
|
|
|
|
} from '../lib/firebase/contracts'
|
2021-12-09 22:05:55 +00:00
|
|
|
|
|
|
|
export const useContract = (contractId: string) => {
|
2021-12-09 23:23:21 +00:00
|
|
|
const [contract, setContract] = useState<Contract | null | 'loading'>(
|
|
|
|
'loading'
|
|
|
|
)
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2021-12-09 23:23:21 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (contractId) return listenForContract(contractId, setContract)
|
|
|
|
}, [contractId])
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2021-12-09 23:23:21 +00:00
|
|
|
return contract
|
|
|
|
}
|
2021-12-16 06:36:51 +00:00
|
|
|
|
|
|
|
export const useContractWithPreload = (
|
2021-12-17 23:16:42 +00:00
|
|
|
slug: string,
|
2021-12-16 06:36:51 +00:00
|
|
|
initial: Contract | null
|
|
|
|
) => {
|
|
|
|
const [contract, setContract] = useState<Contract | null>(initial)
|
2021-12-17 23:16:42 +00:00
|
|
|
const [contractId, setContractId] = useState<string | undefined | null>(
|
|
|
|
initial?.id
|
|
|
|
)
|
2021-12-16 06:36:51 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (contractId) return listenForContract(contractId, setContract)
|
2021-12-17 23:16:42 +00:00
|
|
|
|
|
|
|
if (contractId !== null)
|
|
|
|
getContractFromSlug(slug).then((c) => setContractId(c?.id || null))
|
|
|
|
}, [contractId, slug])
|
2021-12-16 06:36:51 +00:00
|
|
|
|
|
|
|
return contract
|
|
|
|
}
|