From 2ced61d304903276c61e71505d818b67f767d056 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Sun, 23 Apr 2023 03:20:01 +0200 Subject: [PATCH] Add the product filter form I need a way to search products by name in the invoice form, when the user adds or changes a product. Since this is something that i have to add too to the product list, i added it now so the function will already be ready. --- pkg/products.go | 102 +++++++++++++++++++++++++---- web/template/products/index.gohtml | 16 ++++- 2 files changed, 106 insertions(+), 12 deletions(-) diff --git a/pkg/products.go b/pkg/products.go index a50b65a..ce34053 100644 --- a/pkg/products.go +++ b/pkg/products.go @@ -8,6 +8,7 @@ import ( "math" "net/http" "strconv" + "strings" ) type ProductEntry struct { @@ -19,14 +20,21 @@ type ProductEntry struct { type productsIndexPage struct { Products []*ProductEntry + Filters *productFilterForm } func IndexProducts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { conn := getConn(r) company := mustGetCompany(r) - tag := r.URL.Query().Get("tag") + locale := getLocale(r) + filters := newProductFilterForm(locale) + if err := filters.Parse(r); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } page := &productsIndexPage{ - Products: mustCollectProductEntries(r.Context(), conn, company, tag), + Products: mustCollectProductEntries(r.Context(), conn, company, filters), + Filters: filters, } mustRenderMainTemplate(w, r, "products/index.gohtml", page) } @@ -152,8 +160,72 @@ func HandleUpdateProduct(w http.ResponseWriter, r *http.Request, params httprout } } -func mustCollectProductEntries(ctx context.Context, conn *Conn, company *Company, tag string) []*ProductEntry { - rows, err := conn.Query(ctx, ` +type productFilterForm struct { + Name *InputField + Tags *TagsField + TagsCondition *ToggleField +} + +func newProductFilterForm(locale *Locale) *productFilterForm { + return &productFilterForm{ + Name: &InputField{ + Name: "number", + Label: pgettext("input", "Invoice Number", locale), + Type: "search", + }, + Tags: &TagsField{ + Name: "tags", + Label: pgettext("input", "Tags", locale), + }, + TagsCondition: &ToggleField{ + Name: "tags_condition", + Label: pgettext("input", "Tags Condition", locale), + Selected: "and", + FirstOption: &ToggleOption{ + Value: "and", + Label: pgettext("tag condition", "All", locale), + Description: gettext("Invoices must have all the specified labels.", locale), + }, + SecondOption: &ToggleOption{ + Value: "or", + Label: pgettext("tag condition", "Any", locale), + Description: gettext("Invoices must have at least one of the specified labels.", locale), + }, + }, + } +} + +func (form *productFilterForm) Parse(r *http.Request) error { + if err := r.ParseForm(); err != nil { + return err + } + form.Name.FillValue(r) + form.Tags.FillValue(r) + form.TagsCondition.FillValue(r) + return nil +} + +func mustCollectProductEntries(ctx context.Context, conn *Conn, company *Company, filters *productFilterForm) []*ProductEntry { + args := []interface{}{company.Id} + where := []string{"product.company_id = $1"} + appendWhere := func(expression string, value interface{}) { + args = append(args, value) + where = append(where, fmt.Sprintf(expression, len(args))) + } + if filters != nil { + name := strings.TrimSpace(filters.Name.String()) + if name != "" { + appendWhere("product.name ilike $%d", "%"+name+"%") + } + if len(filters.Tags.Tags) > 0 { + if filters.TagsCondition.Selected == "and" { + appendWhere("product.tags @> $%d", filters.Tags) + } else { + appendWhere("product.tags && $%d", filters.Tags) + } + } + } + rows := conn.MustQuery(ctx, fmt.Sprintf(` select product.slug , product.name , to_price(price, decimal_digits) @@ -161,19 +233,15 @@ func mustCollectProductEntries(ctx context.Context, conn *Conn, company *Company from product join company using (company_id) join currency using (currency_code) - where product.company_id = $1 and (($2 = '') or (tags @> array[$2]::tag_name[])) + where (%s) order by name - `, company.Id, tag) - if err != nil { - panic(err) - } + `, strings.Join(where, ") AND (")), args...) defer rows.Close() var entries []*ProductEntry for rows.Next() { entry := &ProductEntry{} - err = rows.Scan(&entry.Slug, &entry.Name, &entry.Price, &entry.Tags) - if err != nil { + if err := rows.Scan(&entry.Slug, &entry.Name, &entry.Price, &entry.Tags); err != nil { panic(err) } entries = append(entries, entry) @@ -281,3 +349,15 @@ func (form *productForm) MustFillFromDatabase(ctx context.Context, conn *Conn, s form.Tax, form.Tags)) } + +func HandleProductSearch(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + var name string + query := r.URL.Query() + for k := range query { + vs := query[k] + if len(vs) > 0 { + name = vs[0] + } + } + w.Write([]byte(name)) +} diff --git a/web/template/products/index.gohtml b/web/template/products/index.gohtml index bc49247..cf2f0bf 100644 --- a/web/template/products/index.gohtml +++ b/web/template/products/index.gohtml @@ -19,6 +19,20 @@ {{ define "content" }} {{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.productsIndexPage*/ -}} +
+
+ {{ with .Filters }} + {{ template "input-field" .Name }} + {{ template "tags-field" .Tags | addTagsAttr (print `data-conditions="` .TagsCondition.Name `-field"`) }} + {{ template "toggle-field" .TagsCondition }} + {{ end }} + +
+
@@ -37,7 +51,7 @@
{{- range $index, $tag := .Tags }} {{- if gt $index 0 }}, {{ end -}} - {{ . }} + {{ . }} {{- end }} {{ .Price | formatPrice }}