From 811891134ee9d4da5a217a670ac5a2dcd0202059 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 20 May 2020 19:32:53 +0200 Subject: [PATCH] plugins/caldav: add time inputs in event create/update form --- plugins/caldav/plugin.go | 18 ++++++++++++----- plugins/caldav/public/update-event.html | 11 ++++++----- plugins/caldav/routes.go | 26 +++++++++++++++++++------ 3 files changed, 39 insertions(+), 16 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") - }, - "ornow": func(t time.Time) time.Time { if t.IsZero() { - return time.Now() + return "" } - return t + return t.Format(inputDateLayout) + }, + "formatinputtime": func(t time.Time) string { + if t.IsZero() { + return "" + } + 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 @@
- - - + + +
- - + + +

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")