From 8f7933ffe264e0b39226f63090466e6a3692a90b Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Fri, 14 Apr 2023 02:40:48 +0200 Subject: [PATCH] Allow to select AND or OR for tags filter This is because Oriol thinks that there may be cases where you want to search invoices and such that have any of the selected labels, not all of them, so we agreed on adding an option to choose. The idea is that it will be a toggle, but this requires JavaScript and this commit adds it as a dropdown as a first non-JavaScript step. --- pkg/invoices.go | 17 ++++++++++++++++- web/template/invoices/index.gohtml | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/invoices.go b/pkg/invoices.go index 36a4907..0098e6c 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -79,7 +79,11 @@ func mustCollectInvoiceEntries(ctx context.Context, conn *Conn, company *Company maybeAppendWhere("invoice_date >= $%d", filters.FromDate.String(), nil) maybeAppendWhere("invoice_date <= $%d", filters.ToDate.String(), nil) if len(filters.Tags.Tags) > 0 { - appendWhere("invoice.tags && $%d", filters.Tags) + if filters.TagsCondition.String() == "and" { + appendWhere("invoice.tags @> $%d", filters.Tags) + } else { + appendWhere("invoice.tags && $%d", filters.Tags) + } } rows := conn.MustQuery(ctx, fmt.Sprintf(` select invoice.slug @@ -144,6 +148,7 @@ type invoiceFilterForm struct { FromDate *InputField ToDate *InputField Tags *TagsField + TagsCondition *SelectField } func newInvoiceFilterForm(ctx context.Context, conn *Conn, locale *Locale, company *Company) *invoiceFilterForm { @@ -181,6 +186,15 @@ func newInvoiceFilterForm(ctx context.Context, conn *Conn, locale *Locale, compa Name: "tags", Label: pgettext("input", "Tags", locale), }, + TagsCondition: &SelectField{ + Name: "tags_condition", + Label: pgettext("input", "Tags Condition", locale), + Selected: []string{"and"}, + Options: []*SelectOption{ + {Value: "and", Label: pgettext("logical", "AND", locale)}, + {Value: "or", Label: pgettext("logical", "OR", locale)}, + }, + }, } } @@ -194,6 +208,7 @@ func (form *invoiceFilterForm) Parse(r *http.Request) error { form.FromDate.FillValue(r) form.ToDate.FillValue(r) form.Tags.FillValue(r) + form.TagsCondition.FillValue(r) return nil } diff --git a/web/template/invoices/index.gohtml b/web/template/invoices/index.gohtml index 366d6df..3bf0ebd 100644 --- a/web/template/invoices/index.gohtml +++ b/web/template/invoices/index.gohtml @@ -36,6 +36,7 @@ {{ template "input-field" .ToDate }} {{ template "input-field" .InvoiceNumber }} {{ template "tags-field" .Tags }} + {{ template "select-field" .TagsCondition }} {{ end }}