redirect to correct page on action

This commit is contained in:
Suraj 2016-02-06 15:32:46 +05:30
parent 92bd9b4e71
commit 2f62bcc7f8
6 changed files with 30 additions and 14 deletions

View File

@ -4,7 +4,7 @@
<div class="modal-header">
<h4 class="modal-title" id="newNoteLabel"><span class="glyphicon glyphicon-pencil"></span> Edit Task</h4>
</div>
{{ $categories := .Categories }} {{range $index, $task := .Tasks}}
{{ $categories := .Categories }} {{$referer := .Referer}} {{range $index, $task := .Tasks}}
<div class="modal-body">
<form action="/update/" method="POST">
<div class="form-group">
@ -27,8 +27,8 @@
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" text="submit" class="btn btn-default" />
<a href="{{$referer}}" ><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></a>
<input type="submit" value="Submit" class="btn btn-default" />
</div>
</form>
</div>

View File

@ -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

15
utils/utils.go Normal file
View File

@ -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
}

View File

@ -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"

View File

@ -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 {

View File

@ -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"