diff options
author | Simon Ser <contact@emersion.fr> | 2020-05-20 19:32:53 +0200 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2020-05-20 19:32:53 +0200 |
commit | 811891134ee9d4da5a217a670ac5a2dcd0202059 (patch) | |
tree | 11edbba3b81f32bd13a92bd65852d8a42dc51af8 /plugins/caldav | |
parent | 8813faf71ed98cd60dc099e08d57002acb516878 (diff) | |
download | alps-811891134ee9d4da5a217a670ac5a2dcd0202059.tar.gz alps-811891134ee9d4da5a217a670ac5a2dcd0202059.zip |
plugins/caldav: add time inputs in event create/update form
Diffstat (limited to 'plugins/caldav')
-rw-r--r-- | plugins/caldav/plugin.go | 16 | ||||
-rw-r--r-- | plugins/caldav/public/update-event.html | 11 | ||||
-rw-r--r-- | plugins/caldav/routes.go | 26 |
3 files changed, 38 insertions, 15 deletions
diff --git a/plugins/caldav/plugin.go b/plugins/caldav/plugin.go index caeb12a..e8d14a9 100644 --- a/plugins/caldav/plugin.go +++ b/plugins/caldav/plugin.go @@ -10,6 +10,11 @@ import ( "git.sr.ht/~emersion/alps" ) +const ( + inputDateLayout = "2006-01-02" + inputTimeLayout = "15:04" +) + func sanityCheckURL(u *url.URL) error { req, err := http.NewRequest(http.MethodOptions, u.String(), nil) if err != nil { @@ -61,13 +66,16 @@ func newPlugin(srv *alps.Server) (alps.Plugin, error) { p.TemplateFuncs(template.FuncMap{ "formatinputdate": func(t time.Time) string { - return t.Format("2006-01-02") + if t.IsZero() { + return "" + } + return t.Format(inputDateLayout) }, - "ornow": func(t time.Time) time.Time { + "formatinputtime": func(t time.Time) string { if t.IsZero() { - return time.Now() + return "" } - return t + return t.Format(inputTimeLayout) }, }) diff --git a/plugins/caldav/public/update-event.html b/plugins/caldav/public/update-event.html index 0decb5a..ae091bd 100644 --- a/plugins/caldav/public/update-event.html +++ b/plugins/caldav/public/update-event.html @@ -15,13 +15,14 @@ <input type="text" name="summary" id="summary" value="{{.Event.Props.Text "SUMMARY"}}"> <br> - <!-- TODO: inputs with time --> - <label for="start">Start date:</label> - <input type="date" name="start" id="start" value="{{.Event.DateTimeStart nil | ornow | formatinputdate}}"/> + <label for="start-date">Start date:</label> + <input type="date" name="start-date" id="start-date" value="{{.Event.DateTimeStart nil | formatinputdate}}"/> + <input type="time" name="start-time" id="start-time" value="{{.Event.DateTimeStart nil | formatinputtime}}"/> <br> - <label for="end">End date:</label> - <input type="date" name="end" id="end" value="{{.Event.DateTimeEnd nil | ornow | formatinputdate}}"/> + <label for="end-date">End date:</label> + <input type="date" name="end-date" id="end-date" value="{{.Event.DateTimeEnd nil | formatinputdate}}"/> + <input type="time" name="end-time" id="end-time" value="{{.Event.DateTimeEnd nil | formatinputtime}}"/> <br> <label for="description">Description:</label><br> diff --git a/plugins/caldav/routes.go b/plugins/caldav/routes.go index f0c9573..f9e6bd5 100644 --- a/plugins/caldav/routes.go +++ b/plugins/caldav/routes.go @@ -53,6 +53,21 @@ func parseObjectPath(s string) (string, error) { return string(p), nil } +func parseTime(dateStr, timeStr string) (time.Time, error) { + layout := inputDateLayout + s := dateStr + if timeStr != "" { + layout = inputDateLayout + "T" + inputTimeLayout + s = dateStr + "T" + timeStr + } + t, err := time.Parse(layout, s) + if err != nil { + err = fmt.Errorf("malformed date: %v", err) + return time.Time{}, echo.NewHTTPError(http.StatusBadRequest, err) + } + return t, nil +} + func registerRoutes(p *alps.GoPlugin, u *url.URL) { p.GET("/calendar", func(ctx *alps.Context) error { var start time.Time @@ -242,15 +257,14 @@ func registerRoutes(p *alps.GoPlugin, u *url.URL) { summary := ctx.FormValue("summary") description := ctx.FormValue("description") - start, err := time.Parse("2006-01-02", ctx.FormValue("start")) + // TODO: whole-day events + start, err := parseTime(ctx.FormValue("start-date"), ctx.FormValue("start-time")) if err != nil { - err = fmt.Errorf("malformed start date: %v", err) - return echo.NewHTTPError(http.StatusBadRequest, err) + return err } - end, err := time.Parse("2006-01-02", ctx.FormValue("end")) + end, err := parseTime(ctx.FormValue("end-date"), ctx.FormValue("end-time")) if err != nil { - err = fmt.Errorf("malformed end date: %v", err) - return echo.NewHTTPError(http.StatusBadRequest, err) + return err } if start.After(end) { return echo.NewHTTPError(http.StatusBadRequest, "event start is after its end") |