From f68bbb3d68c2b29566e00707cbe3b702f77c9c75 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi <256895@studenti.unimore.it> Date: Thu, 22 Apr 2021 16:45:09 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Save=20last=20set=20theme=20in?= =?UTF-8?q?=20the=20localStorage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/frontend/src/App.js | 44 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/code/frontend/src/App.js b/code/frontend/src/App.js index 6e8f4d3..9722790 100644 --- a/code/frontend/src/App.js +++ b/code/frontend/src/App.js @@ -12,10 +12,50 @@ import PageSettings from "./routes/PageSettings" export default function App() { - const [theme, setTheme] = useState("ThemeDark"); + const loadTheme = () => { + if(localStorage) { + console.debug(`Loading theme from localStorage...`) + let value = localStorage.getItem("theme") + + if(value) { + console.debug(`Loaded theme ${value}!`) + return value + } + else { + console.debug(`There is no theme stored in the localStorage; setting to "ThemeDark"...`) + return "ThemeDark" + } + } + else { + console.warn(`Can't load theme; localStorage doesn't seem to be available; setting to "ThemeDark"...`) + return "ThemeDark" + } + } + + const [theme, _setTheme] = useState(loadTheme()); + + const setTheme = (value) => { + console.debug(`Changing theme to ${value}...`) + _setTheme(value) + } + + const saveTheme = (value) => { + if(localStorage) { + console.debug(`Saving theme ${value} to localStorage...`) + localStorage.setItem("theme", value) + } + else { + console.warn(`Can't save theme; localStorage doesn't seem to be available...`) + } + } + + const setAndSaveTheme = (value) => { + setTheme(value) + saveTheme(value) + } return ( - +