From b7f5c0bf890055a66e012edb23f896439594ca11 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Tue, 27 Sep 2022 18:47:37 -0400 Subject: [PATCH] listen for updates --- web/components/usa-map/state-election-map.tsx | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/web/components/usa-map/state-election-map.tsx b/web/components/usa-map/state-election-map.tsx index 6bbe26c7..8f7bb284 100644 --- a/web/components/usa-map/state-election-map.tsx +++ b/web/components/usa-map/state-election-map.tsx @@ -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(arr: T[], i: number, val: T) { + const newArr = [...arr] + newArr[i] = val + return newArr +}