Add the contact filter form

This commit is contained in:
jordi fita mas 2023-05-10 18:56:07 +02:00
parent 856ddde00e
commit 970340277d
2 changed files with 94 additions and 11 deletions

View File

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

View File

@ -18,6 +18,20 @@
{{ define "content" }}
{{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.ContactsIndexPage*/ -}}
<div aria-label="{{( pgettext "Filters" "title" )}}">
<form method="GET" action="{{ companyURI "/contacts"}}"
data-hx-target="main"
data-hx-boost="true"
data-hx-trigger="change,search,submit"
>
{{ with .Filters }}
{{ template "input-field" .Name }}
{{ template "tags-field" .Tags | addTagsAttr (print `data-conditions="` .TagsCondition.Name `-field"`) }}
{{ template "toggle-field" .TagsCondition }}
{{ end }}
<button type="submit">{{( pgettext "Filter" "action" )}}</button>
</form>
</div>
<table>
<thead>
<tr>