Redirect from POST handlers

This commit is contained in:
Brandon Dyck 2020-09-27 21:11:06 -06:00
parent a64df092a5
commit fa330e4397

View File

@ -8,7 +8,6 @@ import (
"net/http"
"net/url"
"strconv"
"time"
"github.com/rickb777/date"
hm "gitlab.codemonkeysoftware.net/b/hatmill"
@ -18,12 +17,14 @@ import (
)
const (
pathRoot = "/"
pathCreate = "/create"
pathDoCreate = "/create/do"
pathAdmin = "/admin"
pathVote = "/vote"
pathDoVote = "/vote/do"
pathRoot = "/"
pathCreate = "/create"
pathDoCreate = "/create/do"
pathCreateSuccess = "/create/success"
pathAdmin = "/admin"
pathVote = "/vote"
pathDoVote = "/vote/do"
pathVoteSuccess = "/vote/success"
)
type handler struct {
@ -52,6 +53,8 @@ func NewHandler(params HandlerParams) http.Handler {
h.mux.HandleFunc(pathAdmin, h.handleAdmin)
h.mux.HandleFunc(pathVote, h.handleVote)
h.mux.HandleFunc(pathDoVote, h.handleDoVote)
h.mux.HandleFunc(pathVoteSuccess, h.handleVoteSuccess)
h.mux.HandleFunc(pathCreateSuccess, h.handleCreateSuccess)
return h
}
@ -175,7 +178,7 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
}
cmd.DateHours[d][hour] = struct{}{}
}
_, err = h.store.CreateEventResponse(r.Context(), cmd)
eventResponse, err := h.store.CreateEventResponse(r.Context(), cmd)
if err != nil {
log.Println(err)
// TODO handle not found
@ -183,16 +186,41 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
return
}
// TODO Consider redirecting to vote edit page once that exists.
var successQuery = make(url.Values)
successQuery.Add(fieldNameEventID, cmd.EventAlphaID)
successQuery.Add(fieldNameResponseID, eventResponse.ResponseAlphaID)
http.Redirect(w, r, pathVoteSuccess+"?"+successQuery.Encode(), http.StatusSeeOther)
}
func (h *handler) handleVoteSuccess(w http.ResponseWriter, r *http.Request) {
eventResponse, err := h.store.GetEventResponse(r.Context(), back.GetEventResponseQuery{
EventAlphaID: r.URL.Query().Get(fieldNameEventID),
ResponseAlphaID: r.URL.Query().Get(fieldNameResponseID),
})
if err != nil {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{
AlphaID: r.URL.Query().Get(fieldNameEventID),
})
if err != nil {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
// TODO Use actual data.
state := voteState{
name: r.Form.Get(fieldNameGuestName),
earliest: date.New(2006, time.May, 3),
latest: date.New(2006, time.May, 8),
name: eventResponse.GuestName,
earliest: event.Earliest,
latest: event.Latest,
}
body := hm.Terms{
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
e.P()(hm.Text(event.Description)),
e.H3()(hm.Text("Thanks for voting!")),
e.P()(
hm.Text("You can edit your response anytime at "),
@ -201,7 +229,7 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
),
voteForm(true, state),
}
_ = h.writePage(w, "Billy's birthday party", body)
_ = h.writePage(w, event.Name, body)
}
func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
@ -254,13 +282,32 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, err)
return
}
var successQuery = make(url.Values)
successQuery.Add(fieldNameEventID, event.AlphaID)
successQuery.Add(fieldNameAdminCode, event.AdminCode)
http.Redirect(w, r, pathCreateSuccess+"?"+successQuery.Encode(), http.StatusSeeOther)
}
func (h *handler) handleCreateSuccess(w http.ResponseWriter, r *http.Request) {
eventID := r.URL.Query().Get(fieldNameEventID)
event, err := h.store.GetEventMetadata(r.Context(), back.GetEventMetadataQuery{
AlphaID: eventID,
})
if err != nil {
log.Println(err)
// TODO handle not found
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
var adminQuery = make(url.Values)
adminQuery.Add(fieldNameEventID, event.AlphaID)
adminQuery.Add(fieldNameAdminCode, event.AdminCode)
adminQuery.Add(fieldNameEventID, eventID)
adminQuery.Add(fieldNameAdminCode, r.URL.Query().Get(fieldNameAdminCode))
adminURL := h.baseURL + pathAdmin + "?" + adminQuery.Encode()
var voteQuery = make(url.Values)
voteQuery.Add(fieldNameEventID, event.AlphaID)
voteQuery.Add(fieldNameEventID, eventID)
voteURL := h.baseURL + pathVote + "?" + voteQuery.Encode()
const dateDisplayFmt = "Monday, January 2, 2006"
@ -278,16 +325,16 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
),
e.H3()(hm.Text("Name")),
hm.Text(eventName),
hm.Text(event.Name),
e.H3()(hm.Text("Description")),
hm.Text(description),
hm.Text(event.Description),
e.H3()(hm.Text("Earliest date")),
hm.Text(earliest.Format(dateDisplayFmt)),
hm.Text(event.Earliest.Format(dateDisplayFmt)),
e.H3()(hm.Text("Latest date")),
hm.Text(latest.Format(dateDisplayFmt)),
hm.Text(event.Latest.Format(dateDisplayFmt)),
}
_ = h.writePage(w, "Created event", body)
}
@ -327,6 +374,8 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
e.Textarea(a.Name(fieldNameDescription))(hm.Text(metadata.Description)),
e.Br(),
// TODO Should the date fields be disabled, or should we cull invalid
// response times after changing event dates?
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
e.Input(
a.Name(fieldNameEarliest),
@ -383,6 +432,7 @@ const (
fieldNameGuestName = "guest_name"
fieldNameEventID = "event_id"
fieldNameAdminCode = "admin_code"
fieldNameResponseID = "response_id"
)
const formDateLayout = "2006-01-02"