diff --git a/src/app/(sidebar)/account/create/page.tsx b/src/app/(sidebar)/account/create/page.tsx index f2686ada..802a131c 100644 --- a/src/app/(sidebar)/account/create/page.tsx +++ b/src/app/(sidebar)/account/create/page.tsx @@ -1,29 +1,78 @@ "use client"; -import { useState } from "react"; -import { useRouter } from "next/navigation"; +import { useCallback, useEffect, useState } from "react"; import { Card, Text, Button } from "@stellar/design-system"; import { Keypair } from "@stellar/stellar-sdk"; -import { useIsTestingNetwork } from "@/hooks/useIsTestingNetwork"; -import { Routes } from "@/constants/routes"; import { useStore } from "@/store/useStore"; +import { useFriendBot } from "@/query/useFriendBot"; +import { useQueryClient } from "@tanstack/react-query"; + +import { useIsTestingNetwork } from "@/hooks/useIsTestingNetwork"; + import { GenerateKeypair } from "@/components/GenerateKeypair"; import { ExpandBox } from "@/components/ExpandBox"; +import { SuccessMsg } from "@/components/FriendBot/SuccessMsg"; +import { ErrorMsg } from "@/components/FriendBot/ErrorMsg"; import "../styles.scss"; export default function CreateAccount() { - const { account } = useStore(); - const router = useRouter(); + const { account, network } = useStore(); const [secretKey, setSecretKey] = useState(""); + const [showAlert, setShowAlert] = useState(false); + const queryClient = useQueryClient(); const IS_TESTING_NETWORK = useIsTestingNetwork(); + const IS_CUSTOM_NETWORK_WITH_HORIZON = + network.id === "custom" && network.horizonUrl; + + const resetQuery = useCallback( + () => + queryClient.resetQueries({ + queryKey: ["friendBot"], + }), + [queryClient], + ); + + const resetStates = useCallback(() => { + account.reset(); + resetQuery(); + }, [resetQuery]); + + const { error, isError, isFetching, isLoading, isSuccess, refetch } = + useFriendBot({ + network, + publicKey: account.publicKey!, + }); + + useEffect(() => { + if (isError || isSuccess) { + setShowAlert(true); + } + }, [isError, isSuccess]); + + useEffect(() => { + if ( + account.registeredNetwork?.id && + account.registeredNetwork.id !== network.id + ) { + resetStates(); + setShowAlert(false); + } + }, [account.registeredNetwork, network.id]); const generateKeypair = () => { + resetStates(); + const keypair = Keypair.random(); - account.updatePublicKey(keypair.publicKey()); + if (IS_TESTING_NETWORK) { + account.updateKeypair(keypair.publicKey(), keypair.secret()); + } else { + account.updateKeypair(keypair.publicKey()); + } + setSecretKey(keypair.secret()); }; @@ -43,30 +92,60 @@ export default function CreateAccount() {
- - {IS_TESTING_NETWORK ? ( + + {IS_TESTING_NETWORK || IS_CUSTOM_NETWORK_WITH_HORIZON ? ( ) : null}
- -
- -
-
+ {Boolean(account.publicKey) && ( + +
+ +
+
+ )} + + { + setShowAlert(false); + }} + /> + + { + setShowAlert(false); + }} + /> ); } diff --git a/src/app/(sidebar)/account/fund/page.tsx b/src/app/(sidebar)/account/fund/page.tsx index e2c38715..6e98b26f 100644 --- a/src/app/(sidebar)/account/fund/page.tsx +++ b/src/app/(sidebar)/account/fund/page.tsx @@ -1,17 +1,19 @@ "use client"; import { useEffect, useState } from "react"; -import { Alert, Card, Input, Text, Button } from "@stellar/design-system"; +import { Card, Input, Text, Button } from "@stellar/design-system"; import Link from "next/link"; import { Routes } from "@/constants/routes"; -import { shortenStellarAddress } from "@/helpers/shortenStellarAddress"; import { useIsTestingNetwork } from "@/hooks/useIsTestingNetwork"; import { useFriendBot } from "@/query/useFriendBot"; import { useStore } from "@/store/useStore"; import { validate } from "@/validate"; +import { SuccessMsg } from "@/components/FriendBot/SuccessMsg"; +import { ErrorMsg } from "@/components/FriendBot/ErrorMsg"; + import "../styles.scss"; export default function FundAccount() { @@ -23,11 +25,28 @@ export default function FundAccount() { const IS_TESTING_NETWORK = useIsTestingNetwork(); - const { error, isError, isLoading, isSuccess, refetch, isFetchedAfterMount } = - useFriendBot({ - network: network.id, - publicKey: generatedPublicKey, - }); + const { + error, + isError, + isFetching, + isLoading, + isSuccess, + refetch, + isFetchedAfterMount, + } = useFriendBot({ + network, + publicKey: generatedPublicKey, + }); + + useEffect(() => { + if ( + account.registeredNetwork?.id && + account.registeredNetwork.id !== network.id + ) { + account.reset(); + setShowAlert(false); + } + }, [account.registeredNetwork, network.id]); useEffect(() => { if (isError || isSuccess) { @@ -60,13 +79,12 @@ export default function FundAccount() { { setGeneratedPublicKey(e.target.value); - const error = validate.publicKey(e.target.value); setInlineErrorMessage(error || ""); }} @@ -78,8 +96,8 @@ export default function FundAccount() {