From 5f38eb7c524a6cd780fc65a517a530d2650e907a Mon Sep 17 00:00:00 2001 From: Suraj Date: Mon, 1 Feb 2016 19:56:06 +0530 Subject: [PATCH] when delete returns to the original URL and not /deleted --- main.go | 2 ++ views/deleteViews.go | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index d5f7e94..6cec264 100644 --- a/main.go +++ b/main.go @@ -17,9 +17,11 @@ func main() { views.PopulateTemplates() http.HandleFunc("/", views.ShowAllTasksFunc) http.HandleFunc("/complete/", views.CompleteTaskFunc) + //delete permanently deletes from db http.HandleFunc("/delete/", views.DeleteTaskFunc) http.HandleFunc("/files/", views.UploadedFileHandler) http.HandleFunc("/deleted/", views.ShowTrashTaskFunc) + //trash moves to recycle bin http.HandleFunc("/trash/", views.TrashTaskFunc) http.HandleFunc("/edit/", views.EditTaskFunc) http.HandleFunc("/completed/", views.ShowCompleteTasksFunc) diff --git a/views/deleteViews.go b/views/deleteViews.go index 7d01c4a..945ecde 100644 --- a/views/deleteViews.go +++ b/views/deleteViews.go @@ -4,17 +4,29 @@ import ( "log" "net/http" "strconv" + "strings" "github.com/thewhitetulip/Tasks/db" ) //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] + } + if r.Method == "GET" { id, err := strconv.Atoi(r.URL.Path[len("/trash/"):]) if err != nil { - log.Println(err) - http.Redirect(w, r, "/trash", http.StatusBadRequest) + log.Println("TrashTaskFunc", err) + http.Redirect(w, r, redirectUrl, http.StatusBadRequest) } else { err = db.TrashTask(id) if err != nil { @@ -22,11 +34,11 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) { } else { message = "Task trashed" } - http.Redirect(w, r, "/trash", http.StatusFound) + http.Redirect(w, r, redirectUrl, http.StatusFound) } } else { message = "Method not allowed" - http.Redirect(w, r, "/trash", http.StatusFound) + http.Redirect(w, r, redirectUrl, http.StatusFound) } } @@ -95,7 +107,7 @@ func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) { } else { message = "Task deleted" } - http.Redirect(w, r, "/", http.StatusFound) + http.Redirect(w, r, "/deleted", http.StatusFound) } } } else {