From 65c0984348889384f0198dc3de4ba7e3fc43966d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Thu, 27 Jun 2024 09:51:52 +0200 Subject: [PATCH] Added page layout errors --- main.go | 38 +++++++++++++++++++--------- page.go | 3 ++- static/css/default_light/main.css | 28 ++++++++++++++++++--- static/css/gruvbox/main.css | 28 ++++++++++++++++++--- static/less/main.less | 42 +++++++++++++++++++++++++++---- views/layouts/main.gotmpl | 4 +++ 6 files changed, 119 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index 7bccbcf..5ea259f 100644 --- a/main.go +++ b/main.go @@ -199,6 +199,20 @@ func httpError(w http.ResponseWriter, err error) { // {{{ j, _ := json.Marshal(resp) w.Write(j) } // }}} +func pageError(w http.ResponseWriter, redirectURL string, pageErr error) { // {{{ + u, err := url.Parse(redirectURL) + if err != nil { + httpError(w, err) + return + } + + values := u.Query() + values.Add("_err", pageErr.Error()) + u.RawQuery = values.Encode() + + w.Header().Add("Location", u.String()) + w.WriteHeader(302) +} // }}} func staticHandler(w http.ResponseWriter, r *http.Request, sess *session.T) { // {{{ if flagDev && !reloadTemplates(w) { @@ -375,13 +389,13 @@ func getPage(layout, page string) (tmpl *template.Template, err error) { // {{{ return } // }}} -func pageIndex(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ +func pageIndex(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ page := Page{ LAYOUT: "main", PAGE: "index", CONFIG: smonConfig.Settings, } - page.Render(w) + page.Render(w, r) } // }}} func actionAreaNew(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ @@ -491,7 +505,7 @@ func actionSectionDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { return } // }}} -func pageProblems(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ +func pageProblems(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ page := Page{ LAYOUT: "main", PAGE: "problems", @@ -524,7 +538,7 @@ func pageProblems(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ "Problems": problems, "ProblemsGrouped": problemsGrouped, } - page.Render(w) + page.Render(w, r) return } // }}} func actionProblemAcknowledge(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ @@ -589,7 +603,7 @@ func pageDatapoints(w http.ResponseWriter, r *http.Request, _ *session.T) { // { page.Data = map[string]any{ "Datapoints": datapoints, } - page.Render(w) + page.Render(w, r) return } // }}} func pageDatapointEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ @@ -624,7 +638,7 @@ func pageDatapointEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { / page.Data = map[string]any{ "Datapoint": datapoint, } - page.Render(w) + page.Render(w, r) return } // }}} func actionDatapointUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ @@ -740,11 +754,11 @@ func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) { "TimeTo": timeTo.Format("2006-01-02T15:04:05"), "Display": display, } - page.Render(w) + page.Render(w, r) return } // }}} -func pageTriggers(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ +func pageTriggers(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ areas, err := TriggersRetrieve() if err != nil { httpError(w, werr.Wrap(err).Log()) @@ -764,7 +778,7 @@ func pageTriggers(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ }, } - page.Render(w) + page.Render(w, r) } // }}} func actionTriggerCreate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ name := r.PathValue("name") @@ -852,7 +866,7 @@ func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { // }, } - page.Render(w) + page.Render(w, r) } // }}} func actionTriggerDatapointAdd(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ triggerID := r.PathValue("id") @@ -974,7 +988,7 @@ func actionTriggerDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { w.WriteHeader(302) } // }}} -func pageConfiguration(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ +func pageConfiguration(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ areas, err := AreaRetrieve() if err != nil { httpError(w, werr.Wrap(err).Log()) @@ -994,7 +1008,7 @@ func pageConfiguration(w http.ResponseWriter, _ *http.Request, _ *session.T) { / }, } - page.Render(w) + page.Render(w, r) } // }}} func actionConfigurationTheme(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ theme := r.FormValue("theme") diff --git a/page.go b/page.go index 18e151b..38ff22c 100644 --- a/page.go +++ b/page.go @@ -22,7 +22,7 @@ type Page struct { Data any } -func (p *Page) Render(w http.ResponseWriter) { +func (p *Page) Render(w http.ResponseWriter, r *http.Request) { tmpl, err := getPage(p.LAYOUT, p.PAGE) if err != nil { httpError(w, we.Wrap(err).Log()) @@ -49,6 +49,7 @@ func (p *Page) Render(w http.ResponseWriter) { "PAGE": p.PAGE, "MENU": p.MENU, "CONFIG": smonConfig.Settings, + "ERROR": r.URL.Query().Get("_err"), "Label": p.Label, "Icon": p.Icon, diff --git a/static/css/default_light/main.css b/static/css/default_light/main.css index 786fa5f..30f8dae 100644 --- a/static/css/default_light/main.css +++ b/static/css/default_light/main.css @@ -12,6 +12,30 @@ html { [onClick] { cursor: pointer; } +#page-error { + display: none; + position: fixed; + z-index: 8192; + width: 500px; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + padding: 48px; + border: 2px solid #a00; + font-weight: bold; + background: #fff; + box-shadow: 10px 10px 15px 0px rgba(0, 0, 0, 0.25); +} +#page-error.show { + display: block; + position: fixed; +} +#page-error .close { + position: absolute; + top: 16px; + right: 16px; + font-size: 1.5em; +} #layout { display: grid; grid-template-areas: "menu content"; @@ -35,9 +59,7 @@ html { #menu .entry > a { display: grid; justify-items: center; - grid-template-rows: 38px - 16px - ; + grid-template-rows: 38px 16px; padding: 16px; color: #7bb8eb; text-decoration: none; diff --git a/static/css/gruvbox/main.css b/static/css/gruvbox/main.css index 91cde42..b69f507 100644 --- a/static/css/gruvbox/main.css +++ b/static/css/gruvbox/main.css @@ -12,6 +12,30 @@ html { [onClick] { cursor: pointer; } +#page-error { + display: none; + position: fixed; + z-index: 8192; + width: 500px; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + padding: 48px; + border: 2px solid #a00; + font-weight: bold; + background: #fff; + box-shadow: 10px 10px 15px 0px rgba(0, 0, 0, 0.25); +} +#page-error.show { + display: block; + position: fixed; +} +#page-error .close { + position: absolute; + top: 16px; + right: 16px; + font-size: 1.5em; +} #layout { display: grid; grid-template-areas: "menu content"; @@ -35,9 +59,7 @@ html { #menu .entry > a { display: grid; justify-items: center; - grid-template-rows: 38px - 16px - ; + grid-template-rows: 38px 16px; padding: 16px; color: #777; text-decoration: none; diff --git a/static/less/main.less b/static/less/main.less index 1d0e3b9..81d35a5 100644 --- a/static/less/main.less +++ b/static/less/main.less @@ -18,6 +18,37 @@ html { cursor: pointer; } +#page-error { + display: none; + position: fixed; + z-index: 8192; + + width: 500px; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + + padding: 48px; + border: 2px solid #a00; + font-weight: bold; + + background: #fff; + box-shadow: 10px 10px 15px 0px rgba(0, 0, 0, 0.25); + + &.show { + display: block; + position: fixed; + + } + + .close { + position: absolute; + top: 16px; + right: 16px; + font-size: 1.5em; + } +} + #layout { display: grid; grid-template-areas: "menu content"; @@ -37,16 +68,17 @@ html { .entry { &.selected { background: @bg3; - a { color: @text2 !important; } + + a { + color: @text2 !important; + } } &>a { display: grid; justify-items: center; grid-template-rows: - 38px - 16px - ; + 38px 16px; padding: 16px; color: @text3; text-decoration: none; @@ -158,6 +190,7 @@ body { h1, h2 { margin-bottom: 4px; + &:first-child { margin-top: 0px; } @@ -247,4 +280,3 @@ label { width: min-content; border-radius: 8px; } - diff --git a/views/layouts/main.gotmpl b/views/layouts/main.gotmpl index ede3178..956a25d 100644 --- a/views/layouts/main.gotmpl +++ b/views/layouts/main.gotmpl @@ -20,6 +20,10 @@ +
+
+ {{ .ERROR }} +
{{ block "menu" . }}{{ end }}