From 81746103f5c5e92b72c6065ea44de6297bc6ad27 Mon Sep 17 00:00:00 2001 From: jeremy-babylonchain Date: Thu, 24 Oct 2024 08:43:33 +0900 Subject: [PATCH] add terms acceptance --- src/app/api/postTerms.ts | 28 +++++++++++++++++++ src/app/components/Modals/ErrorModal.tsx | 4 +++ src/app/components/Points/StakerPoints.tsx | 2 -- src/app/page.tsx | 32 ++++++++++++++++++++++ src/app/types/errors.ts | 1 + 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/app/api/postTerms.ts diff --git a/src/app/api/postTerms.ts b/src/app/api/postTerms.ts new file mode 100644 index 00000000..91baa76d --- /dev/null +++ b/src/app/api/postTerms.ts @@ -0,0 +1,28 @@ +import { apiWrapper } from "./apiWrapper"; + +interface TermsPayload { + address: string; + termsAccepted: boolean; + publicKey: string; +} + +export const postTerms = async ( + address: string, + termsAccepted: boolean, + publicKey: string, +) => { + const payload: TermsPayload = { + address: address, + termsAccepted: termsAccepted, + publicKey: publicKey, + }; + + const response = await apiWrapper( + "POST", + "/terms_acceptance", + "Error submitting terms acceptance request", + payload, + ); + + return response.status === 200; +}; diff --git a/src/app/components/Modals/ErrorModal.tsx b/src/app/components/Modals/ErrorModal.tsx index 62c91fc7..2505ed69 100644 --- a/src/app/components/Modals/ErrorModal.tsx +++ b/src/app/components/Modals/ErrorModal.tsx @@ -54,6 +54,8 @@ export const ErrorModal: React.FC = ({ return "Stake Error"; case ErrorState.UNBONDING: return "Unbonding Error"; + case ErrorState.TERMS: + return "Terms Error"; default: return "Unknown Error"; } @@ -71,6 +73,8 @@ export const ErrorModal: React.FC = ({ return `Failed to stake due to: ${errorMessage}`; case ErrorState.WALLET: return `Failed to switch network due to: ${errorMessage}`; + case ErrorState.TERMS: + return `Failed to accept terms due to: ${errorMessage}`; default: return errorMessage; } diff --git a/src/app/components/Points/StakerPoints.tsx b/src/app/components/Points/StakerPoints.tsx index 4767ebe7..b2bfb2cb 100644 --- a/src/app/components/Points/StakerPoints.tsx +++ b/src/app/components/Points/StakerPoints.tsx @@ -31,8 +31,6 @@ export const StakerPoints: React.FC = ({ const points = stakerPoints?.[0]?.points; - console.log(points !== undefined || points !== 0); - return (

diff --git a/src/app/page.tsx b/src/app/page.tsx index 02db8f43..97b7445d 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -21,6 +21,7 @@ import { Network, WalletProvider } from "@/utils/wallet/wallet_provider"; import { getDelegations, PaginatedDelegations } from "./api/getDelegations"; import { getGlobalParams } from "./api/getGlobalParams"; +import { postTerms } from "./api/postTerms"; import { UTXO_KEY } from "./common/constants"; import { signPsbtTransaction } from "./common/utils/psbt"; import { Delegations } from "./components/Delegations/Delegations"; @@ -191,6 +192,8 @@ const Home: React.FC = () => { const [delegationsLocalStorage, setDelegationsLocalStorage] = useLocalStorage< Delegation[] >(delegationsLocalStorageKey, []); + const [termsAcceptedLocalStorage, setTermsAcceptedLocalStorage] = + useLocalStorage("termsAccepted", false); const [connectModalOpen, setConnectModalOpen] = useState(false); @@ -204,6 +207,9 @@ const Home: React.FC = () => { setBTCWalletNetwork(undefined); setPublicKeyNoCoord(""); setAddress(""); + if (termsAcceptedLocalStorage) { + setTermsAcceptedLocalStorage(false); + } }; const handleConnectBTC = useCallback( @@ -314,6 +320,32 @@ const Home: React.FC = () => { 0, ); + useEffect(() => { + const acceptTerms = async () => { + if (address && publicKeyNoCoord && !termsAcceptedLocalStorage) { + try { + await postTerms(address, true, publicKeyNoCoord); + setTermsAcceptedLocalStorage(true); + } catch (error: Error | any) { + handleError({ + error: error, + hasError: true, + errorState: ErrorState.TERMS, + refetchFunction: () => acceptTerms(), + }); + } + } + }; + + acceptTerms(); + }, [ + address, + publicKeyNoCoord, + termsAcceptedLocalStorage, + setTermsAcceptedLocalStorage, + handleError, + ]); + return (