Switch from onSnapshot to our listenForValues, which doesn't set with partial cached values

This commit is contained in:
James Grugett 2022-02-03 17:30:40 -06:00
parent 587e1e5d61
commit d170e31f54
4 changed files with 18 additions and 28 deletions

View File

@ -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<Bet>(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<Bet>(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) ?? []
}

View File

@ -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)
return listenForValues<Comment>(
getCommentsCollection(contractId),
(comments) => {
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
setComments(comments)
})
}
)
}
// Return a map of betId -> comment

View File

@ -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<Contract>(q, setContracts)
}
export function listenForContract(

View File

@ -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<User>(userRef, setUser)
}
const CACHED_USER_KEY = 'CACHED_USER_KEY'