This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/public/static/js/ResultService.js
2022-04-27 10:57:26 +00:00

72 lines
1.8 KiB
JavaScript

class ResultService
{
constructor(alertBox, rankingTable, endpoint)
{
this.alertBox = alertBox;
this.rankingTable = rankingTable;
this.endpoint = endpoint;
this.hash = 0;
this.loop();
}
async loop()
{
try {
await this.updateTable();
this.alertBox.classList.add("hidden");
} catch (error) {
console.log(error);
this.alertBox.innerHTML = "<b>Noe gikk galt: </b>" + error;
this.alertBox.classList.remove("hidden");
}
setTimeout(() => {
this.loop();
}, 1000);
}
async updateTable()
{
let response = await fetch(this.endpoint + this.hash);
if (response.status === 204)
{
return;
}
let json = await response.json();
this.hash = json.hash;
let data = json.data;
data.times = data.times.reverse();
let tableHTML = "";
tableHTML += "<table>";
tableHTML += "<tr>";
tableHTML += "<th>#</th>";
tableHTML += "<th>Lag</th>";
tableHTML += "<th>Bedrift</th>";
tableHTML += "<th>Tid</th>";
tableHTML += "<th>Runder</th>";
tableHTML += "</tr>";
let iterator = 0;
data.times.forEach(element => {
iterator++;
tableHTML += "<tr>";
tableHTML += "<td>" + iterator + "</td>";
tableHTML += "<td>" + data.map.team[element.id].name + "</td>";
tableHTML += "<td>" + data.map.team[element.id].company + "</td>";
tableHTML += "<td>" + element.time + "</td>";
tableHTML += "<td>" + data.map.team[element.id].rounds+ "</td>";
tableHTML += "</tr>";
});
tableHTML += "<table>";
this.rankingTable.innerHTML = tableHTML;
}
}