Webdev-Bench / tasks /c20 /repo /src /components /NewsletterForm.jsx
Roy-DOU's picture
Add files using upload-large-folder tool
148f3c7 verified
Raw History Blame Contribute Delete
1.87 kB
import { useRef, useState } from 'react'
import recaptcha from '../assets/recaptcha.png'
export default function NewsletterForm({ variant }) {
const [email, setEmail] = useState('')
const [name, setName] = useState('')
const [accepted, setAccepted] = useState(false)
const [sent, setSent] = useState(false)
const [confirmedEmail, setConfirmedEmail] = useState('')
const submitCount = useRef(0)
function handleSubmit(e) {
e.preventDefault()
const requestedEmail = email
submitCount.current += 1
const delay = Math.max(400, 1200 - (submitCount.current - 1) * 800)
setTimeout(() => {
setSent(true)
setConfirmedEmail(requestedEmail)
}, delay)
}
return (
<form className={`newsletter-form ${variant}`} onSubmit={handleSubmit}>
<label className="accept-row">
<input
type="checkbox"
checked={accepted}
onChange={(e) => setAccepted(e.target.checked)}
/>
</label>
<p className="accept-text">
I accept the <a href="#">terms and conditions</a>
</p>
<input
className="text-input"
type="email"
placeholder="Email address*"
required
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<input
className="text-input"
type="text"
placeholder="Name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<button className="subscribe-btn" type="submit">
SUBSCRIBE
</button>
{sent && (
<p className="form-feedback">
Bedankt voor je aanmelding! We hebben je ingeschreven op{' '}
<strong>{confirmedEmail}</strong>.
</p>
)}
<img
className="recaptcha"
src={recaptcha}
alt="reCAPTCHA: I'm not a robot"
/>
</form>
)
}