| 2 | import { Button } from "./Button"; |
| 3 | |
| 4 | export const BirthdayForm = () => { |
| 5 | const [email, setEmail] = React.useState(""); |
| 6 | const [okToContact, setOkToContact] = React.useState(false); |
| 7 | const [sendingState, setSendingState] = useState< |
| 8 | "initial" | "sending" | "sent" | "failed" |
| 9 | >("initial"); |
| 10 | |
| 11 | const submit = () => { |
| 12 | setSendingState("sending"); |
| 13 | fetch("/api/attend-birthday", { |
| 14 | method: "POST", |
| 15 | body: JSON.stringify({ email, okToContact }), |
| 16 | headers: { |
| 17 | "Content-Type": "application/json", |
| 18 | }, |
| 19 | }).then(() => { |
| 20 | setSendingState("sent"); |
| 21 | alert( |
| 22 | "Thanks for RSVPing! We'll be in touch closer to the date. See you soon!" |
| 23 | ); |
| 24 | }); |
| 25 | }; |
| 26 | |
| 27 | return ( |
| 28 | <form |
| 29 | onSubmit={e => { |
| 30 | e.preventDefault(); |
| 31 | submit(); |
| 32 | }} |
| 33 | > |
| 34 | <label> |
| 35 | Your E-Mail Address |
| 36 | <input |
| 37 | value={email} |
| 38 | onChange={e => setEmail(e.target.value)} |
| 39 | type="email" |
| 40 | placeholder="lea@artemov.de" |
| 41 | /> |
| 42 | </label> |
| 43 | <label className="inline"> |
| 44 | <input |
| 45 | type="checkbox" |
| 46 | checked={okToContact} |
| 47 | onChange={() => setOkToContact(!okToContact)} |
| 48 | /> |
| 49 | I'm OK to be contacted closer to the date with more details. I'm |
| 50 | genuinely interested to come. |
| 51 | </label> |
| 52 | <Button |
| 53 | disabled={ |
| 54 | sendingState !== "initial" || |
| 55 | !( |
| 56 | okToContact && |
| 57 | /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email) |
| 58 | ) |
| 59 | } |
| 60 | > |
| 61 | {sendingState === "sent" |