Skip to content

Commit a324804

Browse files
committed
Debaters: count votes in Supabase and show running totals
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019UZz4xLPVHqA1JTJc3DSVw
1 parent 408bd70 commit a324804

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

‎DoctorsVideos_App/debaters.html‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,26 +147,54 @@ <h3 style="margin-top:22px">Who won your vote?</h3>
147147
<script src="common.js"></script>
148148
<script src="ads.js"></script>
149149
<script>
150-
// Remembers each viewer's pick in their own browser. Nothing is sent anywhere.
150+
// One vote per browser for each debate. A vote is one row (debate, option, time) with no personal
151+
// data; the running totals come from the get_debate_counts function. If the database is not set
152+
// up or cannot be reached, the buttons still work and simply show no numbers.
151153
document.querySelectorAll('.debate-vote').forEach(function (group) {
152-
var key = 'debate-vote-' + group.dataset.debate;
154+
var debate = group.dataset.debate;
155+
var key = 'debate-vote-' + debate;
153156
var status = group.nextElementSibling;
154-
function show(pick) {
155-
group.querySelectorAll('button').forEach(function (b) {
157+
var buttons = group.querySelectorAll('button[data-vote]');
158+
var pick = null;
159+
try { pick = localStorage.getItem(key); } catch (e) {}
160+
161+
function show() {
162+
buttons.forEach(function (b) {
156163
var on = b.dataset.vote === pick;
157164
b.setAttribute('aria-pressed', on ? 'true' : 'false');
158165
b.classList.toggle('topic-chip-featured', on);
159166
});
160167
status.textContent = pick ? 'Your pick: ' + pick + '.' : '';
161168
}
162-
var saved = null;
163-
try { saved = localStorage.getItem(key); } catch (e) {}
164-
show(saved);
169+
170+
function loadCounts() {
171+
if (typeof db === 'undefined') return;
172+
Promise.resolve(db.rpc('get_debate_counts', { p_debate: debate })).then(function (res) {
173+
if (!res || res.error || !Array.isArray(res.data)) return;
174+
var counts = {};
175+
res.data.forEach(function (row) {
176+
var n = Number(row.count != null ? row.count : (row.votes != null ? row.votes : row.total));
177+
if (row.option != null && isFinite(n)) counts[row.option] = n;
178+
});
179+
buttons.forEach(function (b) {
180+
b.textContent = b.dataset.vote + ' \u2014 ' + (counts[b.dataset.vote] || 0);
181+
});
182+
}).catch(function () {});
183+
}
184+
185+
show();
186+
loadCounts();
187+
165188
group.addEventListener('click', function (e) {
166189
var b = e.target.closest('button[data-vote]');
167190
if (!b) return;
168-
try { localStorage.setItem(key, b.dataset.vote); } catch (err) {}
169-
show(b.dataset.vote);
191+
if (pick) { show(); loadCounts(); return; }
192+
pick = b.dataset.vote;
193+
try { localStorage.setItem(key, pick); } catch (err) {}
194+
show();
195+
if (typeof db === 'undefined') return;
196+
Promise.resolve(db.from('debate_votes').insert({ debate: debate, option: pick }))
197+
.then(loadCounts, loadCounts);
170198
});
171199
});
172200
</script>

0 commit comments

Comments
 (0)