Unify and cleanup message template

This commit is contained in:
Jan Eitzinger 2023-06-22 18:09:40 +02:00
parent 82b8e8c284
commit 8eda4b306d
5 changed files with 25 additions and 80 deletions

View File

@ -275,7 +275,8 @@ func main() {
rw.WriteHeader(http.StatusUnauthorized)
web.RenderTemplate(rw, r, "login.tmpl", &web.Page{
Title: "Login failed - ClusterCockpit",
Error: err.Error(),
MsgType: "alert-warning",
Message: err.Error(),
Build: buildInfo,
})
})).Methods(http.MethodPost)
@ -285,7 +286,8 @@ func main() {
rw.WriteHeader(http.StatusOK)
web.RenderTemplate(rw, r, "login.tmpl", &web.Page{
Title: "Bye - ClusterCockpit",
Info: "Logout sucessful",
MsgType: "alert-info",
Message: "Logout successful",
Build: buildInfo,
})
}))).Methods(http.MethodPost)
@ -300,7 +302,8 @@ func main() {
rw.WriteHeader(http.StatusUnauthorized)
web.RenderTemplate(rw, r, "login.tmpl", &web.Page{
Title: "Authentication failed - ClusterCockpit",
Error: err.Error(),
MsgType: "alert-danger",
Message: err.Error(),
Build: buildInfo,
})
})

View File

@ -287,8 +287,7 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request) {
if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) {
http.Redirect(rw, r, "/monitoring/users/?user="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusFound)
} else {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Missing Access Rights"})
// web.RenderMessage(rw, "error", "Missing access rights!")
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Error", MsgType: "alert-danger", Message: "Missing Access Rights"})
}
case "name":
usernames, _ := repo.FindColumnValues(user, strings.Trim(splitSearch[1], " "), "user", "username", "name")
@ -299,26 +298,18 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request) {
if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) {
http.Redirect(rw, r, "/monitoring/users/?user=NoUserNameFound", http.StatusPermanentRedirect)
} else {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Missing Access Rights"})
// web.RenderMessage(rw, "error", "Missing access rights!")
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Error", MsgType: "alert-danger", Message: "Missing Access Rights"})
}
}
default:
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: fmt.Sprintf("Unknown search term %s", strings.Trim(splitSearch[0], " "))})
// web.RenderMessage(rw, "error", fmt.Sprintf("Unknown search term %s", strings.Trim(splitSearch[0], " ")))
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warning", MsgType: "alert-warning", Message: fmt.Sprintf("Unknown search term %s", strings.Trim(splitSearch[0], " "))})
}
} else if len(splitSearch) == 1 {
username, project, jobname, err := repo.FindUserOrProjectOrJobname(user, strings.Trim(search, " "))
// err := fmt.Errorf("Blabla")
/* Causes 'http: superfluous response.WriteHeader call' causing SSL error and frontend crash: Cause unknown*/
if err != nil {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "No search result"})
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Info", MsgType: "alert-info", Message: "Search without result"})
return
// web.RenderMessage(rw, "info", "Search with no result")
// log.Errorf("Error while searchbar best guess: %v", err.Error())
}
if username != "" {
@ -330,13 +321,10 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request) {
} else {
http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(strings.Trim(search, " ")), http.StatusFound) // No Result: Probably jobId
}
} else {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Searchbar query parameters malformed"})
// web.RenderMessage(rw, "warn", "Searchbar query parameters malformed")
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Error", MsgType: "alert-danger", Message: "Searchbar query parameters malformed"})
}
} else {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Empty search"})
// web.RenderMessage(rw, "warn", "Empty search")
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warning", MsgType: "alert-warning", Message: "Empty search"})
}
}

View File

@ -17,15 +17,9 @@
<div class="container">
<div class="row">
<div class="col-4 mx-auto">
{{if .Error}}
<div class="alert alert-warning" role="alert">
{{.Error}}
</div>
{{end}}
{{if .Info}}
<div class="alert alert-success" role="alert">
{{.Info}}
{{if .MsgType}}
<div class="alert {{.MsgType}}" role="alert">
{{.Message}}
</div>
{{end}}

View File

@ -13,8 +13,8 @@
{{define "content"}}
<div class="row">
<div class="col">
<div class="alert alert-info" role="alert">
{{.Info}}
<div class="alert {{.MsgType}}" role="alert">
{{.Message}}
</div>
</div>
</div>

View File

@ -82,8 +82,8 @@ type Build struct {
type Page struct {
Title string // Page title
Error string // For generic use (e.g. the exact error message on /login)
Info string // For generic use (e.g. "Logout successfull" on /login)
MsgType string // For generic use in message boxes
Message string // For generic use in message boxes
User auth.User // Information about the currently logged in user (Full User Info)
Roles map[string]auth.Role // Available roles for frontend render checks
Build Build // Latest information about the application
@ -110,43 +110,3 @@ func RenderTemplate(rw http.ResponseWriter, r *http.Request, file string, page *
log.Errorf("Template error: %s", err.Error())
}
}
type Message struct {
Title string
Type string
Message string
Icon string
}
func RenderMessage(rw http.ResponseWriter, msgType string, msg string) {
var page Message
log.Info("render message template")
switch msgType {
case "success":
page.Title = "Success"
page.Type = "alert-success"
case "info":
page.Title = "Info"
page.Type = "alert-info"
case "warn":
page.Title = "Warning"
page.Type = "alert-warning"
case "error":
page.Title = "Error"
page.Type = "alert-danger"
default:
page.Title = "Message"
page.Type = "alert-secondary"
}
t, ok := templates["message.tmpl"]
if !ok {
log.Error("WEB/WEB > template message.tmpl not found")
}
page.Message = msg
rw.Header().Add("Content-Type", "text/html; charset=utf-8")
if err := t.Execute(rw, page); err != nil {
log.Errorf("Template error: %s", err.Error())
}
}