Convert useUserBets to react query
This commit is contained in:
parent
9c15d5b96c
commit
354d435b47
|
@ -72,7 +72,7 @@ export function BetsList(props: { user: User }) {
|
||||||
const signedInUser = useUser()
|
const signedInUser = useUser()
|
||||||
const isYourBets = user.id === signedInUser?.id
|
const isYourBets = user.id === signedInUser?.id
|
||||||
const hideBetsBefore = isYourBets ? 0 : JUNE_1_2022
|
const hideBetsBefore = isYourBets ? 0 : JUNE_1_2022
|
||||||
const userBets = useUserBets(user.id, { includeRedemptions: true })
|
const userBets = useUserBets(user.id)
|
||||||
const [contractsById, setContractsById] = useState<
|
const [contractsById, setContractsById] = useState<
|
||||||
Dictionary<Contract> | undefined
|
Dictionary<Contract> | undefined
|
||||||
>()
|
>()
|
||||||
|
@ -80,7 +80,10 @@ export function BetsList(props: { user: User }) {
|
||||||
// Hide bets before 06-01-2022 if this isn't your own profile
|
// Hide bets before 06-01-2022 if this isn't your own profile
|
||||||
// NOTE: This means public profits also begin on 06-01-2022 as well.
|
// NOTE: This means public profits also begin on 06-01-2022 as well.
|
||||||
const bets = useMemo(
|
const bets = useMemo(
|
||||||
() => userBets?.filter((bet) => bet.createdTime >= (hideBetsBefore ?? 0)),
|
() =>
|
||||||
|
userBets?.filter(
|
||||||
|
(bet) => !bet.isAnte && bet.createdTime >= (hideBetsBefore ?? 0)
|
||||||
|
),
|
||||||
[userBets, hideBetsBefore]
|
[userBets, hideBetsBefore]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,21 @@
|
||||||
import { uniq } from 'lodash'
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import {
|
import {
|
||||||
Bet,
|
Bet,
|
||||||
listenForUserBets,
|
getUserBetsQuery,
|
||||||
listenForUserContractBets,
|
listenForUserContractBets,
|
||||||
} from 'web/lib/firebase/bets'
|
} from 'web/lib/firebase/bets'
|
||||||
|
|
||||||
export const useUserBets = (
|
export const useUserBets = (userId: string) => {
|
||||||
userId: string | undefined,
|
const result = useFirestoreQueryData(
|
||||||
options: { includeRedemptions: boolean }
|
['bets', userId],
|
||||||
) => {
|
getUserBetsQuery(userId),
|
||||||
const [bets, setBets] = useState<Bet[] | undefined>(undefined)
|
{ subscribe: true, includeMetadataChanges: true },
|
||||||
|
// Temporary workaround for react-query bug:
|
||||||
useEffect(() => {
|
// https://github.com/invertase/react-query-firebase/issues/25
|
||||||
if (userId) return listenForUserBets(userId, setBets, options)
|
{ cacheTime: 0 }
|
||||||
}, [userId])
|
)
|
||||||
|
return result.data
|
||||||
return bets
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useUserContractBets = (
|
export const useUserContractBets = (
|
||||||
|
@ -33,36 +32,6 @@ export const useUserContractBets = (
|
||||||
return bets
|
return bets
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useUserBetContracts = (
|
|
||||||
userId: string | undefined,
|
|
||||||
options: { includeRedemptions: boolean }
|
|
||||||
) => {
|
|
||||||
const [contractIds, setContractIds] = useState<string[] | undefined>()
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (userId) {
|
|
||||||
const key = `user-bet-contractIds-${userId}`
|
|
||||||
|
|
||||||
const userBetContractJson = localStorage.getItem(key)
|
|
||||||
if (userBetContractJson) {
|
|
||||||
setContractIds(JSON.parse(userBetContractJson))
|
|
||||||
}
|
|
||||||
|
|
||||||
return listenForUserBets(
|
|
||||||
userId,
|
|
||||||
(bets) => {
|
|
||||||
const contractIds = uniq(bets.map((bet) => bet.contractId))
|
|
||||||
setContractIds(contractIds)
|
|
||||||
localStorage.setItem(key, JSON.stringify(contractIds))
|
|
||||||
},
|
|
||||||
options
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}, [userId])
|
|
||||||
|
|
||||||
return contractIds
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useGetUserBetContractIds = (userId: string | undefined) => {
|
export const useGetUserBetContractIds = (userId: string | undefined) => {
|
||||||
const [contractIds, setContractIds] = useState<string[] | undefined>()
|
const [contractIds, setContractIds] = useState<string[] | undefined>()
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import {
|
||||||
getDocs,
|
getDocs,
|
||||||
getDoc,
|
getDoc,
|
||||||
DocumentSnapshot,
|
DocumentSnapshot,
|
||||||
|
Query,
|
||||||
} from 'firebase/firestore'
|
} from 'firebase/firestore'
|
||||||
import { uniq } from 'lodash'
|
import { uniq } from 'lodash'
|
||||||
|
|
||||||
|
@ -131,24 +132,12 @@ export async function getContractsOfUserBets(userId: string) {
|
||||||
return filterDefined(contracts)
|
return filterDefined(contracts)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function listenForUserBets(
|
export function getUserBetsQuery(userId: string) {
|
||||||
userId: string,
|
return query(
|
||||||
setBets: (bets: Bet[]) => void,
|
|
||||||
options: { includeRedemptions: boolean }
|
|
||||||
) {
|
|
||||||
const { includeRedemptions } = options
|
|
||||||
const userQuery = query(
|
|
||||||
collectionGroup(db, 'bets'),
|
collectionGroup(db, 'bets'),
|
||||||
where('userId', '==', userId),
|
where('userId', '==', userId),
|
||||||
orderBy('createdTime', 'desc')
|
orderBy('createdTime', 'desc')
|
||||||
)
|
) as Query<Bet>
|
||||||
return listenForValues<Bet>(userQuery, (bets) => {
|
|
||||||
setBets(
|
|
||||||
bets.filter(
|
|
||||||
(bet) => (includeRedemptions || !bet.isRedemption) && !bet.isAnte
|
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function listenForUserContractBets(
|
export function listenForUserContractBets(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user