Remove unused bets (redemptions, antes) and sort by desc in query (#264)

* Remove unused bets and sort in query

* remove console

* Explicitly ignore or include redemptions

* Pass options from parent function

* Fix let=>const
This commit is contained in:
Ian Philips 2022-05-19 10:03:37 -06:00 committed by GitHub
parent 8013862f15
commit 7d8ccb78a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 16 deletions

View File

@ -45,8 +45,7 @@ type BetFilter = 'open' | 'closed' | 'resolved' | 'all'
export function BetsList(props: { user: User }) {
const { user } = props
const bets = useUserBets(user.id)
const bets = useUserBets(user.id, { includeRedemptions: true })
const [contracts, setContracts] = useState<Contract[] | undefined>()
const [sort, setSort] = useState<BetSort>('newest')

View File

@ -66,7 +66,7 @@ export function UserPage(props: {
if (!user) return
getUsersComments(user.id).then(setUsersComments)
listContracts(user.id).then(setUsersContracts)
getUserBets(user.id).then(setUsersBets)
getUserBets(user.id, { includeRedemptions: false }).then(setUsersBets)
}, [user])
useEffect(() => {

View File

@ -6,11 +6,14 @@ import {
listenForUserContractBets,
} from 'web/lib/firebase/bets'
export const useUserBets = (userId: string | undefined) => {
export const useUserBets = (
userId: string | undefined,
options: { includeRedemptions: boolean }
) => {
const [bets, setBets] = useState<Bet[] | undefined>(undefined)
useEffect(() => {
if (userId) return listenForUserBets(userId, setBets)
if (userId) return listenForUserBets(userId, setBets, options)
}, [userId])
return bets
@ -30,7 +33,10 @@ export const useUserContractBets = (
return bets
}
export const useUserBetContracts = (userId: string | undefined) => {
export const useUserBetContracts = (
userId: string | undefined,
options: { includeRedemptions: boolean }
) => {
const [contractIds, setContractIds] = useState<string[] | undefined>()
useEffect(() => {
@ -42,11 +48,15 @@ export const useUserBetContracts = (userId: string | undefined) => {
setContractIds(JSON.parse(userBetContractJson))
}
return listenForUserBets(userId, (bets) => {
const contractIds = _.uniq(bets.map((bet) => bet.contractId))
setContractIds(contractIds)
localStorage.setItem(key, JSON.stringify(contractIds))
})
return listenForUserBets(
userId,
(bets) => {
const contractIds = _.uniq(bets.map((bet) => bet.contractId))
setContractIds(contractIds)
localStorage.setItem(key, JSON.stringify(contractIds))
},
options
)
}
}, [userId])

View File

@ -60,23 +60,39 @@ export function listenForBets(
})
}
export async function getUserBets(userId: string) {
export async function getUserBets(
userId: string,
options: { includeRedemptions: boolean }
) {
const { includeRedemptions } = options
return getValues<Bet>(
query(collectionGroup(db, 'bets'), where('userId', '==', userId))
)
.then((bets) =>
bets.filter(
(bet) => (includeRedemptions || !bet.isRedemption) && !bet.isAnte
)
)
.catch((reason) => reason)
}
export function listenForUserBets(
userId: string,
setBets: (bets: Bet[]) => void
setBets: (bets: Bet[]) => void,
options: { includeRedemptions: boolean }
) {
const { includeRedemptions } = options
const userQuery = query(
collectionGroup(db, 'bets'),
where('userId', '==', userId)
where('userId', '==', userId),
orderBy('createdTime', 'desc')
)
return listenForValues<Bet>(userQuery, (bets) => {
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
setBets(bets)
setBets(
bets.filter(
(bet) => (includeRedemptions || !bet.isRedemption) && !bet.isAnte
)
)
})
}