Fix prefetching to not populate useless state (#827)
This commit is contained in:
parent
879d6fb2dd
commit
42548cea2a
|
@ -11,6 +11,7 @@ import {
|
||||||
listenForNewContracts,
|
listenForNewContracts,
|
||||||
getUserBetContractsQuery,
|
getUserBetContractsQuery,
|
||||||
} from 'web/lib/firebase/contracts'
|
} from 'web/lib/firebase/contracts'
|
||||||
|
import { QueryClient } from 'react-query'
|
||||||
|
|
||||||
export const useContracts = () => {
|
export const useContracts = () => {
|
||||||
const [contracts, setContracts] = useState<Contract[] | undefined>()
|
const [contracts, setContracts] = useState<Contract[] | undefined>()
|
||||||
|
@ -92,6 +93,13 @@ export const useUpdatedContracts = (contracts: Contract[] | undefined) => {
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const queryClient = new QueryClient()
|
||||||
|
|
||||||
|
export const prefetchUserBetContracts = (userId: string) =>
|
||||||
|
queryClient.prefetchQuery(['contracts', 'bets', userId], () =>
|
||||||
|
getUserBetContractsQuery(userId)
|
||||||
|
)
|
||||||
|
|
||||||
export const useUserBetContracts = (userId: string) => {
|
export const useUserBetContracts = (userId: string) => {
|
||||||
const result = useFirestoreQueryData(
|
const result = useFirestoreQueryData(
|
||||||
['contracts', 'bets', userId],
|
['contracts', 'bets', userId],
|
||||||
|
|
|
@ -1,11 +1,24 @@
|
||||||
|
import { QueryClient } from 'react-query'
|
||||||
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
||||||
import { DAY_MS, HOUR_MS } from 'common/util/time'
|
import { DAY_MS, HOUR_MS } from 'common/util/time'
|
||||||
import { getPortfolioHistoryQuery, Period } from 'web/lib/firebase/users'
|
import { getPortfolioHistoryQuery, Period } from 'web/lib/firebase/users'
|
||||||
|
|
||||||
export const usePortfolioHistory = (userId: string, period: Period) => {
|
const queryClient = new QueryClient()
|
||||||
const nowRounded = Math.round(Date.now() / HOUR_MS) * HOUR_MS
|
|
||||||
const cutoff = periodToCutoff(nowRounded, period).valueOf()
|
|
||||||
|
|
||||||
|
const getCutoff = (period: Period) => {
|
||||||
|
const nowRounded = Math.round(Date.now() / HOUR_MS) * HOUR_MS
|
||||||
|
return periodToCutoff(nowRounded, period).valueOf()
|
||||||
|
}
|
||||||
|
|
||||||
|
export const prefetchPortfolioHistory = (userId: string, period: Period) => {
|
||||||
|
const cutoff = getCutoff(period)
|
||||||
|
return queryClient.prefetchQuery(['portfolio-history', userId, cutoff], () =>
|
||||||
|
getPortfolioHistoryQuery(userId, cutoff)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const usePortfolioHistory = (userId: string, period: Period) => {
|
||||||
|
const cutoff = getCutoff(period)
|
||||||
const result = useFirestoreQueryData(
|
const result = useFirestoreQueryData(
|
||||||
['portfolio-history', userId, cutoff],
|
['portfolio-history', userId, cutoff],
|
||||||
getPortfolioHistoryQuery(userId, cutoff)
|
getPortfolioHistoryQuery(userId, cutoff)
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import { useUserBetContracts } from './use-contracts'
|
import { prefetchUserBetContracts } from './use-contracts'
|
||||||
import { usePortfolioHistory } from './use-portfolio-history'
|
import { prefetchPortfolioHistory } from './use-portfolio-history'
|
||||||
import { useUserBets } from './use-user-bets'
|
import { prefetchUserBets } from './use-user-bets'
|
||||||
|
|
||||||
export function usePrefetch(userId: string | undefined) {
|
export function usePrefetch(userId: string | undefined) {
|
||||||
const maybeUserId = userId ?? ''
|
const maybeUserId = userId ?? ''
|
||||||
|
|
||||||
useUserBets(maybeUserId)
|
prefetchUserBets(maybeUserId)
|
||||||
useUserBetContracts(maybeUserId)
|
prefetchUserBetContracts(maybeUserId)
|
||||||
usePortfolioHistory(maybeUserId, 'weekly')
|
prefetchPortfolioHistory(maybeUserId, 'weekly')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { QueryClient } from 'react-query'
|
||||||
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import {
|
import {
|
||||||
|
@ -6,6 +7,11 @@ import {
|
||||||
listenForUserContractBets,
|
listenForUserContractBets,
|
||||||
} from 'web/lib/firebase/bets'
|
} from 'web/lib/firebase/bets'
|
||||||
|
|
||||||
|
const queryClient = new QueryClient()
|
||||||
|
|
||||||
|
export const prefetchUserBets = (userId: string) =>
|
||||||
|
queryClient.prefetchQuery(['bets', userId], () => getUserBetsQuery(userId))
|
||||||
|
|
||||||
export const useUserBets = (userId: string) => {
|
export const useUserBets = (userId: string) => {
|
||||||
const result = useFirestoreQueryData(
|
const result = useFirestoreQueryData(
|
||||||
['bets', userId],
|
['bets', userId],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user