Hook up the Deactivate All button
This commit is contained in:
parent
3519517b96
commit
c4d53eb993
@ -14,7 +14,11 @@
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
<article class="center">
|
<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>
|
</article>
|
||||||
|
|
||||||
{{ else }}
|
{{ else }}
|
||||||
|
@ -2,8 +2,10 @@ package html
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
|
"encoding/json"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Jaculabilis/intake/core"
|
"github.com/Jaculabilis/intake/core"
|
||||||
@ -18,9 +20,25 @@ func tsToDate(t int) string {
|
|||||||
return tm.Format(time.DateTime)
|
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{
|
var funcs = template.FuncMap{
|
||||||
"raw": rawHtml,
|
"raw": rawHtml,
|
||||||
"tsToDate": tsToDate,
|
"tsToDate": tsToDate,
|
||||||
|
"massDeacVars": massDeactivateVals,
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed intake.css
|
//go:embed intake.css
|
||||||
|
32
web/item.go
32
web/item.go
@ -2,7 +2,9 @@ package web
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Jaculabilis/intake/core"
|
"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})
|
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)
|
||||||
|
}
|
||||||
|
@ -34,6 +34,7 @@ func RunServer(db *core.DB, addr string, port string) {
|
|||||||
handleFunc("GET /item/{source}/{id}", env.getItem)
|
handleFunc("GET /item/{source}/{id}", env.getItem)
|
||||||
handleFunc("DELETE /item/{source}/{id}", env.deleteItem)
|
handleFunc("DELETE /item/{source}/{id}", env.deleteItem)
|
||||||
handleFunc("POST /item/{source}/{id}/action/{action}", env.doAction)
|
handleFunc("POST /item/{source}/{id}/action/{action}", env.doAction)
|
||||||
|
handleFunc("POST /mass-deactivate", env.massDeactivate)
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(bind, nil))
|
log.Fatal(http.ListenAndServe(bind, nil))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user