-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
135 lines (110 loc) · 3.76 KB
/
Copy pathscript.js
File metadata and controls
135 lines (110 loc) · 3.76 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const GOOGLE_SCRIPT_URL = "https://script.google.com/macros/s/AKfycbzoZSTgqfS1xtpQDI9src2dWXn-7X2vZi_Zs-hw00tsw4W7Z9xZlYwJuPTa2HkXhjJM/exec";
const REGISTRATION_CLOSED = true;
const form = document.querySelector("#registrationForm");
const submitButton = form.querySelector(".submit-button");
const formStatus = document.querySelector("#formStatus");
const fieldErrors = {
name: document.querySelector('[data-error-for="name"]'),
phone: document.querySelector('[data-error-for="phone"]'),
email: document.querySelector('[data-error-for="email"]'),
isMember: document.querySelector('[data-error-for="isMember"]'),
};
const errorMessages = {
name: "請填寫姓名。",
phone: "請填寫電話或 WhatsApp。",
email: "請填寫有效 Email。",
isMember: "請選擇是否學會會員。",
};
function setLoading(isLoading) {
submitButton.disabled = isLoading;
submitButton.classList.toggle("is-loading", isLoading);
submitButton.querySelector(".button-text").textContent = isLoading ? "提交中..." : "提交報名";
}
function setStatus(message, type = "") {
formStatus.textContent = message;
formStatus.className = `form-status${type ? ` is-${type}` : ""}`;
}
function closeRegistrationForm() {
form.classList.add("is-closed");
Array.from(form.elements).forEach((element) => {
element.disabled = true;
});
submitButton.querySelector(".button-text").textContent = "報名已截止";
setStatus("名額已滿,報名已截止。", "error");
}
function getFormData() {
const formData = new FormData(form);
return {
name: String(formData.get("name") || "").trim(),
phone: String(formData.get("phone") || "").trim(),
email: String(formData.get("email") || "").trim(),
isMember: String(formData.get("isMember") || "").trim(),
};
}
function validate(data) {
const errors = {};
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!data.name) errors.name = errorMessages.name;
if (!data.phone) errors.phone = errorMessages.phone;
if (!emailPattern.test(data.email)) errors.email = errorMessages.email;
if (!data.isMember) errors.isMember = errorMessages.isMember;
return errors;
}
function showErrors(errors) {
Object.entries(fieldErrors).forEach(([field, element]) => {
element.textContent = errors[field] || "";
});
}
async function submitRegistration(data) {
if (GOOGLE_SCRIPT_URL.includes("PASTE_YOUR")) {
throw new Error("Google Apps Script URL is not configured.");
}
const response = await fetch(GOOGLE_SCRIPT_URL, {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "text/plain;charset=utf-8",
},
body: JSON.stringify(data),
});
const result = await response.json();
if (!response.ok || !result.ok) {
const error = new Error(result.error || "Submission failed.");
error.code = result.code;
throw error;
}
return result;
}
form.addEventListener("submit", async (event) => {
event.preventDefault();
if (REGISTRATION_CLOSED) {
setStatus("名額已滿,報名已截止。", "error");
return;
}
setStatus("");
const data = getFormData();
const errors = validate(data);
showErrors(errors);
if (Object.keys(errors).length > 0) {
setStatus("請先修正以上資料。", "error");
return;
}
try {
setLoading(true);
await submitRegistration(data);
form.reset();
setStatus("已成功提交報名,多謝你!活動後歡迎填寫意見問卷:survey.html", "success");
} catch (error) {
if (error.code === "QUOTA_FULL") {
setStatus("名額已滿,報名已截止。", "error");
} else {
setStatus("暫時未能提交,請稍後再試或聯絡主辦方。", "error");
}
console.error(error);
} finally {
setLoading(false);
}
});
if (REGISTRATION_CLOSED) {
closeRegistrationForm();
}