Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
fix: show faucet return error and reset captcha (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio committed Apr 6, 2023
1 parent 5ab985f commit 3790b73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 9 additions & 2 deletions packages/app/src/systems/Faucet/components/FaucetApp.tsx
@@ -1,4 +1,5 @@
import cx from "classnames";
import { useState } from "react";
import ReCAPTCHA from "react-google-recaptcha";

import { useCaptcha, useFaucet } from "../hooks";
Expand All @@ -16,7 +17,13 @@ export function FaucetApp({
isLoading,
onSuccess,
}: FaucetAppProps) {
const faucet = useFaucet({ onSuccess });
// We need to re-render the captcha widget when it faucet
// endpoint fails and need to retrigger the captcha.
const [version, setVersion] = useState(0);
const faucet = useFaucet({
onSuccess,
onError: () => setVersion(version + 1),
});
const captcha = useCaptcha();

return (
Expand All @@ -34,7 +41,7 @@ export function FaucetApp({
"is-hidden": captcha.isLoading,
})}
>
<ReCAPTCHA {...captcha.getProps()} />
<ReCAPTCHA key={version} {...captcha.getProps()} />
</div>
{captcha.isFailed && (
<div className="faucetCaptcha--error">
Expand Down
7 changes: 6 additions & 1 deletion packages/app/src/systems/Faucet/hooks/useFaucet.ts
Expand Up @@ -4,7 +4,7 @@ import { useMemo } from 'react';
import { useMutation, useQuery } from 'react-query';

import { FUEL_FAUCET_URL } from '~/config';
import { useBalances, useWallet } from '~/systems/Core';
import { handleError, useBalances, useWallet } from '~/systems/Core';
import type { Maybe } from '~/types';
import { Queries } from '~/types';

Expand All @@ -22,6 +22,7 @@ export async function fetchFaucet(input: RequestInit) {

type UseFaucetOpts = {
onSuccess?: () => void;
onError?: () => void;
};

export function useFaucet(opts: UseFaucetOpts = {}) {
Expand Down Expand Up @@ -49,6 +50,10 @@ export function useFaucet(opts: UseFaucetOpts = {}) {
await balances.refetch();
opts.onSuccess?.();
},
onError: (err) => {
handleError(err);
opts.onError?.();
},
}
);

Expand Down

0 comments on commit 3790b73

Please sign in to comment.