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.
This commit is contained in:
jordi fita mas 2023-04-14 02:40:48 +02:00
parent 8c592cfe5e
commit 8f7933ffe2
2 changed files with 17 additions and 1 deletions

View File

@ -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
}

View File

@ -36,6 +36,7 @@
{{ template "input-field" .ToDate }}
{{ template "input-field" .InvoiceNumber }}
{{ template "tags-field" .Tags }}
{{ template "select-field" .TagsCondition }}
{{ end }}
<button type="submit">{{( pgettext "Filter" "action" )}}</button>
</form>