Browse Source

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

master
Simon Ser 4 years ago
parent
commit
811891134e
No known key found for this signature in database GPG Key ID: FDE7BE0E88F5E48
3 changed files with 38 additions and 15 deletions
  1. +12
    -4
      plugins/caldav/plugin.go
  2. +6
    -5
      plugins/caldav/public/update-event.html
  3. +20
    -6
      plugins/caldav/routes.go

+ 12
- 4
plugins/caldav/plugin.go View File

@@ -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)
},
})



+ 6
- 5
plugins/caldav/public/update-event.html View File

@@ -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>


+ 20
- 6
plugins/caldav/routes.go View File

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


Loading…
Cancel
Save