-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailForm.tsx
49 lines (42 loc) · 1.34 KB
/
EmailForm.tsx
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
import { SubscribeButton } from "@/components/SubscribeButton";
import { type FormEvent, useCallback, useRef } from "react";
export const EmailForm = () => {
const email = useRef<HTMLInputElement>(null);
const onSubmit = useCallback(
(event: FormEvent) => {
event.preventDefault();
if (!email.current) return;
const { value } = email.current;
if (!/^\S+@\S+\.\S+$/.test(value)) return alert("錯誤的 Email");
fetch("https://pobtd17gf3.execute-api.us-east-1.amazonaws.com/Prod", {
method: "POST",
body: JSON.stringify({
email: email.current.value,
}),
})
.then((response) => {
if (!response.ok)
alert(`訂閱電子報發生了錯誤: ${response.statusText}`);
else alert("成功訂閱電子報");
})
.catch((e) => {
alert(`送出請求時發生了錯誤: ${e}`);
});
},
[email.current],
);
return (
<form className="flex w-full gap-4" onSubmit={onSubmit}>
<label className="flex-grow">
<input
ref={email}
className="block h-full w-full rounded-lg bg-gray-200 px-4 outline-gray-700 placeholder:text-sm"
type="email"
required
placeholder="電子郵件"
/>
</label>
<SubscribeButton type="submit" />
</form>
);
};