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)
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")

View File

@ -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,

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -20,6 +20,10 @@
</script>
</head>
<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">
{{ block "menu" . }}{{ end }}
<div id="page">