1
- import { useMemo } from "react" ;
1
+ import { useMemo , useRef } from "react" ;
2
2
import { useCallback } from "react" ;
3
3
import { useState } from "react" ;
4
4
import {
@@ -19,23 +19,27 @@ import { validateEmail } from "../utils/helpers";
19
19
import { registerEmail } from "../utils/whitelist" ;
20
20
import { useLocation } from "react-router-dom" ;
21
21
import { sovrynLink } from "src/contracts/config" ;
22
+ import HCaptcha from "@hcaptcha/react-hcaptcha" ;
23
+ import { captchaSiteKey } from "src/utils" ;
22
24
23
25
export const WaitListSignup : React . FC = ( { children } ) => {
24
26
const [ isLoading , setIsLoading ] = useState ( false ) ;
27
+ const [ token , setToken ] = useState ( "" ) ;
25
28
const [ success , setSuccess ] = useState ( false ) ;
26
29
const [ sovrynMail , setSovrylMail ] = useState ( false ) ;
27
- const [ errorMessage , setErrorMessage ] = useState ( "" ) ;
30
+ const [ error , setError ] = useState ( "" ) ;
28
31
const [ email , setEmail ] = useState ( "" ) ;
32
+ const captchaRef = useRef < HCaptcha > ( null ) ;
29
33
30
34
const location = useLocation ( ) ;
31
35
32
36
const isValidEmail = useMemo ( ( ) => validateEmail ( email ) , [ email ] ) ;
33
37
34
38
const resetStatus = useCallback ( ( ) => {
35
- if ( ! errorMessage && ! success ) return ;
36
- setErrorMessage ( "" ) ;
39
+ if ( ! error && ! success ) return ;
40
+ setError ( "" ) ;
37
41
setSuccess ( false ) ;
38
- } , [ errorMessage , success ] ) ;
42
+ } , [ error , success ] ) ;
39
43
40
44
const handleEmailChange = useCallback (
41
45
( e : React . ChangeEvent < HTMLInputElement > ) => {
@@ -52,29 +56,44 @@ export const WaitListSignup: React.FC = ({ children }) => {
52
56
53
57
const ref = params . get ( "r" ) || "" ;
54
58
resetStatus ( ) ;
55
- if ( ! isValidEmail ) {
59
+ if ( ! isValidEmail || ! token ) {
56
60
return ;
57
61
}
58
62
try {
59
63
setIsLoading ( true ) ;
60
- await registerEmail ( email , ref , sovrynMail ) ;
64
+ await registerEmail ( email , ref , sovrynMail , token ) ;
61
65
62
66
setEmail ( "" ) ;
63
- setErrorMessage ( "" ) ;
67
+ setError ( "" ) ;
64
68
setSuccess ( true ) ;
65
69
setIsLoading ( false ) ;
66
70
} catch ( error : any ) {
67
71
if ( error ?. response ?. data ?. message ) {
68
- setErrorMessage ( error ?. response ?. data ?. message ) ;
72
+ setError ( error ?. response ?. data ?. message ) ;
69
73
} else {
70
- setErrorMessage ( "An error has occurred" ) ;
74
+ setError ( "An error has occurred" ) ;
71
75
}
72
76
setIsLoading ( false ) ;
73
77
}
78
+ setToken ( "" ) ;
79
+ captchaRef . current ?. resetCaptcha ( ) ;
74
80
} ,
75
- [ email , isValidEmail , location . search , resetStatus , sovrynMail ]
81
+ [ email , isValidEmail , location . search , resetStatus , sovrynMail , token ]
76
82
) ;
77
83
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
+
78
97
return (
79
98
< Box
80
99
sx = { {
@@ -169,6 +188,16 @@ export const WaitListSignup: React.FC = ({ children }) => {
169
188
/>
170
189
Add me to the general Sovryn mailing list
171
190
</ 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
+
172
201
< Button
173
202
sx = { {
174
203
width : 285 ,
@@ -177,13 +206,13 @@ export const WaitListSignup: React.FC = ({ children }) => {
177
206
alignItems : "center"
178
207
} }
179
208
variant = "secondary"
180
- disabled = { ! isValidEmail || isLoading }
209
+ disabled = { ! isValidEmail || isLoading || ! token }
181
210
data-action-id = "zero-landing-signUp"
182
211
>
183
212
Sign Up
184
213
{ isLoading && < Spinner sx = { { ml : 1 } } color = { "cardBackground" } size = { 24 } /> }
185
214
</ Button >
186
- { ( ( email && ! isValidEmail ) || errorMessage ) && (
215
+ { errorMessage && (
187
216
< Paragraph
188
217
sx = { {
189
218
fontSize : 1 ,
@@ -198,7 +227,7 @@ export const WaitListSignup: React.FC = ({ children }) => {
198
227
textAlign : "center"
199
228
} }
200
229
>
201
- { errorMessage ? errorMessage : "Please enter a valid email address." }
230
+ { errorMessage }
202
231
</ Paragraph >
203
232
) }
204
233
</ form >
0 commit comments