From 970340277d60a9eda3a8f51482875a6bb506c59e Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Wed, 10 May 2023 18:56:07 +0200 Subject: [PATCH] Add the contact filter form --- pkg/contacts.go | 91 ++++++++++++++++++++++++++---- web/template/contacts/index.gohtml | 14 +++++ 2 files changed, 94 insertions(+), 11 deletions(-) diff --git a/pkg/contacts.go b/pkg/contacts.go index 830536b..01215c1 100644 --- a/pkg/contacts.go +++ b/pkg/contacts.go @@ -2,9 +2,11 @@ package pkg import ( "context" + "fmt" "github.com/julienschmidt/httprouter" "html/template" "net/http" + "strings" ) type ContactEntry struct { @@ -17,14 +19,21 @@ type ContactEntry struct { type ContactsIndexPage struct { Contacts []*ContactEntry + Filters *contactFilterForm } func IndexContacts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { conn := getConn(r) company := mustGetCompany(r) - tag := r.URL.Query().Get("tag") + locale := getLocale(r) + filters := newContactFilterForm(locale) + if err := filters.Parse(r); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } page := &ContactsIndexPage{ - Contacts: mustCollectContactEntries(r.Context(), conn, company, tag), + Contacts: mustCollectContactEntries(r.Context(), conn, company, filters), + Filters: filters, } mustRenderMainTemplate(w, r, "contacts/index.gohtml", page) } @@ -113,27 +122,87 @@ func HandleUpdateContact(w http.ResponseWriter, r *http.Request, params httprout htmxRedirect(w, r, companyURI(mustGetCompany(r), "/contacts")) } -func mustCollectContactEntries(ctx context.Context, conn *Conn, company *Company, tag string) []*ContactEntry { - rows, err := conn.Query(ctx, ` +type contactFilterForm struct { + Name *InputField + Tags *TagsField + TagsCondition *ToggleField +} + +func newContactFilterForm(locale *Locale) *contactFilterForm { + return &contactFilterForm{ + Name: &InputField{ + Name: "number", + Label: pgettext("input", "Name", 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 *contactFilterForm) 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 mustCollectContactEntries(ctx context.Context, conn *Conn, company *Company, filters *contactFilterForm) []*ContactEntry { + args := []interface{}{company.Id} + where := []string{"contact.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("contact.business_name ilike $%d", "%"+name+"%") + } + if len(filters.Tags.Tags) > 0 { + if filters.TagsCondition.Selected == "and" { + appendWhere("contact.tags @> $%d", filters.Tags) + } else { + appendWhere("contact.tags && $%d", filters.Tags) + } + } + } + rows := conn.MustQuery(ctx, fmt.Sprintf(` select slug , business_name , email , phone , tags from contact - where contact.company_id = $1 and (($2 = '') or (tags @> array[$2]::tag_name[])) + where (%s) order by business_name - `, company.Id, tag) - if err != nil { - panic(err) - } + `, strings.Join(where, ") AND (")), args...) defer rows.Close() var entries []*ContactEntry for rows.Next() { entry := &ContactEntry{} - err = rows.Scan(&entry.Slug, &entry.Name, &entry.Email, &entry.Phone, &entry.Tags) - if err != nil { + if err := rows.Scan(&entry.Slug, &entry.Name, &entry.Email, &entry.Phone, &entry.Tags); err != nil { panic(err) } entries = append(entries, entry) diff --git a/web/template/contacts/index.gohtml b/web/template/contacts/index.gohtml index 92d1c92..068adde 100644 --- a/web/template/contacts/index.gohtml +++ b/web/template/contacts/index.gohtml @@ -18,6 +18,20 @@ {{ define "content" }} {{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.ContactsIndexPage*/ -}} +
+
+ {{ with .Filters }} + {{ template "input-field" .Name }} + {{ template "tags-field" .Tags | addTagsAttr (print `data-conditions="` .TagsCondition.Name `-field"`) }} + {{ template "toggle-field" .TagsCondition }} + {{ end }} + +
+