-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscribe.js
54 lines (48 loc) · 1.66 KB
/
subscribe.js
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
document.getElementById("sub-btn").addEventListener("click", async () => {
const emailInput = document.getElementById("input-email");
const email = emailInput.value;
const feedback = document.getElementById("feedback");
const token = "ab448096-dfd4-4e36-a433-7141de8ce724";
feedback.style.display = "none";
feedback.textContent = "";
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
feedback.textContent = "Please enter a valid email address.";
feedback.style.display = "block";
return;
}
try {
feedback.textContent = "Subscribing... 😊😍😘";
feedback.style.color = "white";
feedback.style.display = "block";
const response = await fetch(
"https://inclusive-talks.vercel.app/api/trpc/createSubscribers",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email, token }),
}
);
console.log("body", response.body);
console.log("text", response.statusText);
if (response.ok) {
feedback.textContent =
"Subscription successful! You will get updates on your email";
feedback.style.color = "green";
feedback.style.display = "block";
emailInput.value = ""; // Clear the input field
} else {
const errorData = await response.json();
feedback.textContent = "Email already exist";
feedback.style.color = "red";
feedback.style.display = "block";
}
} catch (error) {
feedback.textContent =
"Network error. Please check your connection and try again.";
feedback.style.color = "red";
feedback.style.display = "block";
}
});