Hook up the Deactivate All button

This commit is contained in:
Tim Van Baak 2025-01-29 22:21:17 -08:00
parent 3519517b96
commit c4d53eb993
4 changed files with 58 additions and 3 deletions

View File

@ -14,7 +14,11 @@
{{ end }}
<article class="center">
<button>Deactivate All</button>
<button
hx-post="/mass-deactivate"
hx-vals='{{ massDeacVars .Items }}'
hx-confirm="Deactivate {{ len .Items }} items?"
>Deactivate All</button>
</article>
{{ else }}

View File

@ -2,8 +2,10 @@ package html
import (
"embed"
"encoding/json"
"html/template"
"io"
"log"
"time"
"github.com/Jaculabilis/intake/core"
@ -18,9 +20,25 @@ func tsToDate(t int) string {
return tm.Format(time.DateTime)
}
func massDeactivateVals(items []core.Item) string {
var shorts []string
for _, item := range items {
shorts = append(shorts, core.FormatAsShort(item))
}
massDeac := struct {
Items []string `json:"items"`
}{shorts}
vals, err := json.Marshal(massDeac)
if err != nil {
log.Printf("error serializing mass deactivate list: %v", err)
}
return string(vals)
}
var funcs = template.FuncMap{
"raw": rawHtml,
"tsToDate": tsToDate,
"raw": rawHtml,
"tsToDate": tsToDate,
"massDeacVars": massDeactivateVals,
}
//go:embed intake.css

View File

@ -2,7 +2,9 @@ package web
import (
"encoding/json"
"log"
"net/http"
"strings"
"time"
"github.com/Jaculabilis/intake/core"
@ -85,3 +87,33 @@ func (env *Env) doAction(writer http.ResponseWriter, req *http.Request) {
html.Item(writer, html.ItemData{Item: newItem})
}
func (env *Env) massDeactivate(writer http.ResponseWriter, req *http.Request) {
if err := req.ParseForm(); err != nil {
log.Printf("error parsing form data: %v", err)
http.Error(writer, "", http.StatusBadRequest)
return
}
for _, item := range req.PostForm["items"] {
i := strings.Index(item, "/")
if i == -1 {
log.Printf("error: invalid source/item: %s", item)
http.Error(writer, "", http.StatusBadRequest)
return
}
}
for _, item := range req.PostForm["items"] {
i := strings.Index(item, "/")
source := item[:i]
id := item[i+1:]
active, err := core.DeactivateItem(env.db, source, id)
if err != nil {
log.Printf("error: failed to deactivate %s/%s: %v", source, id, err)
}
if active {
log.Printf("deactivated %s/%s", source, id)
}
}
writer.Header()["HX-Refresh"] = []string{"true"}
http.Error(writer, "ok", http.StatusNoContent)
}

View File

@ -34,6 +34,7 @@ func RunServer(db *core.DB, addr string, port string) {
handleFunc("GET /item/{source}/{id}", env.getItem)
handleFunc("DELETE /item/{source}/{id}", env.deleteItem)
handleFunc("POST /item/{source}/{id}/action/{action}", env.doAction)
handleFunc("POST /mass-deactivate", env.massDeactivate)
log.Fatal(http.ListenAndServe(bind, nil))
}