ui has multi-page edit vs new even tho they the same page but templates a lil diff

main
bel 2025-05-05 21:33:31 -06:00
parent 0e3e6c54de
commit 0271f84948
2 changed files with 43 additions and 23 deletions

View File

@ -1,11 +1,6 @@
<html>
<header>
<link rel="stylesheet" href="/experimental/ui/dark.css">
<script>
fill(elem) {
}
</script>
</header>
<body>
<h2>Feeds</h2>
@ -13,7 +8,7 @@
<div>
{{ range feeds }}
<div>
<h4>(<button onclick="fill(this)">{{ namespan "entry.id" .Entry.ID}}</button>) {{ namespan "version.url" .Version.URL }}</h4>
<h4><a href="?edit={{.Entry.ID}}">{{ .Version.URL }}</a></h4>
<div>{{ .Version.Created }} (last {{ .Execution.Executed }})</div>
<div>@{{ .Version.Cron }} ~"{{ .Version.Pattern }}"</div>
<div>{{ .Version.WebhookMethod }} {{ .Version.WebhookURL }} | {{ .Version.WebhookBody }}</div>
@ -21,16 +16,22 @@
{{ end }}
</div>
<br>
<br><hr><br>
<div>
<h3>New</h3>
<h3>
{{ if eq "" .editing.ID }}
New
{{ else }}
Update (<a href="?">clear</a>)
{{ end }}
</h3>
<form method="POST" action="/v1/feeds">
{{ range feedsVersionFields }}
{{ if ne . "Created" }}
{{ range $k, $v := .editing }}
{{ if not (in $k "Created" "Deleted" "Updated" "ID") }}
<div>
<label for="{{ . }}">{{ . }}</label>
<input name="{{ . }}" type="text" />
<label for="{{ $k }}">{{ $k }}</label>
<input name="{{ $k }}" type="text" value="{{ $v }}"/>
</div>
{{ end }}
{{ end }}

View File

@ -2,7 +2,6 @@ package handler
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
@ -30,16 +29,27 @@ func (h Handler) ui(w http.ResponseWriter, r *http.Request) error {
func (h Handler) uiIndex(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
var editing struct {
feeds.Entry `json:",inline"`
feeds.Version `json:",inline"`
}
all := []feeds.Feed{}
if err := feeds.ForEach(ctx, func(f feeds.Feed) error {
all = append(all, f)
if f.Entry.ID == r.URL.Query().Get("edit") {
editing.Entry = f.Entry
editing.Version = f.Version
}
return ctx.Err()
}); err != nil {
return err
}
b, _ := os.ReadFile(path.Join(dir, "index.tmpl"))
tmpl := template.New(r.URL.Path).Funcs(template.FuncMap{
"feeds": func() ([]feeds.Feed, error) {
all := []feeds.Feed{}
err := feeds.ForEach(ctx, func(f feeds.Feed) error {
all = append(all, f)
return ctx.Err()
})
return all, err
"feeds": func() []feeds.Feed {
return all
},
"feedsVersionFields": func() []string {
b, _ := json.Marshal(feeds.Version{})
@ -52,8 +62,8 @@ func (h Handler) uiIndex(w http.ResponseWriter, r *http.Request) error {
slices.Sort(ks)
return ks
},
"namespan": func(k string, v any) string {
return fmt.Sprintf("<span name=%q>%s</span>", k, v)
"in": func(k string, v ...string) bool {
return slices.Contains(v, k)
},
})
@ -62,5 +72,14 @@ func (h Handler) uiIndex(w http.ResponseWriter, r *http.Request) error {
return err
}
return tmpl.Execute(w, nil)
args := map[string]any{}
{
b, _ := json.Marshal(editing)
var m map[string]any
json.Unmarshal(b, &m)
args["editing"] = m
}
return tmpl.Execute(w, args)
}