From 9689f2daa6c34e7d24d5cf1648168c66c5678385 Mon Sep 17 00:00:00 2001 From: Arjun Satarkar Date: Sun, 30 Jul 2023 18:17:35 +0530 Subject: Add auto-refresh checkbox, don't auto-refresh except on front page Also use input type "hidden" instead of display: none where appropriate (no observable effects, just cleaner). --- static/scripts/auto_refresh.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'static') diff --git a/static/scripts/auto_refresh.js b/static/scripts/auto_refresh.js index 20fc5c6..48af816 100644 --- a/static/scripts/auto_refresh.js +++ b/static/scripts/auto_refresh.js @@ -1,6 +1,28 @@ (() => { + const onFrontPage = () => { + const searchParams = new URLSearchParams(window.location.search); + let pageNum = searchParams.get("page_num"); + return (pageNum === "1") || (pageNum === null); + }; + + const refreshCheckboxLabel = document.querySelector("label#refresh_checkbox_label"); + if (onFrontPage()) { + refreshCheckboxLabel.setAttribute("style", ""); + } + const UPDATE_INTERVAL_MILLISECONDS = 1 * 60 * 1000; // 1 minute setInterval(async () => { + if (!onFrontPage()) { + return; + } + + const refreshCheckbox = document.querySelector("label#refresh_checkbox_label > input"); + if (!refreshCheckbox.checked) { + return; + } + + console.log("Refreshing entries..."); + const response = await fetch(window.location); if (!response.ok) { return; @@ -10,5 +32,7 @@ const parser = new DOMParser(); const newDoc = parser.parseFromString(responseText, "text/html"); document.querySelector("table").innerHTML = newDoc.querySelector("table").innerHTML; + + console.log("Refreshed entries."); }, UPDATE_INTERVAL_MILLISECONDS); })(); -- cgit v1.2.3-57-g22cb