Skip to content

Commit 7a6a46e

Browse files
committed
feat: subscription form with validation and feedback
Added email validation, success/error messages, and improved form handling.
1 parent 65bef2a commit 7a6a46e

File tree

1 file changed

+108
-6
lines changed
  • src/components/molecules/SubscribeSection

1 file changed

+108
-6
lines changed

src/components/molecules/SubscribeSection/index.js

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,69 @@
1-
import React from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import { Box, Input, Button, Flex } from '@theme-ui/components'
33

44
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+
567
return (
668
<Box
769
variant="styles.invert"
@@ -28,11 +90,9 @@ function SubscribeSection() {
2890
variant="forms.ButtonInput"
2991
as="form"
3092
sx={{ maxWidth: '460px', mx: 'auto' }}
31-
action="https://datachain.us7.list-manage.com/subscribe/post?u=7de8abe60497e4555ae20d817&amp;id=b6c4b60e83&amp;f_id=00f09de0f0"
32-
method="post"
93+
onSubmit={handleSubmit}
3394
id="mc-embedded-subscribe-form"
3495
name="mc-embedded-subscribe-form"
35-
target="_blank"
3696
noValidate
3797
>
3898
<Input
@@ -41,11 +101,25 @@ function SubscribeSection() {
41101
aria-label="E-mail"
42102
variant="forms.ButtonInput.Input"
43103
name="EMAIL"
104+
value={email}
105+
onChange={e => setEmail(e.target.value)}
106+
required
44107
/>
45108
<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+
/>
47115
</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>
49123
<div
50124
style={{ position: 'absolute', left: '-5000px' }}
51125
aria-hidden="true"
@@ -54,9 +128,37 @@ function SubscribeSection() {
54128
type="text"
55129
name="b_7de8abe60497e4555ae20d817_b6c4b60e83"
56130
tabIndex={-1}
131+
value={honeypot}
132+
onChange={e => setHoneypot(e.target.value)}
57133
/>
58134
</div>
59135
</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+
)}
60162
</Box>
61163
)
62164
}

0 commit comments

Comments
 (0)