Make checkboxes for variable dates

This commit is contained in:
Brandon Dyck 2020-04-13 23:00:37 -06:00
parent 1bfbd566fd
commit e964123131

View File

@ -85,22 +85,26 @@ func disableIf(cond bool, input hm.Term) hm.Term {
type voteState struct { type voteState struct {
name string name string
earliestDate, latestDate time.Time
} }
func voteForm(disabled bool, st voteState) hm.Term { func voteForm(disabled bool, st voteState) hm.Term {
var rows = hm.Terms{ var dates []time.Time
e.Thead()( for curr := st.earliestDate; curr.Before(st.latestDate.Add(time.Hour)); curr = curr.AddDate(0, 0, 1) {
e.Th()(), dates = append(dates, curr)
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 ths = hm.Terms{e.Th()()}
for _, date := range dates {
ths = append(ths, e.Th()(hm.Text(date.Format("Jan 2"))))
}
var rows = hm.Terms{e.Thead()(ths)}
for hour := 0; hour < 24; hour++ {
var row = hm.Terms{ var row = hm.Terms{
e.Td()(hm.Text(timeLabels[slot])), e.Td()(hm.Text(timeLabels[hour])),
} }
for day := 0; day < 3; day++ { for day := 0; day < len(dates); day++ {
row = append(row, e.Td()(disableIf(disabled, e.Input(a.Type("checkbox"))))) row = append(row, e.Td()(disableIf(disabled, e.Input(a.Type("checkbox")))))
} }
rows = append(rows, e.Tr()(row)) rows = append(rows, e.Tr()(row))
@ -119,16 +123,26 @@ func voteForm(disabled bool, st voteState) hm.Term {
func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) { func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
// TODO use actual data // TODO use actual data
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),
}
body := hm.Terms{ body := hm.Terms{
e.H2()(hm.Text("Billy's birthday party")), 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.")),
voteForm(false, voteState{}), voteForm(false, state),
} }
_ = h.writePage(w, body) _ = h.writePage(w, 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 use actual data
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),
}
body := hm.Terms{ body := hm.Terms{
e.H2()(hm.Text("Billy's birthday party")), 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.")),
@ -138,7 +152,7 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
e.A(a.Href("#"))(hm.Text("this link")), e.A(a.Href("#"))(hm.Text("this link")),
hm.Text("."), hm.Text("."),
), ),
voteForm(true, voteState{name: "Suzie Q"}), voteForm(true, state),
} }
_ = h.writePage(w, body) _ = h.writePage(w, body)
} }