plugins/caldav: add time inputs in event create/update form
This commit is contained in:
parent
8813faf71e
commit
811891134e
3 changed files with 39 additions and 16 deletions
|
@ -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)
|
||||
},
|
||||
})
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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…
Reference in a new issue