-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·138 lines (113 loc) · 3.16 KB
/
script.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
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
const form = document.getElementById("generate-form");
const qr = document.getElementById("qrcode");
const resetBtn = document.getElementById("reset-btn");
const spinner = document.getElementById("spinner");
const onGenerateSubmit = (e) => {
e.preventDefault();
clearUI();
// Get values from the form inputs
const firstName = document.getElementById("first-name").value;
const lastName = document.getElementById("last-name").value;
const center = document.getElementById("center").value;
const studentId = document.getElementById("student-id").value;
const size = document.getElementById("size").value;
const centerType = document.querySelector('input[name="center-type"]:checked')?.value;
const team = document.querySelector('input[name="team"]:checked')?.value;
// Check if all fields are filled
let errorMessage = "Please fill in all fields:";
let hasError = false;
if (!firstName) {
errorMessage += "\n- First Name";
hasError = true;
}
if (!lastName) {
errorMessage += "\n- Last Name";
hasError = true;
}
if (!center) {
errorMessage += "\n- Center";
hasError = true;
}
if (!studentId) {
errorMessage += "\n- Student ID";
hasError = true;
}
if (!centerType) {
errorMessage += "\n- Center Type";
hasError = true;
}
if (!team) {
errorMessage += "\n- team";
hasError = true;
}
if (hasError) {
alert(errorMessage);
return;
}
// Prepare data for QR code
const data = {
firstName,
lastName,
center,
studentId,
centerType,
team
};
showSpinner();
setTimeout(() => {
hideSpinner();
generateQRCode(JSON.stringify(data), size);
setTimeout(() => {
const qrImage = qr.querySelector("img");
if (qrImage) {
const saveUrl = qrImage.src;
localStorage.setItem('qrData', JSON.stringify(data));
localStorage.setItem('qrCodeUrl', saveUrl);
createDownloadBtn(saveUrl);
window.location.href = 'ticket.html'; // Navigate to ticket page
}
}, 50);
}, 1000);
};
const generateQRCode = (data, size) => {
// Clear previous QR code if it exists
qr.innerHTML = "";
new QRCode(qr, {
text: data,
width: size,
height: size,
});
};
const showSpinner = function () {
spinner.style.display = "block";
};
const hideSpinner = function () {
spinner.style.display = "none";
};
const clearUI = function () {
qr.innerHTML = "";
const downloadBtn = document.getElementById("downloadlink");
if (downloadBtn) {
downloadBtn.remove();
}
};
const createDownloadBtn = function (downloadUrl) {
// Remove old download button if it exists
const oldDownloadBtn = document.getElementById("downloadlink");
if (oldDownloadBtn) {
oldDownloadBtn.remove();
}
const link = document.createElement("a");
link.id = "downloadlink";
link.classList = "download-btn";
link.href = downloadUrl;
link.download = "qrcode.png"; // Ensure the file extension is included
link.innerHTML = "Download";
document.getElementById("generated").appendChild(link);
};
form.addEventListener("submit", onGenerateSubmit);
resetBtn.addEventListener("click", () => {
form.reset();
clearUI();
hideSpinner();
});