From c2c8c822127b76a6cfe42de268fa67cf4995c2b5 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 2 Oct 2023 03:56:57 +0400 Subject: [PATCH] Updated main.js. Added a new update() function that does everything. Not completed yet. --- web/assets/js/main.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/web/assets/js/main.js b/web/assets/js/main.js index 822c1e5..24851d4 100644 --- a/web/assets/js/main.js +++ b/web/assets/js/main.js @@ -1,5 +1,19 @@ const $ = id => document.getElementById(id); +function update() { + fetch("/api/status") + .then(r => r.json()) + .then(r => { + $("radio-song").textContent = `${r.current_song.artist} - ${r.current_song.title}`; + $("radio-duration").textContent = r.current_song.duration; + $("radio-listeners").textContent = r.listeners.current; + $("radio-listener-peak").textContent = r.listeners.peak; + }).catch(() => { + $("radio-song").textContent = ""; + $("radio-listeners").textContent = + $("radio-listener-peak").textContent = "0"; }); +} + function updateRadioStatus() { fetch("/status") .then(r => r.json()) @@ -29,12 +43,9 @@ function updateLastPlayedSong() { row.insertCell().appendChild(document.createTextNode(last_played.listeners == 0 ? "" : last_played.listeners)); row.insertCell().appendChild(document.createTextNode(last_played.song)); }); } -$("radio-update").addEventListener("click", () => { - updateLastPlayedSong(); - updateRadioStatus(); }); +$("radio-update").addEventListener("click", () => update()); -setInterval(updateRadioStatus, 45000); -setInterval(updateLastPlayedSong, 45000); +setInterval(update, 45000); const audio = document.getElementsByTagName("audio")[0]; audio.hidden = true;