2022-09-05 23:09:01 +00:00
|
|
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
2022-09-16 21:35:03 +00:00
|
|
|
import { MINUTE_MS } from 'common/util/time'
|
|
|
|
import { useQueryClient } from 'react-query'
|
2022-09-05 23:09:01 +00:00
|
|
|
import {
|
|
|
|
getProbChangesNegative,
|
|
|
|
getProbChangesPositive,
|
|
|
|
} from 'web/lib/firebase/contracts'
|
2022-09-16 21:35:03 +00:00
|
|
|
import { getValues } from 'web/lib/firebase/utils'
|
2022-09-05 23:09:01 +00:00
|
|
|
|
|
|
|
export const useProbChanges = (userId: string) => {
|
|
|
|
const { data: positiveChanges } = useFirestoreQueryData(
|
|
|
|
['prob-changes-day-positive', userId],
|
|
|
|
getProbChangesPositive(userId)
|
|
|
|
)
|
|
|
|
const { data: negativeChanges } = useFirestoreQueryData(
|
|
|
|
['prob-changes-day-negative', userId],
|
|
|
|
getProbChangesNegative(userId)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!positiveChanges || !negativeChanges) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return { positiveChanges, negativeChanges }
|
|
|
|
}
|
2022-09-16 21:35:03 +00:00
|
|
|
|
|
|
|
export const usePrefetchProbChanges = (userId: string | undefined) => {
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
if (userId) {
|
|
|
|
queryClient.prefetchQuery(
|
|
|
|
['prob-changes-day-positive', userId],
|
|
|
|
() => getValues(getProbChangesPositive(userId)),
|
|
|
|
{ staleTime: MINUTE_MS }
|
|
|
|
)
|
|
|
|
queryClient.prefetchQuery(
|
|
|
|
['prob-changes-day-negative', userId],
|
|
|
|
() => getValues(getProbChangesNegative(userId)),
|
|
|
|
{ staleTime: MINUTE_MS }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|