plugins/caldav: add time inputs in event create/update form

This commit is contained in:
Simon Ser 2020-05-20 19:32:53 +02:00
parent 8813faf71e
commit 811891134e
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
3 changed files with 39 additions and 16 deletions

View file

@ -10,6 +10,11 @@ import (
"git.sr.ht/~emersion/alps" "git.sr.ht/~emersion/alps"
) )
const (
inputDateLayout = "2006-01-02"
inputTimeLayout = "15:04"
)
func sanityCheckURL(u *url.URL) error { func sanityCheckURL(u *url.URL) error {
req, err := http.NewRequest(http.MethodOptions, u.String(), nil) req, err := http.NewRequest(http.MethodOptions, u.String(), nil)
if err != nil { if err != nil {
@ -61,13 +66,16 @@ func newPlugin(srv *alps.Server) (alps.Plugin, error) {
p.TemplateFuncs(template.FuncMap{ p.TemplateFuncs(template.FuncMap{
"formatinputdate": func(t time.Time) string { "formatinputdate": func(t time.Time) string {
return t.Format("2006-01-02")
},
"ornow": func(t time.Time) time.Time {
if t.IsZero() { 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)
}, },
}) })

View file

@ -15,13 +15,14 @@
<input type="text" name="summary" id="summary" value="{{.Event.Props.Text "SUMMARY"}}"> <input type="text" name="summary" id="summary" value="{{.Event.Props.Text "SUMMARY"}}">
<br> <br>
<!-- TODO: inputs with time --> <label for="start-date">Start date:</label>
<label for="start">Start date:</label> <input type="date" name="start-date" id="start-date" value="{{.Event.DateTimeStart nil | formatinputdate}}"/>
<input type="date" name="start" id="start" value="{{.Event.DateTimeStart nil | ornow | formatinputdate}}"/> <input type="time" name="start-time" id="start-time" value="{{.Event.DateTimeStart nil | formatinputtime}}"/>
<br> <br>
<label for="end">End date:</label> <label for="end-date">End date:</label>
<input type="date" name="end" id="end" value="{{.Event.DateTimeEnd nil | ornow | formatinputdate}}"/> <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> <br>
<label for="description">Description:</label><br> <label for="description">Description:</label><br>

View file

@ -53,6 +53,21 @@ func parseObjectPath(s string) (string, error) {
return string(p), nil 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) { func registerRoutes(p *alps.GoPlugin, u *url.URL) {
p.GET("/calendar", func(ctx *alps.Context) error { p.GET("/calendar", func(ctx *alps.Context) error {
var start time.Time var start time.Time
@ -242,15 +257,14 @@ func registerRoutes(p *alps.GoPlugin, u *url.URL) {
summary := ctx.FormValue("summary") summary := ctx.FormValue("summary")
description := ctx.FormValue("description") 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 { if err != nil {
err = fmt.Errorf("malformed start date: %v", err) return err
return echo.NewHTTPError(http.StatusBadRequest, 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 { if err != nil {
err = fmt.Errorf("malformed end date: %v", err) return err
return echo.NewHTTPError(http.StatusBadRequest, err)
} }
if start.After(end) { if start.After(end) {
return echo.NewHTTPError(http.StatusBadRequest, "event start is after its end") return echo.NewHTTPError(http.StatusBadRequest, "event start is after its end")