Use Date instead of Time for dates

This commit is contained in:
2020-04-21 08:25:15 -06:00
parent 99dd0df77a
commit c4aaa1bca5
5 changed files with 52 additions and 43 deletions

View File

@ -9,6 +9,7 @@ import (
"strconv"
"time"
"github.com/rickb777/date"
hm "gitlab.codemonkeysoftware.net/b/hatmill"
a "gitlab.codemonkeysoftware.net/b/hatmill/attribute"
e "gitlab.codemonkeysoftware.net/b/hatmill/element"
@ -84,14 +85,15 @@ func disableIf(cond bool, input hm.Term) hm.Term {
}
type voteState struct {
name string
earliestDate, latestDate time.Time
name string
earliest, latest date.Date
}
func voteForm(disabled bool, st voteState) hm.Term {
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)
dateSpan := st.latest.Sub(st.earliest)
var dates []date.Date
for offset := date.PeriodOfDays(0); offset <= dateSpan; offset++ {
dates = append(dates, st.earliest.Add(offset))
}
var ths = hm.Terms{e.Th()()}
@ -133,8 +135,8 @@ func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
}
state := voteState{
earliestDate: event.EarliestDate,
latestDate: event.LatestDate,
earliest: event.Earliest,
latest: event.Latest,
}
body := hm.Terms{
e.P()(hm.Text(event.Description)),
@ -148,9 +150,9 @@ func (h *handler) handleDoVote(w http.ResponseWriter, r *http.Request) {
// 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),
name: "Suzie Q",
earliest: date.New(2006, time.May, 3),
latest: date.New(2006, time.May, 8),
}
body := hm.Terms{
e.P()(hm.Text("At Billy's house. Bring presents. Eat cake.")),
@ -188,12 +190,12 @@ func (h *handler) handleCreate(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 := date.Parse(formDateLayout, r.FormValue(fieldNameEarliest))
if err != nil {
fmt.Fprint(w, "bad earliest date")
return
}
latest, err := time.Parse(formDateLayout, r.FormValue(fieldNameLatest))
latest, err := date.Parse(formDateLayout, r.FormValue(fieldNameLatest))
if err != nil {
fmt.Fprint(w, "bad latest date")
return
@ -206,10 +208,10 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
description := r.FormValue(fieldNameDescription)
event, err := h.store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: eventName,
Description: description,
EarliestDate: earliest,
LatestDate: latest,
Name: eventName,
Description: description,
Earliest: earliest,
Latest: latest,
})
if err != nil {
fmt.Fprint(w, err)
@ -292,7 +294,7 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
e.Input(
a.Name(fieldNameEarliest),
a.Type("date"),
a.Value(metadata.EarliestDate.Format(formDateLayout)),
a.Value(metadata.Earliest.Format(formDateLayout)),
),
e.Br(),
@ -300,7 +302,7 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
e.Input(
a.Name(fieldNameLatest),
a.Type("date"),
a.Value(metadata.LatestDate.Format(formDateLayout)),
a.Value(metadata.Latest.Format(formDateLayout)),
),
e.Br(),