forked from OrgGo/Tasks
when delete returns to the original URL and not /deleted
This commit is contained in:
parent
a64c630767
commit
5f38eb7c52
2
main.go
2
main.go
|
@ -17,9 +17,11 @@ func main() {
|
||||||
views.PopulateTemplates()
|
views.PopulateTemplates()
|
||||||
http.HandleFunc("/", views.ShowAllTasksFunc)
|
http.HandleFunc("/", views.ShowAllTasksFunc)
|
||||||
http.HandleFunc("/complete/", views.CompleteTaskFunc)
|
http.HandleFunc("/complete/", views.CompleteTaskFunc)
|
||||||
|
//delete permanently deletes from db
|
||||||
http.HandleFunc("/delete/", views.DeleteTaskFunc)
|
http.HandleFunc("/delete/", views.DeleteTaskFunc)
|
||||||
http.HandleFunc("/files/", views.UploadedFileHandler)
|
http.HandleFunc("/files/", views.UploadedFileHandler)
|
||||||
http.HandleFunc("/deleted/", views.ShowTrashTaskFunc)
|
http.HandleFunc("/deleted/", views.ShowTrashTaskFunc)
|
||||||
|
//trash moves to recycle bin
|
||||||
http.HandleFunc("/trash/", views.TrashTaskFunc)
|
http.HandleFunc("/trash/", views.TrashTaskFunc)
|
||||||
http.HandleFunc("/edit/", views.EditTaskFunc)
|
http.HandleFunc("/edit/", views.EditTaskFunc)
|
||||||
http.HandleFunc("/completed/", views.ShowCompleteTasksFunc)
|
http.HandleFunc("/completed/", views.ShowCompleteTasksFunc)
|
||||||
|
|
|
@ -4,17 +4,29 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/thewhitetulip/Tasks/db"
|
"github.com/thewhitetulip/Tasks/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
//TrashTaskFunc is used to populate the trash tasks
|
//TrashTaskFunc is used to populate the trash tasks
|
||||||
func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
|
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" {
|
if r.Method == "GET" {
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println("TrashTaskFunc", err)
|
||||||
http.Redirect(w, r, "/trash", http.StatusBadRequest)
|
http.Redirect(w, r, redirectUrl, http.StatusBadRequest)
|
||||||
} else {
|
} else {
|
||||||
err = db.TrashTask(id)
|
err = db.TrashTask(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -22,11 +34,11 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
message = "Task trashed"
|
message = "Task trashed"
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, "/trash", http.StatusFound)
|
http.Redirect(w, r, redirectUrl, http.StatusFound)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
message = "Method not allowed"
|
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 {
|
} else {
|
||||||
message = "Task deleted"
|
message = "Task deleted"
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/deleted", http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue