live-studio-audience/cmd/server/internal/public/index.html

94 lines
2.8 KiB
HTML

<!DOCTYPE html>
<html>
<header>
<!--<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/light.css">-->
<link rel="stylesheet" href="/light.css">
<style>
:not(#answers-list[hidden]) + #question,
:not(#answers-list[hidden]) + #question + #answer-form,
:not(#question-options:empty) + #answers-freeform
{
display: none;
}
</style>
</header>
<body>
<h1>Hello World</h1>
<div id="answers-list"></div>
<div id="question">
<h1><span id="question-text"></span></h1>
</div>
<form id="answer-form" action="#" onsubmit="submitAnswersForm(this); return false;">
<ul id="question-options"></ul>
<input id="answers-freeform" type="text" hidden/>
</form>
</body>
<footer>
<script>
let g_questions = [];
let g_live_question = {};
function pollState() {
http("GET", "/api/v1/questions", (body) => {
if (!body)
return;
g_questions = JSON.parse(body);
console.log("TODO polled state:", body);
pollLiveAnswer();
pollLiveQuestion();
});
}
function pollLiveQuestion() {
let live_questions = g_questions.filter((q) => q.Live);
if (live_questions) {
g_live_question = live_questions[0];
}
if (g_live_question) {
document.getElementById("question-text").innerHTML = g_live_question.Text;
let options = "";
for (let i of g_live_question.Options)
options += `<li>${i}</li>`;
document.getElementById("question-options").innerHTML = options;
}
}
function pollLiveAnswer() {
if (!g_live_question || !g_live_question.Closed) {
document.getElementById("answers-list").hidden = true;
return;
}
document.getElementById("answers-list").hidden = false;
http("GET", `/api/v1/questions/${g_live_question.ID}/answers`, (body) => {
let answers = JSON.parse(body);
let result = "";
for (let i of answers)
result += `<li>${i.Text}</li>`;
document.getElementById("answers-list").innerHTML = `<ul>${result}</ul>`;
});
}
function http(method, remote, callback, body) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
callback(xmlhttp.responseText, xmlhttp.status)
}
};
xmlhttp.open(method, remote, true);
if(typeof body == "undefined") {
body = null
}
xmlhttp.send(body);
}
pollState();
setInterval(() => {
try {
pollState();
} catch {}
}, 1000);
</script>
</footer>
</html>