Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dApp: Points - implement claim function debounce #783

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dapp/src/hooks/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useCallback, useRef } from "react"

/**
* Debounce a function
* @param callback The function to debounce
* @param delay The delay in ms
* @returns A debounced function
*/
export default function useDebounce(callback: VoidFunction, delay = 250) {
const latestTimeout = useRef<NodeJS.Timeout>()

const memoizedCallback = useCallback(callback, [callback])

return () => {
if (latestTimeout.current) {
clearTimeout(latestTimeout.current)
}

latestTimeout.current = setTimeout(() => {
memoizedCallback()
}, delay)
}
}
5 changes: 4 additions & 1 deletion dapp/src/pages/DashboardPage/AcrePointsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useAcrePoints } from "#/hooks"
import Spinner from "#/components/shared/Spinner"
import { IconInfoCircle } from "@tabler/icons-react"
import UserDataSkeleton from "#/components/shared/UserDataSkeleton"
import useDebounce from "#/hooks/useDebounce"

export default function AcrePointsCard(props: CardProps) {
const {
Expand All @@ -30,6 +31,8 @@ export default function AcrePointsCard(props: CardProps) {
isCalculationInProgress,
} = useAcrePoints()

const debouncedClaimPoints = useDebounce(claimPoints, 1000)

const formattedTotalPointsAmount = numberToLocaleString(totalBalance)
const formattedClaimablePointsAmount = numberToLocaleString(claimableBalance)

Expand Down Expand Up @@ -101,7 +104,7 @@ export default function AcrePointsCard(props: CardProps) {
{claimableBalance && (
<Button
mt={3}
onClick={claimPoints}
onClick={debouncedClaimPoints}
w="full"
colorScheme="green"
color="gold.200"
Expand Down
Loading