File size: 1,865 Bytes
148f3c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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>
  )
}