1
- import React from 'react'
1
+ import React , { useState , useEffect } from 'react'
2
2
import { Box , Input , Button , Flex } from '@theme-ui/components'
3
3
4
4
function SubscribeSection ( ) {
5
+ const [ email , setEmail ] = useState ( '' )
6
+ const [ tags , setTags ] = useState ( '3538617' )
7
+ const [ honeypot , setHoneypot ] = useState ( '' )
8
+ const [ isSubmitting , setIsSubmitting ] = useState ( false )
9
+ const [ successMessage , setSuccessMessage ] = useState ( '' )
10
+ const [ errorMessage , setErrorMessage ] = useState ( '' )
11
+
12
+ useEffect ( ( ) => {
13
+ if ( successMessage ) {
14
+ const timer = setTimeout ( ( ) => {
15
+ setSuccessMessage ( '' )
16
+ } , 5000 )
17
+ return ( ) => clearTimeout ( timer )
18
+ }
19
+ } , [ successMessage ] )
20
+
21
+ const handleSubmit = async e => {
22
+ e . preventDefault ( )
23
+
24
+ setSuccessMessage ( '' )
25
+ setErrorMessage ( '' )
26
+
27
+ if ( honeypot ) {
28
+ return
29
+ }
30
+
31
+ if ( ! email ) {
32
+ setErrorMessage ( 'Please enter a valid email address.' )
33
+ return
34
+ }
35
+
36
+ const emailRegex = / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ /
37
+ if ( ! emailRegex . test ( email ) ) {
38
+ setErrorMessage ( 'Please enter a valid email address.' )
39
+ return
40
+ }
41
+
42
+ setIsSubmitting ( true )
43
+
44
+ try {
45
+ const formData = new FormData ( )
46
+ formData . append ( 'EMAIL' , email )
47
+ formData . append ( 'tags' , tags )
48
+ formData . append ( 'b_7de8abe60497e4555ae20d817_b6c4b60e83' , honeypot )
49
+
50
+ await fetch (
51
+ 'https://datachain.us7.list-manage.com/subscribe/post?u=7de8abe60497e4555ae20d817&id=b6c4b60e83&f_id=00f09de0f0' ,
52
+ {
53
+ method : 'POST' ,
54
+ body : formData ,
55
+ mode : 'no-cors'
56
+ }
57
+ )
58
+ setSuccessMessage ( 'Thank you for subscribing!' )
59
+ setEmail ( '' )
60
+ } catch ( error ) {
61
+ setErrorMessage ( 'Something went wrong. Please try again.' )
62
+ } finally {
63
+ setIsSubmitting ( false )
64
+ }
65
+ }
66
+
5
67
return (
6
68
< Box
7
69
variant = "styles.invert"
@@ -28,11 +90,9 @@ function SubscribeSection() {
28
90
variant = "forms.ButtonInput"
29
91
as = "form"
30
92
sx = { { maxWidth : '460px' , mx : 'auto' } }
31
- action = "https://datachain.us7.list-manage.com/subscribe/post?u=7de8abe60497e4555ae20d817&id=b6c4b60e83&f_id=00f09de0f0"
32
- method = "post"
93
+ onSubmit = { handleSubmit }
33
94
id = "mc-embedded-subscribe-form"
34
95
name = "mc-embedded-subscribe-form"
35
- target = "_blank"
36
96
noValidate
37
97
>
38
98
< Input
@@ -41,11 +101,25 @@ function SubscribeSection() {
41
101
aria-label = "E-mail"
42
102
variant = "forms.ButtonInput.Input"
43
103
name = "EMAIL"
104
+ value = { email }
105
+ onChange = { e => setEmail ( e . target . value ) }
106
+ required
44
107
/>
45
108
< div hidden >
46
- < input type = "hidden" name = "tags" value = "3538617" />
109
+ < input
110
+ type = "hidden"
111
+ name = "tags"
112
+ value = { tags }
113
+ onChange = { e => setTags ( e . target . value ) }
114
+ />
47
115
</ div >
48
- < Button variant = "forms.ButtonInput.Button" > Subscribe</ Button >
116
+ < Button
117
+ type = "submit"
118
+ variant = "forms.ButtonInput.Button"
119
+ disabled = { isSubmitting }
120
+ >
121
+ { isSubmitting ? 'Subscribing...' : 'Subscribe' }
122
+ </ Button >
49
123
< div
50
124
style = { { position : 'absolute' , left : '-5000px' } }
51
125
aria-hidden = "true"
@@ -54,9 +128,37 @@ function SubscribeSection() {
54
128
type = "text"
55
129
name = "b_7de8abe60497e4555ae20d817_b6c4b60e83"
56
130
tabIndex = { - 1 }
131
+ value = { honeypot }
132
+ onChange = { e => setHoneypot ( e . target . value ) }
57
133
/>
58
134
</ div >
59
135
</ Flex >
136
+ { successMessage && (
137
+ < Box
138
+ sx = { {
139
+ color : '#4ade80' ,
140
+ textAlign : 'center' ,
141
+ mt : 3 ,
142
+ fontSize : '14px' ,
143
+ fontWeight : 500
144
+ } }
145
+ >
146
+ { successMessage }
147
+ </ Box >
148
+ ) }
149
+ { errorMessage && (
150
+ < Box
151
+ sx = { {
152
+ color : '#f87171' ,
153
+ textAlign : 'center' ,
154
+ mt : 3 ,
155
+ fontSize : '14px' ,
156
+ fontWeight : 500
157
+ } }
158
+ >
159
+ { errorMessage }
160
+ </ Box >
161
+ ) }
60
162
</ Box >
61
163
)
62
164
}
0 commit comments