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,8 +79,12 @@ func mustCollectInvoiceEntries(ctx context.Context, conn *Conn, company *Company
maybeAppendWhere("invoice_date >= $%d", filters.FromDate.String(), nil) maybeAppendWhere("invoice_date >= $%d", filters.FromDate.String(), nil)
maybeAppendWhere("invoice_date <= $%d", filters.ToDate.String(), nil) maybeAppendWhere("invoice_date <= $%d", filters.ToDate.String(), nil)
if len(filters.Tags.Tags) > 0 { if len(filters.Tags.Tags) > 0 {
if filters.TagsCondition.String() == "and" {
appendWhere("invoice.tags @> $%d", filters.Tags)
} else {
appendWhere("invoice.tags && $%d", filters.Tags) appendWhere("invoice.tags && $%d", filters.Tags)
} }
}
rows := conn.MustQuery(ctx, fmt.Sprintf(` rows := conn.MustQuery(ctx, fmt.Sprintf(`
select invoice.slug select invoice.slug
, invoice_date , invoice_date
@ -144,6 +148,7 @@ type invoiceFilterForm struct {
FromDate *InputField FromDate *InputField
ToDate *InputField ToDate *InputField
Tags *TagsField Tags *TagsField
TagsCondition *SelectField
} }
func newInvoiceFilterForm(ctx context.Context, conn *Conn, locale *Locale, company *Company) *invoiceFilterForm { 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", Name: "tags",
Label: pgettext("input", "Tags", locale), 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.FromDate.FillValue(r)
form.ToDate.FillValue(r) form.ToDate.FillValue(r)
form.Tags.FillValue(r) form.Tags.FillValue(r)
form.TagsCondition.FillValue(r)
return nil return nil
} }

View File

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