listen for updates

This commit is contained in:
mantikoros 2022-09-27 18:47:37 -04:00
parent 0e9577101f
commit b7f5c0bf89

View File

@ -5,7 +5,10 @@ import { useEffect, useState } from 'react'
import { getProbability } from 'common/calculate'
import { Contract, CPMMBinaryContract } from 'common/contract'
import { Customize, USAMap } from './usa-map'
import { getContractFromSlug } from 'web/lib/firebase/contracts'
import {
getContractFromSlug,
listenForContract,
} from 'web/lib/firebase/contracts'
export interface StateElectionMarket {
creatorUsername: string
@ -59,5 +62,24 @@ const useContracts = (slugs: string[]) => {
)
}, [slugs])
useEffect(() => {
if (contracts.some((c) => c === undefined)) return
// listen to contract updates
const unsubs = (contracts as Contract[]).map((c, i) =>
listenForContract(
c.id,
(newC) => newC && setContracts(setAt(contracts, i, newC))
)
)
return () => unsubs.forEach((u) => u())
}, [contracts])
return contracts
}
function setAt<T>(arr: T[], i: number, val: T) {
const newArr = [...arr]
newArr[i] = val
return newArr
}