henwen/front/server.go

316 lines
7.7 KiB
Go
Raw Normal View History

2020-04-01 06:35:15 +00:00
package front
2020-03-24 14:59:27 +00:00
import (
2020-03-31 14:47:40 +00:00
"context"
2020-03-24 14:59:27 +00:00
"fmt"
"io"
"net/http"
2020-03-31 14:47:40 +00:00
"net/url"
2020-03-24 14:59:27 +00:00
"time"
2020-04-01 05:49:08 +00:00
hm "gitlab.codemonkeysoftware.net/b/hatmill"
2020-03-24 14:59:27 +00:00
a "gitlab.codemonkeysoftware.net/b/hatmill/attribute"
e "gitlab.codemonkeysoftware.net/b/hatmill/element"
2020-04-01 06:35:15 +00:00
"gitlab.codemonkeysoftware.net/b/henwen/back"
2020-03-24 14:59:27 +00:00
)
const (
2020-03-31 14:47:40 +00:00
pathRoot = "/"
pathCreate = "/create"
pathDoCreate = "/create/do"
pathAdmin = "/admin"
2020-04-14 04:09:48 +00:00
pathVote = "/vote"
pathDoVote = "/vote/do"
2020-03-24 14:59:27 +00:00
)
2020-04-01 05:31:30 +00:00
type handler struct {
mux *http.ServeMux
2020-04-01 06:35:15 +00:00
store *back.Store
title string
baseURL string
}
type HandlerParams struct {
Title string
2020-04-01 06:35:15 +00:00
Store *back.Store
BaseURL string
2020-04-01 05:31:30 +00:00
}
func NewHandler(params HandlerParams) http.Handler {
2020-04-01 05:31:30 +00:00
h := &handler{
store: params.Store,
title: params.Title,
baseURL: params.BaseURL,
mux: http.NewServeMux(),
2020-04-01 05:31:30 +00:00
}
h.mux.HandleFunc(pathRoot, h.handleRoot)
h.mux.HandleFunc(pathCreate, h.handleCreate)
h.mux.HandleFunc(pathDoCreate, h.handleDoCreate)
h.mux.HandleFunc(pathAdmin, h.handleAdmin)
2020-04-14 04:09:48 +00:00
h.mux.HandleFunc(pathVote, h.handleVote)
h.mux.HandleFunc(pathDoVote, h.handleDoVote)
2020-04-01 05:31:30 +00:00
return h
}
func (h *handler) handleRoot(w http.ResponseWriter, r *http.Request) {
2020-04-01 05:49:08 +00:00
body := hm.Terms{
e.H2()(hm.Text("Welcome!")),
e.A(a.Href(pathCreate))(
hm.Text("Create event"),
),
}
_ = h.writePage(w, body)
2020-04-01 05:31:30 +00:00
}
2020-04-14 04:09:48 +00:00
var timeLabels = []string{
"12 AM", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12 PM", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
}
2020-04-14 04:37:36 +00:00
func disableIf(cond bool, input hm.Term) hm.Term {
if !cond {
return input
}
switch e := input.(type) {
case hm.ParentElement:
e.Attribs = append(e.Attribs, a.Disabled())
return e
case hm.VoidElement:
e.Attribs = append(e.Attribs, a.Disabled())
return e
default:
return e
}
}
type voteState struct {
2020-04-14 05:00:37 +00:00
name string
earliestDate, latestDate time.Time
2020-04-14 04:37:36 +00:00
}
func voteForm(disabled bool, st voteState) hm.Term {
2020-04-14 05:00:37 +00:00
var dates []time.Time
for curr := st.earliestDate; curr.Before(st.latestDate.Add(time.Hour)); curr = curr.AddDate(0, 0, 1) {
dates = append(dates, curr)
}
var ths = hm.Terms{e.Th()()}
for _, date := range dates {
ths = append(ths, e.Th()(hm.Text(date.Format("Jan 2"))))
2020-04-14 04:09:48 +00:00
}
2020-04-14 05:00:37 +00:00
var rows = hm.Terms{e.Thead()(ths)}
for hour := 0; hour < 24; hour++ {
2020-04-14 04:09:48 +00:00
var row = hm.Terms{
2020-04-14 05:00:37 +00:00
e.Td()(hm.Text(timeLabels[hour])),
2020-04-14 04:09:48 +00:00
}
2020-04-14 05:00:37 +00:00
for day := 0; day < len(dates); day++ {
2020-04-14 04:37:36 +00:00
row = append(row, e.Td()(disableIf(disabled, e.Input(a.Type("checkbox")))))
2020-04-14 04:09:48 +00:00
}
rows = append(rows, e.Tr()(row))
}
2020-04-14 04:37:36 +00:00
return e.Form(a.Method(http.MethodPost), a.Action(pathDoVote))(
e.Label()(hm.Text("What's your name?")),
e.Br(),
disableIf(disabled, e.Input(a.Size(40), a.Value(st.name))),
e.Fieldset()(
e.Legend()(hm.Text("When are you available?")),
e.Table()(rows),
),
e.Input(a.Type("submit"), a.Value("Submit")),
)
}
func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
// TODO use actual data
2020-04-14 05:00:37 +00:00
state := voteState{
name: "Suzie Q",
earliestDate: time.Date(2006, time.May, 3, 0, 0, 0, 0, time.UTC),
latestDate: time.Date(2006, time.May, 8, 0, 0, 0, 0, time.UTC),
}
2020-04-14 04:09:48 +00:00
body := hm.Terms{
e.H2()(hm.Text("Billy's birthday party")),
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
2020-04-14 05:00:37 +00:00
voteForm(false, state),
2020-04-14 04:09:48 +00:00
}
_ = h.writePage(w, body)
}
func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
// TODO use actual data
2020-04-14 05:00:37 +00:00
state := voteState{
name: "Suzie Q",
earliestDate: time.Date(2006, time.May, 3, 0, 0, 0, 0, time.UTC),
latestDate: time.Date(2006, time.May, 8, 0, 0, 0, 0, time.UTC),
}
2020-04-14 04:09:48 +00:00
body := hm.Terms{
e.H2()(hm.Text("Billy's birthday party")),
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
e.H3()(hm.Text("Thanks for voting!")),
e.P()(
hm.Text("You can edit your response anytime at "),
e.A(a.Href("#"))(hm.Text("this link")),
hm.Text("."),
),
2020-04-14 05:00:37 +00:00
voteForm(true, state),
2020-04-14 04:09:48 +00:00
}
_ = h.writePage(w, body)
}
2020-04-01 05:31:30 +00:00
func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
2020-04-01 05:49:08 +00:00
body := hm.Terms{
e.H2()(hm.Text("Create an event")),
e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))(
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
e.Input(a.Name(fieldNameEventName)),
e.Label(a.For(fieldNameDescription))(hm.Text("Description")),
e.Textarea(a.Name(fieldNameDescription), a.Placeholder("What's going on?"))(),
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
e.Input(a.Name(fieldNameEarliest), a.Type("date")),
e.Label(a.For(fieldNameLatest))(hm.Text("Latest date")),
e.Input(a.Name(fieldNameLatest), a.Type("date")),
e.Input(a.Type("submit")),
),
}
_ = h.writePage(w, body)
2020-04-01 05:31:30 +00:00
}
func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
2020-04-01 06:10:55 +00:00
earliest, err := time.Parse(formDateLayout, r.FormValue(fieldNameEarliest))
2020-04-01 05:31:30 +00:00
if err != nil {
fmt.Fprint(w, "bad earliest date")
return
}
2020-04-01 06:10:55 +00:00
latest, err := time.Parse(formDateLayout, r.FormValue(fieldNameLatest))
2020-04-01 05:31:30 +00:00
if err != nil {
fmt.Fprint(w, "bad latest date")
return
}
eventName := r.FormValue(fieldNameEventName)
if eventName == "" {
fmt.Fprint(w, "event name is required")
return
}
description := r.FormValue(fieldNameDescription)
2020-04-01 06:35:15 +00:00
event, err := h.store.CreateEvent(context.Background(), back.CreateEventCommand{
2020-04-01 05:49:08 +00:00
Name: eventName,
Description: description,
EarliestDate: earliest,
LatestDate: latest,
})
if err != nil {
fmt.Fprint(w, err)
return
2020-03-24 14:59:27 +00:00
}
2020-04-01 05:49:08 +00:00
var adminQuery = make(url.Values)
adminQuery.Add(keyEventID, event.AlphaID)
adminQuery.Add(keyAdminCode, event.AdminCode)
adminURL := h.baseURL + pathAdmin + "?" + adminQuery.Encode()
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
const dateDisplayFmt = "Monday, January 2, 2006"
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
body := hm.Terms{
e.H2()(hm.Text("Created event!")),
e.P()(
hm.Text("You can find it again at "),
e.A(a.Href(adminURL))(hm.Text(adminURL)),
hm.Text("."),
),
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
e.H3()(hm.Text("Name")),
hm.Text(eventName),
2020-04-01 04:36:41 +00:00
2020-04-01 05:49:08 +00:00
e.H3()(hm.Text("Description")),
hm.Text(description),
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
e.H3()(hm.Text("Earliest date")),
hm.Text(earliest.Format(dateDisplayFmt)),
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
e.H3()(hm.Text("Latest date")),
hm.Text(latest.Format(dateDisplayFmt)),
2020-03-24 14:59:27 +00:00
}
_ = h.writePage(w, body)
2020-03-24 14:59:27 +00:00
}
2020-04-01 05:49:08 +00:00
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
2020-04-01 06:35:15 +00:00
event, err := h.store.GetEvent(context.Background(), back.GetEventQuery{
2020-04-01 05:49:08 +00:00
AlphaID: query.Get(keyEventID),
AdminCode: query.Get(keyAdminCode),
2020-04-01 04:36:41 +00:00
})
if err != nil {
2020-04-01 05:49:08 +00:00
fmt.Fprint(w, err)
return
2020-04-01 04:36:41 +00:00
}
2020-04-01 05:49:08 +00:00
body := hm.Terms{
e.H2()(hm.Text("Edit your event")),
2020-04-01 06:37:39 +00:00
e.Form()(
2020-04-01 05:49:08 +00:00
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
2020-04-01 04:36:41 +00:00
e.Input(
a.Name(fieldNameEventName),
a.Value(event.Name),
),
e.Br(),
2020-04-01 05:49:08 +00:00
e.Label(a.For(fieldNameDescription))(hm.Text("Description")),
2020-04-01 06:37:39 +00:00
e.Textarea(a.Name(fieldNameDescription))(hm.Text(event.Description)),
2020-04-01 04:36:41 +00:00
e.Br(),
2020-04-01 05:49:08 +00:00
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
2020-04-01 04:36:41 +00:00
e.Input(
a.Name(fieldNameEarliest),
a.Type("date"),
2020-04-01 06:10:55 +00:00
a.Value(event.EarliestDate.Format(formDateLayout)),
2020-04-01 04:36:41 +00:00
),
e.Br(),
2020-04-01 05:49:08 +00:00
e.Label(a.For(fieldNameLatest))(hm.Text("Latest date")),
2020-04-01 04:36:41 +00:00
e.Input(
a.Name(fieldNameLatest),
a.Type("date"),
2020-04-01 06:10:55 +00:00
a.Value(event.LatestDate.Format(formDateLayout)),
2020-04-01 04:36:41 +00:00
),
e.Br(),
e.Input(a.Type("submit")),
),
}
_ = h.writePage(w, body)
2020-03-31 12:58:44 +00:00
}
2020-04-01 05:49:08 +00:00
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r)
}
2020-04-01 04:36:41 +00:00
func (h *handler) writePage(w io.Writer, contents hm.Term) error {
2020-04-01 05:49:08 +00:00
page := e.Html()(
e.Head()(
e.Title()(hm.Text(h.title)),
2020-03-31 12:58:44 +00:00
),
2020-04-01 05:49:08 +00:00
e.Body()(
e.H1()(hm.Text(h.title)),
2020-04-01 05:49:08 +00:00
e.Div()(contents),
),
)
_, err := hm.WriteDocument(w, page)
return err
}
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
const (
fieldNameEarliest = "earliestDate"
fieldNameLatest = "latestDate"
fieldNameEventName = "eventName"
fieldNameDescription = "eventDescription"
)
2020-03-24 14:59:27 +00:00
2020-04-01 05:49:08 +00:00
const keyEventID = "event_id"
const keyAdminCode = "admin_code"
2020-03-24 14:59:27 +00:00
2020-04-01 06:10:55 +00:00
const formDateLayout = "2006-01-02"