1
- import { useState } from 'react' ;
1
+ import { createRef , useState } from 'react' ;
2
2
import { decode } from 'html-entities' ;
3
3
import Container from '../Container' ;
4
4
import newsletterStyles from '../../styles/Newsletter.module.scss' ;
5
+ import ReCAPTCHA from 'react-google-recaptcha' ;
6
+
7
+ const SITE_KEY = process . env . NEXT_PUBLIC_RECAPTCHA_SITE_KEY ;
5
8
6
9
const NewsletterForm = ( { status, message, onValidated } ) => {
7
10
const [ error , setError ] = useState ( null ) ;
8
- const [ email , setEmail ] = useState ( null ) ;
11
+ const [ name , setName ] = useState ( '' ) ;
12
+ const [ email , setEmail ] = useState ( '' ) ;
13
+ const recaptchaRef = createRef ( ) ;
14
+
15
+ const onReCAPTCHAChange = async captchaCode => {
16
+ // If the reCAPTCHA code is null or undefined indicating that
17
+ // the reCAPTCHA was expired then return early
18
+ if ( ! captchaCode ) {
19
+ return ;
20
+ }
21
+ try {
22
+ const response = await fetch ( '/api/register' , {
23
+ method : 'POST' ,
24
+ body : JSON . stringify ( { email, name, captcha : captchaCode } ) ,
25
+ headers : {
26
+ 'Content-Type' : 'application/json' ,
27
+ } ,
28
+ } ) ;
29
+ if ( response . ok ) {
30
+ // If the response is ok than show the success console.log
31
+ console . log ( 'Email registered successfully' ) ;
32
+ } else {
33
+ // Else throw an error with the message returned
34
+ const error = await response . json ( ) ;
35
+ throw new Error ( error . message ) ;
36
+ }
37
+ } catch ( error ) {
38
+ console . log ( error ?. message || 'Something went wrong' ) ;
39
+ } finally {
40
+ // Reset the reCAPTCHA when the request has failed or succeeded
41
+ recaptchaRef . current . reset ( ) ;
42
+ setEmail ( '' ) ;
43
+ setName ( '' ) ;
44
+ }
45
+ } ;
9
46
10
47
/**
11
48
* Handle form submit.
@@ -17,17 +54,24 @@ const NewsletterForm = ({ status, message, onValidated }) => {
17
54
18
55
setError ( null ) ;
19
56
57
+ if ( ! name ) {
58
+ setError ( 'Please enter a name' ) ;
59
+ return null ;
60
+ }
61
+
20
62
if ( ! email ) {
21
63
setError ( 'Please enter a valid email address' ) ;
22
64
return null ;
23
65
}
24
66
67
+ recaptchaRef . current . execute ( ) ;
68
+
25
69
const isFormValidated = onValidated ( { EMAIL : email } ) ;
26
70
27
71
event . target . reset ( ) ;
28
72
29
73
// On success return true
30
- return email && email . indexOf ( '@' ) > - 1 && isFormValidated ;
74
+ return name && email && email . indexOf ( '@' ) > - 1 && isFormValidated ;
31
75
} ;
32
76
33
77
/**
@@ -77,22 +121,36 @@ const NewsletterForm = ({ status, message, onValidated }) => {
77
121
>
78
122
< input
79
123
className = { `${ newsletterStyles . newsletter__input } ${ newsletterStyles . newsletter__name } ` }
124
+ onChange = { event => setName ( event ?. target ?. value ?? '' ) }
80
125
type = "text"
81
126
name = "name"
127
+ value = { name }
82
128
placeholder = "name"
83
129
/>
84
130
< input
85
131
className = { `${ newsletterStyles . newsletter__input } ${ newsletterStyles . newsletter__email } ` }
86
132
onChange = { event => setEmail ( event ?. target ?. value ?? '' ) }
87
133
type = "email"
88
134
name = "email"
135
+ value = { email }
89
136
placeholder = "email"
90
137
onKeyUp = { event => handleInputKeyEvent ( event ) }
91
138
/>
92
- < button className = { newsletterStyles . newsletter__button } >
139
+ < button
140
+ className = { newsletterStyles . newsletter__button }
141
+ type = "submit"
142
+ >
93
143
Subscribe
94
144
</ button >
145
+
146
+ < ReCAPTCHA
147
+ ref = { recaptchaRef }
148
+ size = "invisible"
149
+ sitekey = { SITE_KEY }
150
+ onChange = { onReCAPTCHAChange }
151
+ />
95
152
</ form >
153
+
96
154
< div className = { newsletterStyles . newsletterFormInfo } >
97
155
{ status === 'sending' && (
98
156
< div className = { newsletterStyles . newsletterFormSending } >
0 commit comments