From 7d9758c3f568778f17eee8c0ae84f2fc73780df6 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Thu, 18 Jul 2019 12:44:46 +0530 Subject: [PATCH] Refactor "error" view to a generic "message" --- public.go | 22 +++++++++++----------- public/templates/message.html | 10 ++++++++++ utils.go | 13 +++++++------ 3 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 public/templates/message.html diff --git a/public.go b/public.go index b3238bc..85062d0 100644 --- a/public.go +++ b/public.go @@ -41,10 +41,10 @@ type unsubTpl struct { Blacklist bool } -type errorTpl struct { +type msgTpl struct { publicTpl - ErrorTitle string - ErrorMessage string + MessageTitle string + Message string } var ( @@ -79,8 +79,8 @@ func handleUnsubscribePage(c echo.Context) error { if !regexValidUUID.MatchString(campUUID) || !regexValidUUID.MatchString(subUUID) { - return c.Render(http.StatusBadRequest, "error", - makeErrorTpl("Invalid request", "", + return c.Render(http.StatusBadRequest, "message", + makeMsgTpl("Invalid request", "", `The unsubscription request contains invalid IDs. Please follow the correct link.`)) } @@ -97,8 +97,8 @@ func handleUnsubscribePage(c echo.Context) error { if !blacklist { num, _ := res.RowsAffected() if num == 0 { - return c.Render(http.StatusBadRequest, "error", - makeErrorTpl("Already unsubscribed", "", + return c.Render(http.StatusBadRequest, "message", + makeMsgTpl("Already unsubscribed", "", `You are not subscribed to this mailing list. You may have already unsubscribed.`)) } @@ -119,15 +119,15 @@ func handleLinkRedirect(c echo.Context) error { if !regexValidUUID.MatchString(linkUUID) || !regexValidUUID.MatchString(campUUID) || !regexValidUUID.MatchString(subUUID) { - return c.Render(http.StatusBadRequest, "error", - makeErrorTpl("Invalid link", "", "The link you clicked is invalid.")) + return c.Render(http.StatusBadRequest, "message", + makeMsgTpl("Invalid link", "", "The link you clicked is invalid.")) } var url string if err := app.Queries.RegisterLinkClick.Get(&url, linkUUID, campUUID, subUUID); err != nil { app.Logger.Printf("error fetching redirect link: %s", err) - return c.Render(http.StatusInternalServerError, "error", - makeErrorTpl("Error opening link", "", + return c.Render(http.StatusInternalServerError, "message", + makeMsgTpl("Error opening link", "", "There was an error opening the link. Please try later.")) } diff --git a/public/templates/message.html b/public/templates/message.html new file mode 100644 index 0000000..ae09ae2 --- /dev/null +++ b/public/templates/message.html @@ -0,0 +1,10 @@ +{{ define "message" }} + {{ template "header" .}} + +

{{ .Data.Title }}

+
+ {{ .Data.Message }} +
+ + {{ template "footer" .}} +{{ end }} diff --git a/utils.go b/utils.go index 4c90f11..4be23fd 100644 --- a/utils.go +++ b/utils.go @@ -246,16 +246,17 @@ func normalizeTags(tags []string) []string { return out } -// makeErrorTpl takes error details and returns an errorTpl -// with the error details applied to be rendered in an HTML view. -func makeErrorTpl(pageTitle, heading, desc string) errorTpl { +// makeMsgTpl takes a page title, heading, and message and returns +// a msgTpl that can be rendered as a HTML view. This is used for +// rendering aribtrary HTML views with error and success messages. +func makeMsgTpl(pageTitle, heading, msg string) msgTpl { if heading == "" { heading = pageTitle } - err := errorTpl{} + err := msgTpl{} err.Title = pageTitle - err.ErrorTitle = heading - err.ErrorMessage = desc + err.MessageTitle = heading + err.Message = msg return err }