From d170e31f54c03537b7dd09aa6b1f820bb065068d Mon Sep 17 00:00:00 2001 From: James Grugett Date: Thu, 3 Feb 2022 17:30:40 -0600 Subject: [PATCH] Switch from onSnapshot to our listenForValues, which doesn't set with partial cached values --- web/lib/firebase/bets.ts | 14 ++++---------- web/lib/firebase/comments.ts | 15 +++++++-------- web/lib/firebase/contracts.ts | 5 +---- web/lib/firebase/users.ts | 12 ++++++------ 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/web/lib/firebase/bets.ts b/web/lib/firebase/bets.ts index 5397e8fe..642c07c4 100644 --- a/web/lib/firebase/bets.ts +++ b/web/lib/firebase/bets.ts @@ -2,7 +2,6 @@ import { collection, collectionGroup, query, - onSnapshot, where, orderBy, } from 'firebase/firestore' @@ -11,7 +10,7 @@ import _ from 'lodash' import { db } from './init' import { Bet } from '../../../common/bet' import { Contract } from '../../../common/contract' -import { getValues } from './utils' +import { getValues, listenForValues } from './utils' export type { Bet } function getBetsCollection(contractId: string) { @@ -51,11 +50,8 @@ export function listenForBets( contractId: string, setBets: (bets: Bet[]) => void ) { - return onSnapshot(getBetsCollection(contractId), (snap) => { - const bets = snap.docs.map((doc) => doc.data() as Bet) - + return listenForValues(getBetsCollection(contractId), (bets) => { bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) - setBets(bets) }) } @@ -68,9 +64,7 @@ export function listenForUserBets( collectionGroup(db, 'bets'), where('userId', '==', userId) ) - - return onSnapshot(userQuery, (snap) => { - const bets = snap.docs.map((doc) => doc.data() as Bet) + return listenForValues(userQuery, (bets) => { bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) setBets(bets) }) @@ -88,5 +82,5 @@ export function withoutAnteBets(contract: Contract, bets?: Bet[]) { return bets.slice(2) } - return bets ?? [] + return bets?.filter((bet) => !bet.isAnte) ?? [] } diff --git a/web/lib/firebase/comments.ts b/web/lib/firebase/comments.ts index d272581e..7072a71a 100644 --- a/web/lib/firebase/comments.ts +++ b/web/lib/firebase/comments.ts @@ -1,7 +1,6 @@ import { doc, collection, - onSnapshot, setDoc, query, collectionGroup, @@ -46,13 +45,13 @@ export function listenForComments( contractId: string, setComments: (comments: Comment[]) => void ) { - return onSnapshot(getCommentsCollection(contractId), (snap) => { - const comments = snap.docs.map((doc) => doc.data() as Comment) - - comments.sort((c1, c2) => c1.createdTime - c2.createdTime) - - setComments(comments) - }) + return listenForValues( + getCommentsCollection(contractId), + (comments) => { + comments.sort((c1, c2) => c1.createdTime - c2.createdTime) + setComments(comments) + } + ) } // Return a map of betId -> comment diff --git a/web/lib/firebase/contracts.ts b/web/lib/firebase/contracts.ts index 4789d19f..666c7caf 100644 --- a/web/lib/firebase/contracts.ts +++ b/web/lib/firebase/contracts.ts @@ -8,7 +8,6 @@ import { collection, query, getDocs, - onSnapshot, orderBy, getDoc, updateDoc, @@ -116,9 +115,7 @@ export function listenForContracts( setContracts: (contracts: Contract[]) => void ) { const q = query(contractCollection, orderBy('createdTime', 'desc')) - return onSnapshot(q, (snap) => { - setContracts(snap.docs.map((doc) => doc.data() as Contract)) - }) + return listenForValues(q, setContracts) } export function listenForContract( diff --git a/web/lib/firebase/users.ts b/web/lib/firebase/users.ts index 4141e6c6..57969bb6 100644 --- a/web/lib/firebase/users.ts +++ b/web/lib/firebase/users.ts @@ -3,7 +3,6 @@ import { doc, setDoc, getDoc, - onSnapshot, collection, query, where, @@ -22,7 +21,7 @@ import { import { app } from './init' import { PrivateUser, User } from '../../../common/user' import { createUser } from './api-call' -import { getValues, listenForValues } from './utils' +import { getValues, listenForValue, listenForValues } from './utils' export type { User } const db = getFirestore(app) @@ -46,11 +45,12 @@ export async function setUser(userId: string, user: User) { await setDoc(doc(db, 'users', userId), user) } -export function listenForUser(userId: string, setUser: (user: User) => void) { +export function listenForUser( + userId: string, + setUser: (user: User | null) => void +) { const userRef = doc(db, 'users', userId) - return onSnapshot(userRef, (userSnap) => { - setUser(userSnap.data() as User) - }) + return listenForValue(userRef, setUser) } const CACHED_USER_KEY = 'CACHED_USER_KEY'