-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll-demo.html
More file actions
152 lines (135 loc) · 4.09 KB
/
poll-demo.html
File metadata and controls
152 lines (135 loc) · 4.09 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Real-Time Polling</title>
<style>
body {
font-family: sans-serif;
padding: 20px;
background: #f9f9f9;
}
h1 {
color: #333;
}
.option-btn {
margin: 6px;
padding: 10px 18px;
border: none;
border-radius: 6px;
background: #007bff;
color: white;
cursor: pointer;
font-size: 14px;
}
.option-btn:hover:not(:disabled) {
background: #0056b3;
}
.option-btn:disabled {
background: #ccc;
cursor: not-allowed;
}
pre {
background: #fff;
padding: 10px;
border-radius: 6px;
border: 1px solid #ddd;
}
#message {
margin-top: 10px;
font-weight: bold;
color: green;
}
</style>
</head>
<body>
<h1>Real-Time Polling</h1>
<p><strong>Poll:</strong> <span id="question">Loading...</span></p>
<div id="options"></div>
<h2>Live Results</h2>
<pre id="results">Waiting for updates...</pre>
<p id="message"></p>
<!-- Load Socket.IO client -->
<script src="http://localhost:4000/socket.io/socket.io.js"></script>
<script>
const token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjbWZnbzgwNTYwMDAwNmE5d2N0MnJ0MTZ1IiwiaWF0IjoxNzU3NjcyNzUxLCJleHAiOjE3NTgyNzc1NTF9.QT4JkV4vd8C6LB86-VJ4iHfG4MqS8CGcquBaZ5ogaWo"; // 🔑 Replace with your JWT token
const apiBase = "http://localhost:4000";
let pollId = null;
const socket = io(apiBase, { transports: ["websocket"] });
socket.on("connect", () => {
console.log("Connected:", socket.id);
if (pollId) {
socket.emit("joinPoll", pollId);
}
});
socket.on("pollUpdated", (payload) => {
console.log("pollUpdated:", payload);
renderPoll(payload);
});
// ✅ Fetch latest poll dynamically
async function fetchLatestPoll() {
const res = await fetch(`${apiBase}/polls`);
const polls = await res.json();
if (!polls || polls.length === 0) {
document.getElementById("question").innerText =
"No polls found. Please create one first.";
return;
}
// Use the most recent poll
pollId = polls[0].id;
console.log("Using pollId:", pollId);
socket.emit("joinPoll", pollId);
const pollRes = await fetch(`${apiBase}/polls/${pollId}`);
const poll = await pollRes.json();
renderPoll(poll);
}
async function vote(optionId) {
const res = await fetch(`${apiBase}/polls/${pollId}/vote`, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ optionId }),
});
const data = await res.json();
console.log("Vote response:", data);
if (res.status === 200) {
// Disable all buttons after successful vote
document.querySelectorAll("button.option-btn").forEach((btn) => {
btn.disabled = true;
});
document.getElementById("message").innerText =
"✅ You already voted!";
} else {
alert(data.error || "Vote failed");
}
}
function renderPoll(poll) {
document.getElementById("question").innerText = poll.question;
// Render options as buttons
document.getElementById("options").innerHTML = poll.options
.map(
(o) => `
<button class="option-btn" onclick="vote('${o.id}')">
${o.text} (${o.votes} votes)
</button>
`
)
.join("");
// Render results
document.getElementById("results").innerText = JSON.stringify(
poll.options.map((o) => ({
option: o.text,
votes: o.votes,
})),
null,
2
);
}
// 🚀 Start by fetching latest poll
fetchLatestPoll();
</script>
</body>
</html>