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

128 lines
4.2 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>
body {
font-size: 1.25rem;
}
:not(#answers-list[hidden]) + #question-text,
:not(#answers-list[hidden]) + #question-text + #answer-form,
:not(#question-options:empty) + #answers-freeform,
#question-text:empty + #answer-form
{
display: none;
}
</style>
</header>
<body>
<div id="answers-list"></div>
<div id="question-text"></div>
<form id="answer-form" action="#" onsubmit="submitAnswersForm(this); return false;">
<fieldset id="question-options" name="question-options"></fieldset>
<input id="answers-freeform" name="answers-freeform" type="text" hidden/>
<button type="submit">Submit</button>
</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);
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) {
let options = "";
for (let i of g_live_question.Options)
options += `<div>
<input type="radio" value="${i}" name="g_live_question.Options" />
<label for="${i}">${i}</label>
</div>`
if (document.getElementById("question-text").value != g_live_question.Text) {
document.getElementById("question-text").value = g_live_question.Text;
document.getElementById("question-text").innerHTML = `<h1>${g_live_question.Text}</h1>`;
}
if (document.getElementById("question-options").value != options) {
document.getElementById("question-options").value = options;
document.getElementById("question-options").innerHTML = options;
}
} else {
document.getElementById("question-text").innerHTML = "";
document.getElementById("question-options").innerHTML = "";
}
}
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).map((a) => a.Text);
answers.sort((a, b) => {
a = a.toLowerCase();
b = b.toLowerCase();
return (a > b) - (a < b)
});
let result = "";
for (let i of answers)
result += `<li>${i}</li>`;
document.getElementById("answers-list").innerHTML = `<ul>${result}</ul>`;
});
}
function submitAnswersForm(form) {
let children = [];
for (let e of form["question-options"].elements) {
children.push(e);
}
let checked_children = children.filter((c) => c.checked);
let text = form["answers-freeform"].value;
if (checked_children)
text = checked_children[0].value;
http("POST", `/api/v1/questions/${g_live_question.ID}/answers`, (body, status) => {
console.log(status, body);
}, JSON.stringify({
Text: text,
}));
}
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>