Pass page title to writePage

This commit is contained in:
Brandon Dyck 2020-04-13 23:43:04 -06:00
parent b5103c106e
commit 00425829c8

View File

@ -54,12 +54,11 @@ func NewHandler(params HandlerParams) http.Handler {
func (h *handler) handleRoot(w http.ResponseWriter, r *http.Request) { func (h *handler) handleRoot(w http.ResponseWriter, r *http.Request) {
body := hm.Terms{ body := hm.Terms{
e.H2()(hm.Text("Welcome!")),
e.A(a.Href(pathCreate))( e.A(a.Href(pathCreate))(
hm.Text("Create event"), hm.Text("Create event"),
), ),
} }
_ = h.writePage(w, body) _ = h.writePage(w, "Welcome!", body)
} }
var timeLabels = []string{ var timeLabels = []string{
@ -137,22 +136,22 @@ func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
latestDate: event.LatestDate, latestDate: event.LatestDate,
} }
body := hm.Terms{ body := hm.Terms{
e.H2()(hm.Text(event.Name)),
e.P()(hm.Text(event.Description)), e.P()(hm.Text(event.Description)),
voteForm(false, state), voteForm(false, state),
} }
_ = h.writePage(w, body) _ = h.writePage(w, event.Name, body)
} }
func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) { func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
// TODO use actual data // TODO Consider redirecting to vote edit page once that exists.
// TODO Use actual data.
state := voteState{ state := voteState{
name: "Suzie Q", name: "Suzie Q",
earliestDate: time.Date(2006, time.May, 3, 0, 0, 0, 0, time.UTC), 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), latestDate: time.Date(2006, time.May, 8, 0, 0, 0, 0, time.UTC),
} }
body := hm.Terms{ body := hm.Terms{
e.H2()(hm.Text("Billy's birthday party")),
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")), e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
e.H3()(hm.Text("Thanks for voting!")), e.H3()(hm.Text("Thanks for voting!")),
e.P()( e.P()(
@ -162,12 +161,11 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
), ),
voteForm(true, state), voteForm(true, state),
} }
_ = h.writePage(w, body) _ = h.writePage(w, "Billy's birthday party", body)
} }
func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) { func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
body := hm.Terms{ body := hm.Terms{
e.H2()(hm.Text("Create an event")),
e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))( e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))(
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")), e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
e.Input(a.Name(fieldNameEventName)), e.Input(a.Name(fieldNameEventName)),
@ -184,10 +182,11 @@ func (h *handler) handleCreate(w http.ResponseWriter, r *http.Request) {
e.Input(a.Type("submit")), e.Input(a.Type("submit")),
), ),
} }
_ = h.writePage(w, body) _ = h.writePage(w, "Create an event", body)
} }
func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) { func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
// TODO consider redirecting to admin
earliest, err := time.Parse(formDateLayout, r.FormValue(fieldNameEarliest)) earliest, err := time.Parse(formDateLayout, r.FormValue(fieldNameEarliest))
if err != nil { if err != nil {
fmt.Fprint(w, "bad earliest date") fmt.Fprint(w, "bad earliest date")
@ -227,7 +226,6 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
const dateDisplayFmt = "Monday, January 2, 2006" const dateDisplayFmt = "Monday, January 2, 2006"
body := hm.Terms{ body := hm.Terms{
e.H2()(hm.Text("Created event!")),
e.P()( e.P()(
hm.Text("You can find it again at "), hm.Text("You can find it again at "),
e.A(a.Href(adminURL))(hm.Text(adminURL)), e.A(a.Href(adminURL))(hm.Text(adminURL)),
@ -251,7 +249,7 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
e.H3()(hm.Text("Latest date")), e.H3()(hm.Text("Latest date")),
hm.Text(latest.Format(dateDisplayFmt)), hm.Text(latest.Format(dateDisplayFmt)),
} }
_ = h.writePage(w, body) _ = h.writePage(w, "Created event", body)
} }
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) { func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
@ -267,7 +265,6 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
} }
body := hm.Terms{ body := hm.Terms{
e.H2()(hm.Text("Edit your event")),
e.Form()( e.Form()(
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")), e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
e.Input( e.Input(
@ -299,21 +296,26 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
e.Input(a.Type("submit")), e.Input(a.Type("submit")),
), ),
} }
_ = h.writePage(w, body) _ = h.writePage(w, "Edit your event", body)
} }
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r) h.mux.ServeHTTP(w, r)
} }
func (h *handler) writePage(w io.Writer, contents hm.Term) error { func (h *handler) writePage(w io.Writer, title string, contents hm.Term) error {
// TODO Need optional subtitles, and titles should be optional.
// Take a Page struct with title, subtitle, and contents.
page := e.Html()( page := e.Html()(
e.Head()( e.Head()(
e.Title()(hm.Text(h.title)), e.Title()(hm.Text(h.title+" — "+title)),
), ),
e.Body()( e.Body()(
e.H1()(e.A(a.Href(h.baseURL+pathRoot))(hm.Text(h.title))), e.H1()(e.A(a.Href(h.baseURL+pathRoot))(hm.Text(h.title))),
e.Div()(contents), e.Div()(
e.H2()(hm.Text(title)),
contents,
),
), ),
) )
_, err := hm.WriteDocument(w, page) _, err := hm.WriteDocument(w, page)