Add the inline form for product tags

This commit is contained in:
jordi fita mas 2023-05-09 12:18:31 +02:00
parent f0f98e200c
commit 856ddde00e
3 changed files with 41 additions and 2 deletions

View File

@ -352,3 +352,36 @@ func HandleProductSearch(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
}
mustRenderStandaloneTemplate(w, r, "products/search.gohtml", products)
}
func ServeEditProductTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
conn := getConn(r)
locale := getLocale(r)
company := getCompany(r)
slug := params[0].Value
form := newTagsForm(companyURI(company, "/products/"+slug+"/tags"), slug, locale)
if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `select array_to_string(tags, ',') from product where slug = $1`, form.Slug).Scan(&form.Tags)) {
http.NotFound(w, r)
return
}
mustRenderStandaloneTemplate(w, r, "tags/edit.gohtml", form)
}
func HandleUpdateProductTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
locale := getLocale(r)
conn := getConn(r)
company := getCompany(r)
slug := params[0].Value
form := newTagsForm(companyURI(company, "/products/"+slug+"/tags/edit"), slug, locale)
if err := form.Parse(r); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := verifyCsrfTokenValid(r); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
if conn.MustGetText(r.Context(), "", "update product set tags = $1 where slug = $2 returning slug", form.Tags, form.Slug) == "" {
http.NotFound(w, r)
}
mustRenderStandaloneTemplate(w, r, "tags/view.gohtml", form)
}

View File

@ -24,6 +24,8 @@ func NewRouter(db *Db) http.Handler {
companyRouter.POST("/products", HandleAddProduct)
companyRouter.GET("/products/:slug", GetProductForm)
companyRouter.PUT("/products/:slug", HandleUpdateProduct)
companyRouter.PUT("/products/:slug/tags", HandleUpdateProductTags)
companyRouter.GET("/products/:slug/tags/edit", ServeEditProductTags)
companyRouter.GET("/invoices", IndexInvoices)
companyRouter.POST("/invoices", HandleAddInvoice)
companyRouter.GET("/invoices/:slug", ServeInvoice)

View File

@ -47,10 +47,14 @@
<tr>
<td></td>
<td><a href="{{ companyURI "/products/"}}{{ .Slug }}" data-hx-target="main" data-hx-boost="true">{{ .Name }}</a></td>
<td>
<td
data-hx-get="{{companyURI "/products/"}}{{ .Slug }}/tags/edit"
data-hx-target="this"
data-hx-swap="outerHTML"
>
{{- range $index, $tag := .Tags }}
{{- if gt $index 0 }}, {{ end -}}
<a href="?tags={{ . }}" data-hx-target="main" data-hx-boost="true">{{ . }}</a>
{{ . }}
{{- end }}
</td>
<td class="numeric">{{ .Price | formatPrice }}</td>