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 }) { export function BetsList(props: { user: User }) {
const { user } = props const { user } = props
const bets = useUserBets(user.id) const bets = useUserBets(user.id, { includeRedemptions: true })
const [contracts, setContracts] = useState<Contract[] | undefined>() const [contracts, setContracts] = useState<Contract[] | undefined>()
const [sort, setSort] = useState<BetSort>('newest') const [sort, setSort] = useState<BetSort>('newest')

View File

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

View File

@ -6,11 +6,14 @@ import {
listenForUserContractBets, listenForUserContractBets,
} from 'web/lib/firebase/bets' } 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) const [bets, setBets] = useState<Bet[] | undefined>(undefined)
useEffect(() => { useEffect(() => {
if (userId) return listenForUserBets(userId, setBets) if (userId) return listenForUserBets(userId, setBets, options)
}, [userId]) }, [userId])
return bets return bets
@ -30,7 +33,10 @@ export const useUserContractBets = (
return bets return bets
} }
export const useUserBetContracts = (userId: string | undefined) => { export const useUserBetContracts = (
userId: string | undefined,
options: { includeRedemptions: boolean }
) => {
const [contractIds, setContractIds] = useState<string[] | undefined>() const [contractIds, setContractIds] = useState<string[] | undefined>()
useEffect(() => { useEffect(() => {
@ -42,11 +48,15 @@ export const useUserBetContracts = (userId: string | undefined) => {
setContractIds(JSON.parse(userBetContractJson)) setContractIds(JSON.parse(userBetContractJson))
} }
return listenForUserBets(userId, (bets) => { return listenForUserBets(
const contractIds = _.uniq(bets.map((bet) => bet.contractId)) userId,
setContractIds(contractIds) (bets) => {
localStorage.setItem(key, JSON.stringify(contractIds)) const contractIds = _.uniq(bets.map((bet) => bet.contractId))
}) setContractIds(contractIds)
localStorage.setItem(key, JSON.stringify(contractIds))
},
options
)
} }
}, [userId]) }, [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>( return getValues<Bet>(
query(collectionGroup(db, 'bets'), where('userId', '==', userId)) query(collectionGroup(db, 'bets'), where('userId', '==', userId))
) )
.then((bets) =>
bets.filter(
(bet) => (includeRedemptions || !bet.isRedemption) && !bet.isAnte
)
)
.catch((reason) => reason)
} }
export function listenForUserBets( export function listenForUserBets(
userId: string, userId: string,
setBets: (bets: Bet[]) => void setBets: (bets: Bet[]) => void,
options: { includeRedemptions: boolean }
) { ) {
const { includeRedemptions } = options
const userQuery = query( const userQuery = query(
collectionGroup(db, 'bets'), collectionGroup(db, 'bets'),
where('userId', '==', userId) where('userId', '==', userId),
orderBy('createdTime', 'desc')
) )
return listenForValues<Bet>(userQuery, (bets) => { return listenForValues<Bet>(userQuery, (bets) => {
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) setBets(
setBets(bets) bets.filter(
(bet) => (includeRedemptions || !bet.isRedemption) && !bet.isAnte
)
)
}) })
} }