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

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-04-26 13:00:38 +00:00
class ResultService
{
2022-04-26 19:55:10 +00:00
constructor(alertBox, rankingTable, endpoint)
2022-04-26 13:00:38 +00:00
{
2022-04-26 21:20:32 +00:00
this.alertBox = alertBox;
2022-04-26 19:55:10 +00:00
this.rankingTable = rankingTable;
2022-04-26 21:20:32 +00:00
this.endpoint = endpoint;
2022-04-26 19:55:10 +00:00
this.hash = 0;
2022-04-26 13:00:38 +00:00
this.loop();
}
async loop()
{
try {
await this.updateTable();
2022-04-26 19:55:10 +00:00
this.alertBox.classList.add("hidden");
2022-04-26 13:00:38 +00:00
} catch (error) {
console.log(error);
2022-04-26 19:55:10 +00:00
this.alertBox.innerHTML = "<b>Noe gikk galt: </b>" + error;
this.alertBox.classList.remove("hidden");
2022-04-26 13:00:38 +00:00
}
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;
2022-04-26 21:20:32 +00:00
data.times = data.times.reverse();
2022-04-26 13:00:38 +00:00
let tableHTML = "";
tableHTML += "<table>";
tableHTML += "<tr>";
2022-04-26 19:55:10 +00:00
tableHTML += "<th>#</th>";
2022-04-26 13:00:38 +00:00
tableHTML += "<th>Lag</th>";
tableHTML += "<th>Tid</th>";
tableHTML += "</tr>";
2022-04-26 19:55:10 +00:00
let iterator = 0;
2022-04-26 21:20:32 +00:00
data.times.forEach(element => {
2022-04-26 19:55:10 +00:00
iterator++;
2022-04-26 21:20:32 +00:00
2022-04-26 13:00:38 +00:00
tableHTML += "<tr>";
2022-04-26 19:55:10 +00:00
tableHTML += "<td>" + iterator + "</td>";
2022-04-26 13:00:38 +00:00
tableHTML += "<td>" + data.map.team.name[element.id] + "</td>";
tableHTML += "<td>" + element.time + "</td>";
tableHTML += "</tr>";
});
tableHTML += "<table>";
2022-04-26 19:55:10 +00:00
this.rankingTable.innerHTML = tableHTML;
2022-04-26 13:00:38 +00:00
}
}