From 2f62bcc7f8248f5284320d419d6d2c2489aa338c Mon Sep 17 00:00:00 2001 From: Suraj Date: Sat, 6 Feb 2016 15:32:46 +0530 Subject: [PATCH] redirect to correct page on action --- templates/edit.html | 6 +++--- types/types.go | 2 ++ utils/utils.go | 15 +++++++++++++++ views/addViews.go | 3 +++ views/deleteViews.go | 14 ++++---------- views/otherViews.go | 4 +++- 6 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 utils/utils.go diff --git a/templates/edit.html b/templates/edit.html index f3bb3e4..d0d0332 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -4,7 +4,7 @@ - {{ $categories := .Categories }} {{range $index, $task := .Tasks}} + {{ $categories := .Categories }} {{$referer := .Referer}} {{range $index, $task := .Tasks}} diff --git a/types/types.go b/types/types.go index 12a495c..fb42a89 100644 --- a/types/types.go +++ b/types/types.go @@ -12,6 +12,7 @@ type Task struct { Created string Priority string Category string + Referer string } //Context is the struct passed to templates @@ -22,6 +23,7 @@ type Context struct { Message string CSRFToken string Categories []CategoryCount + Referer string } //CategoryCount is the struct used to populate the sidebar diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..56cdc96 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,15 @@ +package utils + +import "strings" + +func GetRedirectUrl(referer string) string { + var redirectUrl string + url := strings.Split(referer, "/") + + if len(url) > 4 { + redirectUrl = "/" + strings.Join(url[3:], "/") + } else { + redirectUrl = "/" + } + return redirectUrl +} diff --git a/views/addViews.go b/views/addViews.go index 3f8c25d..1a2897b 100644 --- a/views/addViews.go +++ b/views/addViews.go @@ -16,6 +16,7 @@ import ( "time" "github.com/thewhitetulip/Tasks/db" + "github.com/thewhitetulip/Tasks/utils" ) // UploadedFileHandler is used to handle the uploaded file related requests @@ -143,9 +144,11 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) { log.Println(err) http.Redirect(w, r, "/", http.StatusBadRequest) } else { + redirectUrl := utils.GetRedirectUrl(r.Referer()) task, err := db.GetTaskByID(id) categories := db.GetCategories() task.Categories = categories + task.Referer = redirectUrl if err != nil { task.Message = "Error fetching Tasks" diff --git a/views/deleteViews.go b/views/deleteViews.go index 5b2e326..2b6ca51 100644 --- a/views/deleteViews.go +++ b/views/deleteViews.go @@ -8,29 +8,23 @@ import ( "log" "net/http" "strconv" - "strings" "github.com/thewhitetulip/Tasks/db" + "github.com/thewhitetulip/Tasks/utils" ) //TrashTaskFunc is used to populate the trash tasks func TrashTaskFunc(w http.ResponseWriter, r *http.Request) { //for best UX we want the user to be returned to the page making //the delete transaction, we use the r.Referer() function to get the link - var redirectUrl string - redirect := strings.Split(r.Referer(), "/") - index := len(redirect) - 1 - if len(redirect) == 4 { - redirectUrl = "/" - } else { - redirectUrl = redirect[index] - } + redirectUrl := utils.GetRedirectUrl(r.Referer()) if r.Method == "GET" { id, err := strconv.Atoi(r.URL.Path[len("/trash/"):]) if err != nil { log.Println("TrashTaskFunc", err) - http.Redirect(w, r, redirectUrl, http.StatusBadRequest) + message = "Incorrect command" + http.Redirect(w, r, redirectUrl, http.StatusFound) } else { err = db.TrashTask(id) if err != nil { diff --git a/views/otherViews.go b/views/otherViews.go index e1ce230..7671f77 100644 --- a/views/otherViews.go +++ b/views/otherViews.go @@ -14,6 +14,7 @@ import ( "text/template" "github.com/thewhitetulip/Tasks/db" + "github.com/thewhitetulip/Tasks/utils" ) //PopulateTemplates is used to parse all templates present in @@ -54,6 +55,7 @@ func PopulateTemplates() { //CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { + redirectURL := utils.GetRedirectUrl(r.Referer()) id, err := strconv.Atoi(r.URL.Path[len("/complete/"):]) if err != nil { log.Println(err) @@ -64,7 +66,7 @@ func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) { } else { message = "Task marked complete" } - http.Redirect(w, r, "/", http.StatusFound) + http.Redirect(w, r, redirectURL, http.StatusFound) } } else { message = "Method not allowed"