1- import { useMemo } from "react" ;
1+ import { useMemo , useRef } from "react" ;
22import { useCallback } from "react" ;
33import { useState } from "react" ;
44import {
@@ -19,23 +19,27 @@ import { validateEmail } from "../utils/helpers";
1919import { registerEmail } from "../utils/whitelist" ;
2020import { useLocation } from "react-router-dom" ;
2121import { sovrynLink } from "src/contracts/config" ;
22+ import HCaptcha from "@hcaptcha/react-hcaptcha" ;
23+ import { captchaSiteKey } from "src/utils" ;
2224
2325export const WaitListSignup : React . FC = ( { children } ) => {
2426 const [ isLoading , setIsLoading ] = useState ( false ) ;
27+ const [ token , setToken ] = useState ( "" ) ;
2528 const [ success , setSuccess ] = useState ( false ) ;
2629 const [ sovrynMail , setSovrylMail ] = useState ( false ) ;
27- const [ errorMessage , setErrorMessage ] = useState ( "" ) ;
30+ const [ error , setError ] = useState ( "" ) ;
2831 const [ email , setEmail ] = useState ( "" ) ;
32+ const captchaRef = useRef < HCaptcha > ( null ) ;
2933
3034 const location = useLocation ( ) ;
3135
3236 const isValidEmail = useMemo ( ( ) => validateEmail ( email ) , [ email ] ) ;
3337
3438 const resetStatus = useCallback ( ( ) => {
35- if ( ! errorMessage && ! success ) return ;
36- setErrorMessage ( "" ) ;
39+ if ( ! error && ! success ) return ;
40+ setError ( "" ) ;
3741 setSuccess ( false ) ;
38- } , [ errorMessage , success ] ) ;
42+ } , [ error , success ] ) ;
3943
4044 const handleEmailChange = useCallback (
4145 ( e : React . ChangeEvent < HTMLInputElement > ) => {
@@ -52,29 +56,44 @@ export const WaitListSignup: React.FC = ({ children }) => {
5256
5357 const ref = params . get ( "r" ) || "" ;
5458 resetStatus ( ) ;
55- if ( ! isValidEmail ) {
59+ if ( ! isValidEmail || ! token ) {
5660 return ;
5761 }
5862 try {
5963 setIsLoading ( true ) ;
60- await registerEmail ( email , ref , sovrynMail ) ;
64+ await registerEmail ( email , ref , sovrynMail , token ) ;
6165
6266 setEmail ( "" ) ;
63- setErrorMessage ( "" ) ;
67+ setError ( "" ) ;
6468 setSuccess ( true ) ;
6569 setIsLoading ( false ) ;
6670 } catch ( error : any ) {
6771 if ( error ?. response ?. data ?. message ) {
68- setErrorMessage ( error ?. response ?. data ?. message ) ;
72+ setError ( error ?. response ?. data ?. message ) ;
6973 } else {
70- setErrorMessage ( "An error has occurred" ) ;
74+ setError ( "An error has occurred" ) ;
7175 }
7276 setIsLoading ( false ) ;
7377 }
78+ setToken ( "" ) ;
79+ captchaRef . current ?. resetCaptcha ( ) ;
7480 } ,
75- [ email , isValidEmail , location . search , resetStatus , sovrynMail ]
81+ [ email , isValidEmail , location . search , resetStatus , sovrynMail , token ]
7682 ) ;
7783
84+ const errorMessage = useMemo ( ( ) => {
85+ if ( error ) {
86+ return error ;
87+ }
88+ if ( email && ! isValidEmail ) {
89+ return "Please enter a valid email address." ;
90+ }
91+ if ( email && ! token ) {
92+ return "Please compelete recaptcha" ;
93+ }
94+ return null ;
95+ } , [ email , error , isValidEmail , token ] ) ;
96+
7897 return (
7998 < Box
8099 sx = { {
@@ -169,6 +188,16 @@ export const WaitListSignup: React.FC = ({ children }) => {
169188 />
170189 Add me to the general Sovryn mailing list
171190 </ Label >
191+
192+ < Box sx = { { my : 1 } } >
193+ < HCaptcha
194+ theme = "dark"
195+ sitekey = { captchaSiteKey || "" }
196+ onVerify = { setToken }
197+ ref = { captchaRef }
198+ />
199+ </ Box >
200+
172201 < Button
173202 sx = { {
174203 width : 285 ,
@@ -177,13 +206,13 @@ export const WaitListSignup: React.FC = ({ children }) => {
177206 alignItems : "center"
178207 } }
179208 variant = "secondary"
180- disabled = { ! isValidEmail || isLoading }
209+ disabled = { ! isValidEmail || isLoading || ! token }
181210 data-action-id = "zero-landing-signUp"
182211 >
183212 Sign Up
184213 { isLoading && < Spinner sx = { { ml : 1 } } color = { "cardBackground" } size = { 24 } /> }
185214 </ Button >
186- { ( ( email && ! isValidEmail ) || errorMessage ) && (
215+ { errorMessage && (
187216 < Paragraph
188217 sx = { {
189218 fontSize : 1 ,
@@ -198,7 +227,7 @@ export const WaitListSignup: React.FC = ({ children }) => {
198227 textAlign : "center"
199228 } }
200229 >
201- { errorMessage ? errorMessage : "Please enter a valid email address." }
230+ { errorMessage }
202231 </ Paragraph >
203232 ) }
204233 </ form >
0 commit comments