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

fix: updated switch button added debounce #325

Merged
merged 1 commit into from
May 16, 2024
Merged
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
15 changes: 9 additions & 6 deletions src/routes/popup/purchase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import { PageType, trackPage } from "~utils/analytics";
import type { PaymentType, Quote } from "~lib/onramper";
import { useHistory } from "~utils/hash_router";
import { ExtensionStorage } from "~utils/storage";
import { useDebounce } from "~wallets/hooks";

export default function Purchase() {
const [push] = useHistory();
const youPayInput = useInput();
const debouncedYouPayInput = useDebounce(youPayInput.state, 300);
const [arConversion, setArConversion] = useState<boolean>(false);
const [loading, setLoading] = useState(false);
const [currencies, setCurrencies] = useState<any[]>([]);
Expand Down Expand Up @@ -77,7 +79,8 @@ export default function Purchase() {
setLoading(true);
setQuote(null);
if (
Number(youPayInput.state) <= 0 ||
Number(debouncedYouPayInput) <= 0 ||
debouncedYouPayInput === "" ||
!selectedCurrency ||
!paymentMethod
) {
Expand All @@ -95,9 +98,9 @@ export default function Purchase() {
paymentMethod: paymentMethod.id
});
if (arConversion) {
params.append("cryptoAmount", youPayInput.state);
params.append("cryptoAmount", debouncedYouPayInput);
} else {
params.append("fiatAmount", youPayInput.state);
params.append("fiatAmount", debouncedYouPayInput);
}

const url = `${baseUrl}?${params.toString()}`;
Expand All @@ -119,12 +122,12 @@ export default function Purchase() {
setLoading(false);
};

if (youPayInput.state) {
if (debouncedYouPayInput) {
fetchQuote();
} else {
setQuote(null);
}
}, [youPayInput.state, selectedCurrency, paymentMethod, arConversion]);
}, [debouncedYouPayInput, selectedCurrency, paymentMethod, arConversion]);

return (
<>
Expand Down Expand Up @@ -164,7 +167,7 @@ export default function Purchase() {
</SwitchText>
</Switch>
<InputButton
disabled={arConversion}
disabled={!arConversion}
label={
arConversion
? browser.i18n.getMessage("buy_screen_pay")
Expand Down
21 changes: 20 additions & 1 deletion src/wallets/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { WalletInterface } from "~components/welcome/load/Migrate";
import type { JWKInterface } from "arweave/web/lib/wallet";
import { type AnsUser, getAnsProfile } from "~lib/ans";
import { useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import { useStorage } from "@plasmohq/storage/hook";
import { defaultGateway } from "~gateways/gateway";
import { ExtensionStorage } from "~utils/storage";
Expand Down Expand Up @@ -128,3 +128,22 @@ export function useBalance() {

return balance;
}

export function useDebounce<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = useState(value);
const handler = useRef<ReturnType<typeof setTimeout>>();

useEffect(() => {
handler.current = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
if (handler.current) {
clearTimeout(handler.current);
}
};
}, [value, delay]);

return debouncedValue;
}