-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
135 lines (106 loc) · 3.98 KB
/
Copy pathscript.js
File metadata and controls
135 lines (106 loc) · 3.98 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
//Firebase Imports from global window object
const {
doc,
getDoc,
getDocs,
setDoc,
updateDoc,
collection
} = window.firebaseTools;
//firestore set up
const db = window.firebaseDB;
const subjectsCollection = collection(db, "subjects");
//state variables
let subjects = [];
let hasVoted = localStorage.getItem("hasVoted") === "true";
//Fetch subjects from Firestore & render them
async function fetchSubjects() {
// console.log("Fetching subjects from Firestore..."); //testing don't remove.
const querySnapshot = await getDocs(subjectsCollection);
subjects = [];
querySnapshot.forEach((docSnap) => {
subjects.push({
name: docSnap.id,
votes: docSnap.data().votes || 0
});
});
renderTable(subjects);
}
// Cast a vote to Firestore
async function castVote(subjectName) {
// console.log("Vote button clicked for:", subjectName); // Testing only, don't remove.
if (hasVoted) return;
const subjectDoc = doc(db, "subjects", subjectName);
const snap = await getDoc(subjectDoc);
const currentVotes = snap.exists() ? snap.data().votes : 0;
await updateDoc(subjectDoc, {
votes: currentVotes + 1
});
//// Record the vote in local storage to prevent duplicates
localStorage.setItem("hasVoted", "true");
hasVoted = true;
fetchSubjects(); // Re-fetch and update table
}
//Render all subject rows in the table
function renderTable(subjects) {
console.log("Subjects being rendered:", subjects);
const tbody = document.getElementById("subjects-table-body");
tbody.innerHTML = "";
const totalVotes = subjects.reduce((sum, s) => sum + s.votes, 0);
const rankedSubjects = subjects
.map((s) => ({
...s,
percentage: totalVotes === 0 ? 0 : Math.round((s.votes / totalVotes) * 100)
}))
.sort((a, b) => b.votes - a.votes);
rankedSubjects.forEach((subject, index) => {
const tr = document.createElement("tr");
tr.innerHTML = `
<td>${index + 1}</td>
<td>${subject.name}</td>
<td><button class="vote" ${hasVoted ? "disabled" : ""} onclick="castVote('${subject.name}')">Vote</button></td>
<td>
<div class="bar">
<div class="bar-fill" style="width: 0%;">${subject.percentage}%</div>
</div>
</td>
`;
tbody.appendChild(tr);
const barFill = tr.querySelector(".bar-fill");
setTimeout(() => {
barFill.style.width = `${subject.percentage}%`;
}, 100);
});
//Update leaderboard (top 3 subjects only)
updateLeaderboard(rankedSubjects);
}
function updateLeaderboard(sorted) {
const total = sorted.reduce((sum, s) => sum + s.votes, 0);
const topThree = sorted.filter((s) => s.votes > 0).slice(0, 3);
const [first, second, third] = [topThree[0], topThree[1], topThree[2]];
document.getElementById("first-subject").textContent = first ? first.name : "-";
document.getElementById("first-votes").textContent = first ? `${Math.round((first.votes / total) * 100)}% votes` : "";
document.getElementById("second-subject").textContent = second ? second.name : "-";
document.getElementById("second-votes").textContent = second ? `${Math.round((second.votes / total) * 100)}% votes` : "";
document.getElementById("third-subject").textContent = third ? third.name : "-";
document.getElementById("third-votes").textContent = third ? `${Math.round((third.votes / total) * 100)}% votes` : "";
}
//On page load, fetch & display subjects
document.addEventListener("DOMContentLoaded", fetchSubjects);
// Make castVote globally available so inline onclick can access it
window.castVote = castVote;
//testing purposes only
window.resetVotes = () => {
localStorage.removeItem("hasVoted");
alert("Voting reset! Refresh to try again.");
};
// copy link home page
function copyLink() {
const currentURL = window.location.href;
navigator.clipboard.writeText(currentURL).then(() => {
alert("🔗 Link copied to clipboard! Share it with your friends!");
}).catch((err) => {
alert("Failed to copy the link. Please try manually.");
console.error("Copy error:", err);
});
}