Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeesun Kim authored and Jeesun Kim committed Apr 23, 2024
1 parent 1e9f7eb commit 5df2ace
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
15 changes: 8 additions & 7 deletions src/app/(sidebar)/account/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function CreateAccount() {
resetQuery();
}, [resetQuery]);

const { error, isError, isLoading, isSuccess, refetch, isFetchedAfterMount } =
const { error, isError, isFetching, isLoading, isSuccess, refetch } =
useFriendBot({
network,
publicKey: account.publicKey!,
Expand Down Expand Up @@ -101,8 +101,11 @@ export default function CreateAccount() {
size="md"
disabled={!account.publicKey || isLoading}
variant="tertiary"
isLoading={isLoading}
onClick={() => refetch()}
isLoading={isLoading || isFetching}
onClick={() => {
resetQuery();
refetch();
}}
data-testid="fundAccount-button"
>
Fund account with Friendbot
Expand All @@ -124,16 +127,14 @@ export default function CreateAccount() {
</Card>

<SuccessMsg
isVisible={Boolean(
showAlert && isFetchedAfterMount && isSuccess && account.publicKey,
)}
isVisible={Boolean(showAlert && isSuccess && account.publicKey)}
onClose={() => {
setShowAlert(false);
}}
/>

<ErrorMsg
isVisible={Boolean(showAlert && isFetchedAfterMount && isError)}
isVisible={Boolean(showAlert && isError)}
errorMsg={error?.message}
onClose={() => {
setShowAlert(false);
Expand Down
23 changes: 15 additions & 8 deletions src/app/(sidebar)/account/fund/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ export default function FundAccount() {

const IS_TESTING_NETWORK = useIsTestingNetwork();

const { error, isError, isLoading, isSuccess, refetch, isFetchedAfterMount } =
useFriendBot({
network,
publicKey: generatedPublicKey,
});
const {
error,
isError,
isFetching,
isLoading,
isSuccess,
refetch,
isFetchedAfterMount,
} = useFriendBot({
network,
publicKey: generatedPublicKey,
});

useEffect(() => {
if (
Expand Down Expand Up @@ -89,8 +96,8 @@ export default function FundAccount() {
<Button
disabled={!generatedPublicKey || Boolean(inlineErrorMessage)}
size="md"
variant={isFetchedAfterMount && isError ? "error" : "secondary"}
isLoading={isLoading}
variant="secondary"
isLoading={isLoading || isFetching}
onClick={() => {
if (!inlineErrorMessage) {
refetch();
Expand All @@ -101,7 +108,7 @@ export default function FundAccount() {
</Button>

<Button
disabled={!account.publicKey || isLoading}
disabled={!account.publicKey || isLoading || isFetching}
size="md"
variant="tertiary"
onClick={() => {
Expand Down
18 changes: 14 additions & 4 deletions src/query/useFriendBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const useFriendBot = ({
network: Network | EmptyObj;
publicKey: string;
}) => {
const knownFriendbotURL =
network.id === "futurenet"
? "https://friendbot-futurenet.stellar.org"
: "https://friendbot.stellar.org";

const query = useQuery({
queryKey: ["friendBot"],
queryFn: async () => {
Expand All @@ -18,20 +23,25 @@ export const useFriendBot = ({
}

try {
const response = await fetch(
`${network.horizonUrl}/friendbot?addr=${publicKey}`,
);
const url =
network.id === "custom"
? `${network.horizonUrl}/friendbot`
: `${knownFriendbotURL}/`;
const response = await fetch(`${url}?addr=${publicKey}`);

if (!response.ok) {
const errorBody = await response.json();

console.log("errorBody: ", errorBody);
throw new Error("there was an error", { cause: errorBody });
}
return response;
} catch (e: any) {
if (e.cause.status === 0) {
throw new Error(`Unable to reach Friendbot server at ${network}`);
} else if (e.cause.detail.includes("createAccountAlreadyExist")) {
throw new Error(
`This account is already funded. Therefore, we are unable to fund ${shortenStellarAddress(publicKey)} on the test network.`,
);
} else {
throw new Error(
`Unable to fund ${shortenStellarAddress(publicKey)} on the test network. Details: ${e.cause.detail}`,
Expand Down
5 changes: 2 additions & 3 deletions tests/fundAccountPage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test.describe("[futurenet/testnet] Fund Account Page", () => {

// Mock the friendbot api call
await page.route(
"*/**/friendbot?addr=GDVOT2ALMUF3G54RBHNJUEV6LOAZCQQCARHEVNUPKGMVPWFC4PFN33QR",
"*/**/?addr=GDVOT2ALMUF3G54RBHNJUEV6LOAZCQQCARHEVNUPKGMVPWFC4PFN33QR",
async (route) => {
await route.fulfill({
status: 200,
Expand All @@ -118,7 +118,6 @@ test.describe("[futurenet/testnet] Fund Account Page", () => {

// Success <Alert/> is visible
const alertBox = page.getByText(/Successfully funded/);
console.log("alertBox: ", alertBox);
await expect(alertBox).toBeVisible();
});

Expand Down Expand Up @@ -160,7 +159,7 @@ test.describe("[futurenet/testnet] Fund Account Page", () => {

await responsePromise;

const alertBox = page.getByText(/Unable to fund/);
const alertBox = page.getByText(/This account is already funded/);
await expect(alertBox).toBeVisible();
});

Expand Down

0 comments on commit 5df2ace

Please sign in to comment.