Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Claim hooks 4 exclude expired claims (#2059)
Browse files Browse the repository at this point in the history
* Filtering out expired claims from useUserAvailableClaims result

* New hook useClassifiedUserClaims to get user claims, classified

Co-authored-by: Leandro <[email protected]>
  • Loading branch information
alfetopito and Leandro authored Jan 7, 2022
1 parent 6272c54 commit efc1af0
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions src/custom/state/claim/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,70 @@ type Account = string | null | undefined
export type UserClaims = UserClaimData[]
export type RepoClaims = RepoClaimData[]

type ClassifiedUserClaims = {
available: UserClaims
expired: UserClaims
claimed: UserClaims
}

/**
* Gets an array of available claim
* Gets all user claims, classified
*
* @param account
*/
export function useUserAvailableClaims(account: Account): UserClaims {
export function useClassifiedUserClaims(account: Account): ClassifiedUserClaims {
const userClaims = useUserClaims(account)
const contract = useVCowContract()

const isInvestmentStillAvailable = useInvestmentStillAvailable()
const isAirdropStillAvailable = useAirdropStillAvailable()

// build list of parameters, with the claim index
// we check for all claims because expired now might have been claimed before
const claimIndexes = useMemo(() => userClaims?.map(({ index }) => [index]) || [], [userClaims])

// just a note, this line returns an empty array on Mainet but works on Rinkeby
// So not sure if this is in plan to be implemented or it doesn't work currently
const results = useSingleContractMultipleData(contract, 'isClaimed', claimIndexes)

return useMemo(() => {
const available: UserClaims = []
const expired: UserClaims = []
const claimed: UserClaims = []

if (!userClaims || userClaims.length === 0) {
// user has no claims
return []
return { available, expired, claimed }
}

return results.reduce<UserClaims>((acc, result, index) => {
results.forEach((result, index) => {
const claim = userClaims[index]

if (
result.valid && // result is valid
!result.loading && // result is not loading
result.result?.[0] === false // result is false, meaning not claimed
result.result?.[0] === true // result true means claimed
) {
acc.push(userClaims[index]) // get the claim not yet claimed
claimed.push(claim)
} else if (!isAirdropStillAvailable || (!isInvestmentStillAvailable && PAID_CLAIM_TYPES.includes(claim.type))) {
expired.push(claim)
} else {
available.push(claim)
}
return acc
}, [])
}, [results, userClaims])
})

return { available, expired, claimed }
}, [isAirdropStillAvailable, isInvestmentStillAvailable, results, userClaims])
}

/**
* Gets an array of available claims
*
* Syntactic sugar on top of `useClassifiedUserClaims`
*
* @param account
*/
export function useUserAvailableClaims(account: Account): UserClaims {
const { available } = useClassifiedUserClaims(account)

return available
}

/**
Expand Down

0 comments on commit efc1af0

Please sign in to comment.