Browse Source

Refactor "error" view to a generic "message"

Kailash Nadh 6 years ago
parent
commit
7d9758c3f5
3 changed files with 28 additions and 17 deletions
  1. 11 11
      public.go
  2. 10 0
      public/templates/message.html
  3. 7 6
      utils.go

+ 11 - 11
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."))
 	}
 

+ 10 - 0
public/templates/message.html

@@ -0,0 +1,10 @@
+{{ define "message" }}
+    {{ template "header" .}}
+
+    <h2>{{ .Data.Title }}</h2>
+    <div>
+        {{ .Data.Message }}
+    </div>
+
+    {{ template "footer" .}}
+{{ end }}

+ 7 - 6
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
 }