2022-09-02 02:38:09 +00:00
|
|
|
import { useQueryClient } from 'react-query'
|
2022-08-28 23:03:00 +00:00
|
|
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
2021-12-14 05:40:38 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-03-02 03:31:48 +00:00
|
|
|
import {
|
|
|
|
Bet,
|
2022-09-02 02:38:09 +00:00
|
|
|
getUserBets,
|
2022-08-28 23:03:00 +00:00
|
|
|
getUserBetsQuery,
|
2022-03-02 03:31:48 +00:00
|
|
|
listenForUserContractBets,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/lib/firebase/bets'
|
2022-09-23 20:30:44 +00:00
|
|
|
import { MINUTE_MS, sleep } from 'common/util/time'
|
2021-12-14 05:40:38 +00:00
|
|
|
|
2022-09-02 02:38:09 +00:00
|
|
|
export const usePrefetchUserBets = (userId: string) => {
|
|
|
|
const queryClient = useQueryClient()
|
2022-09-08 21:59:05 +00:00
|
|
|
return queryClient.prefetchQuery(
|
|
|
|
['bets', userId],
|
2022-09-23 20:30:44 +00:00
|
|
|
() => sleep(1000).then(() => getUserBets(userId)),
|
2022-09-08 21:59:05 +00:00
|
|
|
{ staleTime: MINUTE_MS }
|
|
|
|
)
|
2022-09-02 02:38:09 +00:00
|
|
|
}
|
2022-09-01 04:59:58 +00:00
|
|
|
|
2022-08-28 23:03:00 +00:00
|
|
|
export const useUserBets = (userId: string) => {
|
|
|
|
const result = useFirestoreQueryData(
|
|
|
|
['bets', userId],
|
2022-08-29 21:47:19 +00:00
|
|
|
getUserBetsQuery(userId)
|
2022-08-28 23:03:00 +00:00
|
|
|
)
|
|
|
|
return result.data
|
2021-12-14 05:40:38 +00:00
|
|
|
}
|
2022-02-04 23:24:54 +00:00
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
export const useUserContractBets = (
|
|
|
|
userId: string | undefined,
|
|
|
|
contractId: string | undefined
|
|
|
|
) => {
|
|
|
|
const [bets, setBets] = useState<Bet[] | undefined>(undefined)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (userId && contractId)
|
|
|
|
return listenForUserContractBets(userId, contractId, setBets)
|
|
|
|
}, [userId, contractId])
|
|
|
|
|
|
|
|
return bets
|
|
|
|
}
|
|
|
|
|
2022-04-09 23:10:58 +00:00
|
|
|
export const useGetUserBetContractIds = (userId: string | undefined) => {
|
2022-04-20 21:59:34 +00:00
|
|
|
const [contractIds, setContractIds] = useState<string[] | undefined>()
|
2022-04-09 23:10:58 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-04-20 21:59:34 +00:00
|
|
|
if (userId) {
|
|
|
|
const key = `user-bet-contractIds-${userId}`
|
|
|
|
const userBetContractJson = localStorage.getItem(key)
|
|
|
|
if (userBetContractJson) {
|
|
|
|
setContractIds(JSON.parse(userBetContractJson))
|
|
|
|
}
|
2022-04-09 23:10:58 +00:00
|
|
|
}
|
|
|
|
}, [userId])
|
|
|
|
|
|
|
|
return contractIds
|
|
|
|
}
|