From 0c24aba5dd6b7804ba63d596e3ae692fccb68e88 Mon Sep 17 00:00:00 2001 From: Marin Ivanov Date: Tue, 20 Aug 2024 15:24:28 +0300 Subject: add errors --- app.js | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) (limited to 'app.js') diff --git a/app.js b/app.js index 889afc0..8dec438 100644 --- a/app.js +++ b/app.js @@ -6,10 +6,15 @@ const loading = (val) => { loader.$click(() => loading(0)); let error = null; +function errClose() { + error = null; + app.reload(); +} let data = {}; function onerror(err) { error = err; console.error(err); + app.reload(); } function parsedata(r) { return Object.fromEntries(r.split("\n").map(x=>x.trim()).filter(x=>x).map(x=>x.split('='))); @@ -24,7 +29,7 @@ function cmd(url, body) { }) .then(r => parsedata(r)); }; -function loaddata() { +function loadconfig() { loading(1); cmd("/cgi-bin/getconfig") .then(x => { @@ -34,21 +39,34 @@ function loaddata() { .catch(onerror) .then(() => loading(0)) } +function saveconfig(groups) { + cmd("/cgi-bin/setconfig", data) + .catch(onerror) + .then(loadconfig) +} + +function CErr(text) { + var el = div( + div("🗙").$cls("close").$click(errClose), + span("⚠ "), + text, + ).$cls("err"); + function errClose() { + error = null; + el.remove(); + }; + return el; +} const routes = { "/": () => div( h2("Lorem Ipsum"), p("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "), p("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."), - - h2("Ipsum Lorem"), - p("Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."), - p("Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."), - p("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."), - p("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "), ), "/network": () => div( - h1("Мрежови настройки"), + error && CErr(error.message), + h1("Network settings"), table( tr( td("IP"), @@ -62,10 +80,9 @@ const routes = { ), ) ).$cls("network"), - input("button").$value("Съхрани") + input("button").$value("Save").$click(saveconfig) ), "/config": () => { - loaddata(); return div(); }, "/waiting": () => { @@ -73,11 +90,11 @@ const routes = { setTimeout(() => { loading(0); }, 3000); - return div("Пуйчи"); + return div(""); }, "": () => div( - h1("Несъществуваща страница"), - p("Страницата не е намерена.") + h1("Not Found"), + p("The requested page is not available.") ), }; const onchange = () => { @@ -86,4 +103,4 @@ const onchange = () => { }; router(app, routes, onchange); -loaddata(); +loadconfig(); -- cgit v1.2.3