2022-02-04 23:24:54 +00:00
|
|
|
import _ from 'lodash'
|
2021-12-14 05:40:38 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-03-02 03:31:48 +00:00
|
|
|
import {
|
|
|
|
Bet,
|
|
|
|
listenForUserBets,
|
|
|
|
listenForUserContractBets,
|
|
|
|
} from '../lib/firebase/bets'
|
2021-12-14 05:40:38 +00:00
|
|
|
|
2022-02-04 00:13:04 +00:00
|
|
|
export const useUserBets = (userId: string | undefined) => {
|
|
|
|
const [bets, setBets] = useState<Bet[] | undefined>(undefined)
|
2021-12-14 05:40:38 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-02-04 00:13:04 +00:00
|
|
|
if (userId) return listenForUserBets(userId, setBets)
|
2021-12-14 05:40:38 +00:00
|
|
|
}, [userId])
|
|
|
|
|
|
|
|
return bets
|
|
|
|
}
|
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-02-04 23:24:54 +00:00
|
|
|
export const useUserBetContracts = (userId: string | undefined) => {
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [userId])
|
|
|
|
|
|
|
|
return contractIds
|
|
|
|
}
|
2022-04-09 23:10:58 +00:00
|
|
|
|
|
|
|
export const useGetUserBetContractIds = (userId: string | undefined) => {
|
|
|
|
const [contractIds, setContractIds] = useState<string[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const key = `user-bet-contractIds-${userId}`
|
|
|
|
const userBetContractJson = localStorage.getItem(key)
|
|
|
|
if (userBetContractJson) {
|
|
|
|
setContractIds(JSON.parse(userBetContractJson))
|
|
|
|
}
|
|
|
|
}, [userId])
|
|
|
|
|
|
|
|
return contractIds
|
|
|
|
}
|