Switch from onSnapshot to our listenForValues, which doesn't set with partial cached values
This commit is contained in:
parent
587e1e5d61
commit
d170e31f54
|
@ -2,7 +2,6 @@ import {
|
||||||
collection,
|
collection,
|
||||||
collectionGroup,
|
collectionGroup,
|
||||||
query,
|
query,
|
||||||
onSnapshot,
|
|
||||||
where,
|
where,
|
||||||
orderBy,
|
orderBy,
|
||||||
} from 'firebase/firestore'
|
} from 'firebase/firestore'
|
||||||
|
@ -11,7 +10,7 @@ import _ from 'lodash'
|
||||||
import { db } from './init'
|
import { db } from './init'
|
||||||
import { Bet } from '../../../common/bet'
|
import { Bet } from '../../../common/bet'
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
import { getValues } from './utils'
|
import { getValues, listenForValues } from './utils'
|
||||||
export type { Bet }
|
export type { Bet }
|
||||||
|
|
||||||
function getBetsCollection(contractId: string) {
|
function getBetsCollection(contractId: string) {
|
||||||
|
@ -51,11 +50,8 @@ export function listenForBets(
|
||||||
contractId: string,
|
contractId: string,
|
||||||
setBets: (bets: Bet[]) => void
|
setBets: (bets: Bet[]) => void
|
||||||
) {
|
) {
|
||||||
return onSnapshot(getBetsCollection(contractId), (snap) => {
|
return listenForValues<Bet>(getBetsCollection(contractId), (bets) => {
|
||||||
const bets = snap.docs.map((doc) => doc.data() as Bet)
|
|
||||||
|
|
||||||
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
||||||
|
|
||||||
setBets(bets)
|
setBets(bets)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -68,9 +64,7 @@ export function listenForUserBets(
|
||||||
collectionGroup(db, 'bets'),
|
collectionGroup(db, 'bets'),
|
||||||
where('userId', '==', userId)
|
where('userId', '==', userId)
|
||||||
)
|
)
|
||||||
|
return listenForValues<Bet>(userQuery, (bets) => {
|
||||||
return onSnapshot(userQuery, (snap) => {
|
|
||||||
const bets = snap.docs.map((doc) => doc.data() as Bet)
|
|
||||||
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
||||||
setBets(bets)
|
setBets(bets)
|
||||||
})
|
})
|
||||||
|
@ -88,5 +82,5 @@ export function withoutAnteBets(contract: Contract, bets?: Bet[]) {
|
||||||
return bets.slice(2)
|
return bets.slice(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
return bets ?? []
|
return bets?.filter((bet) => !bet.isAnte) ?? []
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import {
|
import {
|
||||||
doc,
|
doc,
|
||||||
collection,
|
collection,
|
||||||
onSnapshot,
|
|
||||||
setDoc,
|
setDoc,
|
||||||
query,
|
query,
|
||||||
collectionGroup,
|
collectionGroup,
|
||||||
|
@ -46,13 +45,13 @@ export function listenForComments(
|
||||||
contractId: string,
|
contractId: string,
|
||||||
setComments: (comments: Comment[]) => void
|
setComments: (comments: Comment[]) => void
|
||||||
) {
|
) {
|
||||||
return onSnapshot(getCommentsCollection(contractId), (snap) => {
|
return listenForValues<Comment>(
|
||||||
const comments = snap.docs.map((doc) => doc.data() as Comment)
|
getCommentsCollection(contractId),
|
||||||
|
(comments) => {
|
||||||
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
|
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
|
||||||
|
setComments(comments)
|
||||||
setComments(comments)
|
}
|
||||||
})
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a map of betId -> comment
|
// Return a map of betId -> comment
|
||||||
|
|
|
@ -8,7 +8,6 @@ import {
|
||||||
collection,
|
collection,
|
||||||
query,
|
query,
|
||||||
getDocs,
|
getDocs,
|
||||||
onSnapshot,
|
|
||||||
orderBy,
|
orderBy,
|
||||||
getDoc,
|
getDoc,
|
||||||
updateDoc,
|
updateDoc,
|
||||||
|
@ -116,9 +115,7 @@ export function listenForContracts(
|
||||||
setContracts: (contracts: Contract[]) => void
|
setContracts: (contracts: Contract[]) => void
|
||||||
) {
|
) {
|
||||||
const q = query(contractCollection, orderBy('createdTime', 'desc'))
|
const q = query(contractCollection, orderBy('createdTime', 'desc'))
|
||||||
return onSnapshot(q, (snap) => {
|
return listenForValues<Contract>(q, setContracts)
|
||||||
setContracts(snap.docs.map((doc) => doc.data() as Contract))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function listenForContract(
|
export function listenForContract(
|
||||||
|
|
|
@ -3,7 +3,6 @@ import {
|
||||||
doc,
|
doc,
|
||||||
setDoc,
|
setDoc,
|
||||||
getDoc,
|
getDoc,
|
||||||
onSnapshot,
|
|
||||||
collection,
|
collection,
|
||||||
query,
|
query,
|
||||||
where,
|
where,
|
||||||
|
@ -22,7 +21,7 @@ import {
|
||||||
import { app } from './init'
|
import { app } from './init'
|
||||||
import { PrivateUser, User } from '../../../common/user'
|
import { PrivateUser, User } from '../../../common/user'
|
||||||
import { createUser } from './api-call'
|
import { createUser } from './api-call'
|
||||||
import { getValues, listenForValues } from './utils'
|
import { getValues, listenForValue, listenForValues } from './utils'
|
||||||
export type { User }
|
export type { User }
|
||||||
|
|
||||||
const db = getFirestore(app)
|
const db = getFirestore(app)
|
||||||
|
@ -46,11 +45,12 @@ export async function setUser(userId: string, user: User) {
|
||||||
await setDoc(doc(db, 'users', userId), 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)
|
const userRef = doc(db, 'users', userId)
|
||||||
return onSnapshot(userRef, (userSnap) => {
|
return listenForValue<User>(userRef, setUser)
|
||||||
setUser(userSnap.data() as User)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const CACHED_USER_KEY = 'CACHED_USER_KEY'
|
const CACHED_USER_KEY = 'CACHED_USER_KEY'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user