Add back listenForContract

This commit is contained in:
Austin Chen 2021-12-09 21:01:44 -08:00
parent 09b4639198
commit 4f2c8bcdce

View File

@ -8,6 +8,7 @@ import {
collection, collection,
query, query,
getDocs, getDocs,
onSnapshot,
} from 'firebase/firestore' } from 'firebase/firestore'
export type Contract = { export type Contract = {
@ -42,6 +43,7 @@ export type Bet = {
} }
const db = getFirestore(app) const db = getFirestore(app)
const contractCollection = collection(db, 'contracts')
// Push contract to Firestore // Push contract to Firestore
export async function setContract(contract: Contract) { export async function setContract(contract: Contract) {
@ -55,14 +57,23 @@ export async function deleteContract(contractId: string) {
} }
export async function listContracts(creatorId: string): Promise<Contract[]> { export async function listContracts(creatorId: string): Promise<Contract[]> {
const contractsRef = collection(db, 'contracts') const q = query(contractCollection, where('creatorId', '==', creatorId))
const q = query(contractsRef, where('creatorId', '==', creatorId))
const snapshot = await getDocs(q) const snapshot = await getDocs(q)
const contracts: Contract[] = [] const contracts: Contract[] = []
snapshot.forEach((doc) => contracts.push(doc.data() as Contract)) snapshot.forEach((doc) => contracts.push(doc.data() as Contract))
return contracts 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 // Push bet to Firestore
// TODO: Should bets be subcollections under its contract? // TODO: Should bets be subcollections under its contract?
export async function setBet(bet: Bet) { export async function setBet(bet: Bet) {