Factor out voting form

This commit is contained in:
Brandon Dyck 2020-04-13 22:37:36 -06:00
parent 1b823fe4f0
commit 1bfbd566fd

View File

@ -67,8 +67,27 @@ var timeLabels = []string{
"12 PM", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
}
func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
// TODO use actual data
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 {
name string
}
func voteForm(disabled bool, st voteState) hm.Term {
var rows = hm.Terms{
e.Thead()(
e.Th()(),
@ -82,46 +101,34 @@ func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
e.Td()(hm.Text(timeLabels[slot])),
}
for day := 0; day < 3; day++ {
row = append(row, e.Td()(e.Input(a.Type("checkbox"))))
row = append(row, e.Td()(disableIf(disabled, e.Input(a.Type("checkbox")))))
}
rows = append(rows, e.Tr()(row))
}
body := hm.Terms{
e.H2()(hm.Text("Billy's birthday party")),
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
e.Form(a.Method(http.MethodPost), a.Action(pathDoVote))(
return e.Form(a.Method(http.MethodPost), a.Action(pathDoVote))(
e.Label()(hm.Text("What's your name?")),
e.Br(),
e.Input(a.Size(40)),
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
body := hm.Terms{
e.H2()(hm.Text("Billy's birthday party")),
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
voteForm(false, voteState{}),
}
_ = h.writePage(w, body)
}
func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
// TODO use actual data
var rows = hm.Terms{
e.Thead()(
e.Th()(),
e.Th()(hm.Text("May 3")),
e.Th()(hm.Text("May 4")),
e.Th()(hm.Text("May 5")),
),
}
for slot := 0; slot < 24; slot++ {
var row = hm.Terms{
e.Td()(hm.Text(timeLabels[slot])),
}
for day := 0; day < 3; day++ {
row = append(row, e.Td()(e.Input(a.Type("checkbox"), a.Disabled())))
}
rows = append(rows, e.Tr()(row))
}
body := hm.Terms{
e.H2()(hm.Text("Billy's birthday party")),
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
@ -131,16 +138,7 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
e.A(a.Href("#"))(hm.Text("this link")),
hm.Text("."),
),
e.Form(a.Method(http.MethodPost), a.Action(pathDoVote))(
e.Label()(hm.Text("What's your name?")),
e.Br(),
e.Input(a.Size(40), a.Disabled(), a.Value("Suzie Q")),
e.Fieldset()(
e.Legend()(hm.Text("When are you available?")),
e.Table()(rows),
),
e.Input(a.Type("submit"), a.Value("Submit")),
),
voteForm(true, voteState{name: "Suzie Q"}),
}
_ = h.writePage(w, body)
}