Show total responses on admin page

This commit is contained in:
2020-04-16 07:51:24 -06:00
parent 00425829c8
commit 051e5f4601
2 changed files with 53 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import (
"io"
"net/http"
"net/url"
"strconv"
"time"
hm "gitlab.codemonkeysoftware.net/b/hatmill"
@ -122,7 +123,7 @@ func voteForm(disabled bool, st voteState) hm.Term {
func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
event, err := h.store.GetEvent(context.Background(), back.GetEventQuery{
event, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: query.Get(keyEventID),
})
// TODO return 404 if event not found
@ -254,9 +255,10 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
eventID := query.Get(keyEventID)
// TODO authenticate with admin code
event, err := h.store.GetEvent(context.Background(), back.GetEventQuery{
AlphaID: query.Get(keyEventID),
metadata, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: eventID,
})
// TODO return 404 if event not found
if err != nil {
@ -264,24 +266,33 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
return
}
responses, err := h.store.GetEventResponses(context.Background(), back.GetEventResponsesQuery{
AlphaID: eventID,
})
if err != nil {
fmt.Fprint(w, err)
return
}
// TODO show results (number of responses, grid)
body := hm.Terms{
e.Form()(
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
e.Input(
a.Name(fieldNameEventName),
a.Value(event.Name),
a.Value(metadata.Name),
),
e.Br(),
e.Label(a.For(fieldNameDescription))(hm.Text("Description")),
e.Textarea(a.Name(fieldNameDescription))(hm.Text(event.Description)),
e.Textarea(a.Name(fieldNameDescription))(hm.Text(metadata.Description)),
e.Br(),
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
e.Input(
a.Name(fieldNameEarliest),
a.Type("date"),
a.Value(event.EarliestDate.Format(formDateLayout)),
a.Value(metadata.EarliestDate.Format(formDateLayout)),
),
e.Br(),
@ -289,12 +300,14 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
e.Input(
a.Name(fieldNameLatest),
a.Type("date"),
a.Value(event.LatestDate.Format(formDateLayout)),
a.Value(metadata.LatestDate.Format(formDateLayout)),
),
e.Br(),
e.Input(a.Type("submit")),
),
e.H3()(hm.Text("Responses")),
e.P()(hm.Text(strconv.Itoa(responses.TotalResponses))),
}
_ = h.writePage(w, "Edit your event", body)
}