Added page layout errors

This commit is contained in:
Magnus Åhall 2024-06-27 09:51:52 +02:00
parent 4b21b0ac07
commit 65c0984348
6 changed files with 119 additions and 24 deletions

38
main.go
View File

@ -199,6 +199,20 @@ func httpError(w http.ResponseWriter, err error) { // {{{
j, _ := json.Marshal(resp) j, _ := json.Marshal(resp)
w.Write(j) 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) { // {{{ func staticHandler(w http.ResponseWriter, r *http.Request, sess *session.T) { // {{{
if flagDev && !reloadTemplates(w) { if flagDev && !reloadTemplates(w) {
@ -375,13 +389,13 @@ func getPage(layout, page string) (tmpl *template.Template, err error) { // {{{
return return
} // }}} } // }}}
func pageIndex(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ func pageIndex(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
page := Page{ page := Page{
LAYOUT: "main", LAYOUT: "main",
PAGE: "index", PAGE: "index",
CONFIG: smonConfig.Settings, CONFIG: smonConfig.Settings,
} }
page.Render(w) page.Render(w, r)
} // }}} } // }}}
func actionAreaNew(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ 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 return
} // }}} } // }}}
func pageProblems(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ func pageProblems(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
page := Page{ page := Page{
LAYOUT: "main", LAYOUT: "main",
PAGE: "problems", PAGE: "problems",
@ -524,7 +538,7 @@ func pageProblems(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
"Problems": problems, "Problems": problems,
"ProblemsGrouped": problemsGrouped, "ProblemsGrouped": problemsGrouped,
} }
page.Render(w) page.Render(w, r)
return return
} // }}} } // }}}
func actionProblemAcknowledge(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ 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{ page.Data = map[string]any{
"Datapoints": datapoints, "Datapoints": datapoints,
} }
page.Render(w) page.Render(w, r)
return return
} // }}} } // }}}
func pageDatapointEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ 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{ page.Data = map[string]any{
"Datapoint": datapoint, "Datapoint": datapoint,
} }
page.Render(w) page.Render(w, r)
return return
} // }}} } // }}}
func actionDatapointUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{ 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"), "TimeTo": timeTo.Format("2006-01-02T15:04:05"),
"Display": display, "Display": display,
} }
page.Render(w) page.Render(w, r)
return return
} // }}} } // }}}
func pageTriggers(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{ func pageTriggers(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
areas, err := TriggersRetrieve() areas, err := TriggersRetrieve()
if err != nil { if err != nil {
httpError(w, werr.Wrap(err).Log()) 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) { // {{{ func actionTriggerCreate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
name := r.PathValue("name") 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) { // {{{ func actionTriggerDatapointAdd(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
triggerID := r.PathValue("id") triggerID := r.PathValue("id")
@ -974,7 +988,7 @@ func actionTriggerDelete(w http.ResponseWriter, r *http.Request, _ *session.T) {
w.WriteHeader(302) 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() areas, err := AreaRetrieve()
if err != nil { if err != nil {
httpError(w, werr.Wrap(err).Log()) 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) { // {{{ func actionConfigurationTheme(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
theme := r.FormValue("theme") theme := r.FormValue("theme")

View File

@ -22,7 +22,7 @@ type Page struct {
Data any 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) tmpl, err := getPage(p.LAYOUT, p.PAGE)
if err != nil { if err != nil {
httpError(w, we.Wrap(err).Log()) httpError(w, we.Wrap(err).Log())
@ -49,6 +49,7 @@ func (p *Page) Render(w http.ResponseWriter) {
"PAGE": p.PAGE, "PAGE": p.PAGE,
"MENU": p.MENU, "MENU": p.MENU,
"CONFIG": smonConfig.Settings, "CONFIG": smonConfig.Settings,
"ERROR": r.URL.Query().Get("_err"),
"Label": p.Label, "Label": p.Label,
"Icon": p.Icon, "Icon": p.Icon,

View File

@ -12,6 +12,30 @@ html {
[onClick] { [onClick] {
cursor: pointer; 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 { #layout {
display: grid; display: grid;
grid-template-areas: "menu content"; grid-template-areas: "menu content";
@ -35,9 +59,7 @@ html {
#menu .entry > a { #menu .entry > a {
display: grid; display: grid;
justify-items: center; justify-items: center;
grid-template-rows: 38px grid-template-rows: 38px 16px;
16px
;
padding: 16px; padding: 16px;
color: #7bb8eb; color: #7bb8eb;
text-decoration: none; text-decoration: none;

View File

@ -12,6 +12,30 @@ html {
[onClick] { [onClick] {
cursor: pointer; 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 { #layout {
display: grid; display: grid;
grid-template-areas: "menu content"; grid-template-areas: "menu content";
@ -35,9 +59,7 @@ html {
#menu .entry > a { #menu .entry > a {
display: grid; display: grid;
justify-items: center; justify-items: center;
grid-template-rows: 38px grid-template-rows: 38px 16px;
16px
;
padding: 16px; padding: 16px;
color: #777; color: #777;
text-decoration: none; text-decoration: none;

View File

@ -18,6 +18,37 @@ html {
cursor: pointer; 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 { #layout {
display: grid; display: grid;
grid-template-areas: "menu content"; grid-template-areas: "menu content";
@ -37,16 +68,17 @@ html {
.entry { .entry {
&.selected { &.selected {
background: @bg3; background: @bg3;
a { color: @text2 !important; }
a {
color: @text2 !important;
}
} }
&>a { &>a {
display: grid; display: grid;
justify-items: center; justify-items: center;
grid-template-rows: grid-template-rows:
38px 38px 16px;
16px
;
padding: 16px; padding: 16px;
color: @text3; color: @text3;
text-decoration: none; text-decoration: none;
@ -158,6 +190,7 @@ body {
h1, h1,
h2 { h2 {
margin-bottom: 4px; margin-bottom: 4px;
&:first-child { &:first-child {
margin-top: 0px; margin-top: 0px;
} }
@ -247,4 +280,3 @@ label {
width: min-content; width: min-content;
border-radius: 8px; border-radius: 8px;
} }

View File

@ -20,6 +20,10 @@
</script> </script>
</head> </head>
<body> <body>
<div id="page-error" class="{{ if ne .ERROR "" }}show{{ end }}">
<div class="close" onclick="console.log(this.parentElement.classList.remove('show'))">✖</div>
{{ .ERROR }}
</div>
<div id="layout"> <div id="layout">
{{ block "menu" . }}{{ end }} {{ block "menu" . }}{{ end }}
<div id="page"> <div id="page">