-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
258 lines (218 loc) · 7.57 KB
/
script.js
File metadata and controls
258 lines (218 loc) · 7.57 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Countdown Timer
const targetDate = new Date("October 28, 2025 00:00:00").getTime();
function updateCountdown() {
const countdownElement = document.getElementById("countdown");
// Check if countdown elements exist (they only exist on index.html)
if (!countdownElement) {
return;
}
const now = new Date().getTime();
const diff = targetDate - now;
if (diff <= 0) {
countdownElement.innerText = "The Fest Has Started!";
return;
}
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
const daysEl = document.getElementById("days");
const hoursEl = document.getElementById("hours");
const minutesEl = document.getElementById("minutes");
const secondsEl = document.getElementById("seconds");
if (daysEl) daysEl.innerText = days;
if (hoursEl) hoursEl.innerText = hours;
if (minutesEl) minutesEl.innerText = minutes;
if (secondsEl) secondsEl.innerText = seconds;
}
setInterval(updateCountdown, 1000);
updateCountdown();
// Mobile Navigation Toggle
const hamburger = document.getElementById("hamburger");
const navLinks = document.getElementById("nav-links");
hamburger.addEventListener("click", () => {
navLinks.classList.toggle("active");
});
// Certificate Generation
const CERTIFICATE_API_URL = 'https://certificate-generator-916823407631.us-central1.run.app';
// Registration verification is handled by registration-check.js
// Use window.RegistrationChecker.isEmailRegistered(email) to check registration
const certBtn = document.getElementById("cert-btn");
const certEmailInput = document.getElementById("cert-email");
const certMsg = document.getElementById("cert-msg");
const certModal = document.getElementById("cert-modal");
const certPreview = document.getElementById("cert-preview");
const errorModal = document.getElementById("error-modal");
const modalClose = document.getElementById("modal-close");
const modalCancel = document.getElementById("modal-cancel");
const modalDownload = document.getElementById("modal-download");
const errorModalClose = document.getElementById("error-modal-close");
let currentCertificateBlob = null;
let currentEmail = "";
if (certBtn && certEmailInput && certMsg) {
certBtn.addEventListener("click", async () => {
const email = certEmailInput.value.trim();
// Validate email
if (!email) {
showCertMessage("Please enter your email address.", "error");
return;
}
if (!isValidEmail(email)) {
showCertMessage("Please enter a valid email address.", "error");
return;
}
// Disable button and show loading
certBtn.disabled = true;
certBtn.textContent = "Loading...";
showCertMessage("Verifying registration...", "info");
currentEmail = email;
try {
// Check if email is registered
const isRegistered = await window.RegistrationChecker.isEmailRegistered(email);
if (!isRegistered) {
showCertMessage("❌ Email not registered! Please register for QFF 2025 first to receive a certificate. If you have already registered, please use the same email address.", "error");
certBtn.disabled = false;
certBtn.textContent = "View Certificate";
return;
}
showCertMessage("Generating certificate...", "info");
// The backend now automatically checks winner status and generates the appropriate certificate
// We just need to request participation type and the backend handles the rest
const response = await fetch(CERTIFICATE_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email,
type: 'participation'
}),
});
if (response.ok) {
// Get the PDF blob
const blob = await response.blob();
currentCertificateBlob = blob;
// Create blob URL for preview
const blobUrl = window.URL.createObjectURL(blob);
certPreview.src = blobUrl;
// Show modal
certModal.classList.add("active");
showCertMessage("", "");
} else {
const errorData = await response.json();
if (response.status === 404) {
// Show error modal
showErrorModal();
} else {
showCertMessage(`❌ Error: ${errorData.error || "Failed to generate certificate"}`, "error");
}
}
} catch (error) {
console.error("Certificate generation error:", error);
showCertMessage("❌ An error occurred. Please try again later or contact the organisers.", "error");
} finally {
// Re-enable button
certBtn.disabled = false;
certBtn.textContent = "View Certificate";
}
});
// Allow Enter key to submit
certEmailInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
certBtn.click();
}
});
}
// Modal close handlers
if (modalClose) {
modalClose.addEventListener("click", closeModal);
}
if (modalCancel) {
modalCancel.addEventListener("click", closeModal);
}
if (errorModalClose) {
errorModalClose.addEventListener("click", closeErrorModal);
}
// Download certificate from modal
if (modalDownload) {
modalDownload.addEventListener("click", () => {
if (currentCertificateBlob && currentEmail) {
const url = window.URL.createObjectURL(currentCertificateBlob);
const a = document.createElement('a');
a.href = url;
a.download = `${currentEmail}_participation_certificate.pdf`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
showCertMessage("✅ Certificate downloaded successfully!", "success");
closeModal();
certEmailInput.value = ""; // Clear the input
}
});
}
// Close modal when clicking outside
if (certModal) {
certModal.addEventListener("click", (e) => {
if (e.target === certModal) {
closeModal();
}
});
}
if (errorModal) {
errorModal.addEventListener("click", (e) => {
if (e.target === errorModal) {
closeErrorModal();
}
});
}
function closeModal() {
if (certModal) {
certModal.classList.remove("active");
if (certPreview.src) {
window.URL.revokeObjectURL(certPreview.src);
certPreview.src = "";
}
}
}
function closeErrorModal() {
if (errorModal) {
errorModal.classList.remove("active");
}
}
function showErrorModal() {
if (errorModal) {
errorModal.classList.add("active");
}
showCertMessage("", "");
}
function showCertMessage(message, type) {
certMsg.textContent = message;
certMsg.className = "";
if (type === "success") {
certMsg.style.color = "#155724";
certMsg.style.backgroundColor = "#d4edda";
certMsg.style.border = "1px solid #c3e6cb";
} else if (type === "error") {
certMsg.style.color = "#721c24";
certMsg.style.backgroundColor = "#f8d7da";
certMsg.style.border = "1px solid #f5c6cb";
} else if (type === "info") {
certMsg.style.color = "#0c5460";
certMsg.style.backgroundColor = "#d1ecf1";
certMsg.style.border = "1px solid #bee5eb";
}
if (message) {
certMsg.style.padding = "0.75rem";
certMsg.style.borderRadius = "6px";
certMsg.style.marginTop = "1rem";
} else {
certMsg.style.padding = "0";
certMsg.style.border = "none";
certMsg.style.backgroundColor = "transparent";
}
}
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}