This commit is contained in:
William 2022-04-21 14:41:19 +02:00
parent 79893ba3dd
commit 941e1a8965
2 changed files with 54 additions and 50 deletions

View File

@ -12,31 +12,41 @@ Laster inn...
</div> </div>
<script> <script>
let hash = 0;
let alertbox = document.getElementById("alert");
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', () => {
loop(); new ResultService();
}); });
async function loop() class ResultService
{ {
try { constructor()
await updateTable(); {
alertbox.classList.add("hidden"); this.hash = 0;
} catch (error) { this.alertbox = document.getElementById("alert");
alertbox.innerHTML = "<b>Noe gikk galt: </b>" + error; this.table = document.getElementById("ranking-table");
alertbox.classList.remove("hidden");
this.#loop();
} }
setTimeout(function() { async #loop(self)
loop(); {
}, 1000); 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");
}
async function updateTable() setTimeout(() => {
{ this.#loop();
let response = await fetch("../api/v1/race/sync.php?h=" + hash); }, 1000);
}
async #updateTable()
{
let response = await fetch("../api/v1/race/sync.php?h=" + this.hash);
if (response.status === 204) if (response.status === 204)
{ {
@ -45,28 +55,28 @@ async function updateTable()
let json = await response.json(); let json = await response.json();
hash = json.hash; this.hash = json.hash;
let data = json.data; let data = json.data;
let table_html = ""; let tableHTML = "";
table_html += "<table>"; tableHTML += "<table>";
table_html += "<tr>"; tableHTML += "<tr>";
table_html += "<th>Navn</th>"; tableHTML += "<th>Navn</th>";
table_html += "<th>Bestetid</th>"; tableHTML += "<th>Bestetid</th>";
table_html += "</tr>"; tableHTML += "</tr>";
data.forEach(element => { data.forEach(element => {
table_html += "<tr>"; tableHTML += "<tr>";
table_html += "<td>" + element.n + "</td>"; tableHTML += "<td>" + element.name + "</td>";
table_html += "<td>" + element.d + "</td>"; tableHTML += "<td>" + element.date + "</td>";
table_html += "</tr>"; tableHTML += "</tr>";
}); });
table_html += "<table>"; tableHTML += "<table>";
console.log(data); this.table.innerHTML = tableHTML;
}
document.getElementById("ranking-table").innerHTML = table_html;
} }
</script> </script>

View File

@ -22,15 +22,9 @@ foreach ($times as $time)
{ {
$team = $team_mapper->get($time->team_id); $team = $team_mapper->get($time->team_id);
if (!$team)
{
// this should not happen as related times should be deleted with the team
#continue;
}
$row = [ $row = [
"n" => $team->name, "name" => $team->name,
"d" => $time->date->getTimestamp() "date" => $time->date->getTimestamp()
]; ];
array_push($data, $row); array_push($data, $row);