From 4f2c8bcdce1abf28a762e5b05a2418b89d12657f Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Thu, 9 Dec 2021 21:01:44 -0800 Subject: [PATCH] Add back listenForContract --- web/lib/firebase/contracts.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/web/lib/firebase/contracts.ts b/web/lib/firebase/contracts.ts index fddb4b3d..66ff599f 100644 --- a/web/lib/firebase/contracts.ts +++ b/web/lib/firebase/contracts.ts @@ -8,6 +8,7 @@ import { collection, query, getDocs, + onSnapshot, } from 'firebase/firestore' export type Contract = { @@ -42,6 +43,7 @@ export type Bet = { } const db = getFirestore(app) +const contractCollection = collection(db, 'contracts') // Push contract to Firestore export async function setContract(contract: Contract) { @@ -55,14 +57,23 @@ export async function deleteContract(contractId: string) { } export async function listContracts(creatorId: string): Promise { - const contractsRef = collection(db, 'contracts') - const q = query(contractsRef, where('creatorId', '==', creatorId)) + const q = query(contractCollection, where('creatorId', '==', creatorId)) const snapshot = await getDocs(q) const contracts: Contract[] = [] snapshot.forEach((doc) => contracts.push(doc.data() as Contract)) return contracts } +export function listenForContract( + contractId: string, + setContract: (contract: Contract) => void +) { + const contractRef = doc(contractCollection, contractId) + return onSnapshot(contractRef, (contractSnap) => { + setContract(contractSnap.data() as Contract) + }) +} + // Push bet to Firestore // TODO: Should bets be subcollections under its contract? export async function setBet(bet: Bet) {