From d92df4af4cc557822cf6e2f1cc559ae76713a5c9 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Fri, 27 Jan 2023 18:05:52 -0300 Subject: [PATCH] If no unit is set on setupVars, try to use localstorage value, if exists Signed-off-by: RD WebDesign --- scripts/pi-hole/js/footer.js | 31 +++++++++++++++----- scripts/pi-hole/php/header_authenticated.php | 3 +- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/scripts/pi-hole/js/footer.js b/scripts/pi-hole/js/footer.js index 28014487..e982c0d6 100644 --- a/scripts/pi-hole/js/footer.js +++ b/scripts/pi-hole/js/footer.js @@ -181,8 +181,31 @@ function initCPUtemp() { } } + function setSetupvarsTempUnit(unit, showmsg = true) { + var token = encodeURIComponent($("#token").text()); + $.getJSON("api.php?setTempUnit=" + unit + "&token=" + token, function (data) { + if (showmsg === true) { + if ("result" in data && data.result === "success") { + utils.showAlert("success", "", "Temperature unit set to " + unit, ""); + } else { + utils.showAlert("error", "", "", "Temperature unit not set"); + } + } + }); + } + // Read the temperature unit from HTML code var tempunit = $("#tempunit").text(); + if (!tempunit) { + // if no value was set in setupVars.conf, tries to retrieve the old config from localstorage + tempunit = localStorage ? localStorage.getItem("tempunit") : null; + if (tempunit === null) { + tempunit = "C"; + } else { + // if some value was found on localstorage, set the value in setupVars.conf + setSetupvarsTempUnit(tempunit, false); + } + } setCPUtemp(tempunit); @@ -195,13 +218,7 @@ function initCPUtemp() { setCPUtemp(tempunit); // store the selected value on setupVars.conf - $.getJSON("api.php?setTempUnit=" + tempunit + "&token=" + token, function (data) { - if ("result" in data && data.result == "success") { - utils.showAlert("success", "", "Temperature unit set to " + tempunit, ""); - } else { - utils.showAlert("error", "", "", "Temperature unit not set"); - } - }); + setSetupvarsTempUnit(tempunit); }); } } diff --git a/scripts/pi-hole/php/header_authenticated.php b/scripts/pi-hole/php/header_authenticated.php index b82fdbde..b022d4a6 100644 --- a/scripts/pi-hole/php/header_authenticated.php +++ b/scripts/pi-hole/php/header_authenticated.php @@ -110,7 +110,8 @@ function getTemperature() $unit = 'C'; } } else { - $unit = 'C'; + // no value is set in setupVars.conf + $unit = ''; } return array($celsius, $limit, $unit);