Skip to content

Commit a9d9941

Browse files
committed
fix: ranked choice live updates!
1 parent b1c5aa5 commit a9d9941

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

templates/result.tmpl

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@
5151
</div>
5252
<div id="results">
5353
{{ range $i, $val := .Results }}
54-
{{ if eq $.VoteType "ranked" }}
55-
<h4 class="mb-3"><u>Round {{ $i | inc }}</u></h4>
56-
{{ end }}
57-
{{ range $option, $count := $val }}
58-
<div id="{{ $option }}" class="fs-5 lh-sm">
59-
{{ $option }}: {{ $count }}
54+
<div id="round-{{ $i }}">
55+
{{ if eq $.VoteType "ranked" }}
56+
<h4 class="mb-3"><u>Round {{ $i | inc }}</u></h4>
57+
{{ end }}
58+
{{ range $option, $count := $val }}
59+
<div id="{{ $i }}-{{ $option }}" class="fs-5 lh-sm">
60+
{{ $option }}: {{ $count }}
61+
</div>
62+
{{ end }}
6063
</div>
6164
<br />
62-
{{ end }}
6365
{{ end }}
6466
</div>
6567
{{ if and (.CanModify) (not .IsHidden) }}
@@ -82,19 +84,20 @@
8284

8385
eventSource.addEventListener("{{ .Id }}", function (event) {
8486
let data = JSON.parse(event.data);
85-
if (data.length == 0) return;
86-
let results = data[0];
87-
for (let option in results) {
88-
let count = results[option];
89-
let element = document.getElementById(option);
90-
if (element == null) {
91-
let newElement = document.createElement("div");
92-
newElement.id = option;
93-
newElement.style = "font-size: 1.25rem; line-height: 1.25";
94-
newElement.innerText = option + ": " + count;
95-
document.getElementById("results").appendChild(newElement);
87+
for (let roundNum in data) {
88+
for (let option in data[roundNum]) {
89+
let count = data[roundNum][option];
90+
let element = document.getElementById(`${roundNum}-${option}`);
91+
if (element == null) {
92+
let newElement = document.createElement("div");
93+
newElement.id = option;
94+
newElement.style = "font-size: 1.25rem; line-height: 1.25";
95+
newElement.innerText = option + ": " + count;
96+
console.log(`round-${roundNum}`);
97+
document.getElementById(`round-${roundNum}`).appendChild(newElement);
98+
}
99+
element.innerText = option + ": " + count;
96100
}
97-
element.innerText = option + ": " + count;
98101
}
99102
});
100103
</script>

0 commit comments

Comments
 (0)