Add the contact filter form
This commit is contained in:
parent
856ddde00e
commit
970340277d
|
@ -2,9 +2,11 @@ package pkg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ContactEntry struct {
|
type ContactEntry struct {
|
||||||
|
@ -17,14 +19,21 @@ type ContactEntry struct {
|
||||||
|
|
||||||
type ContactsIndexPage struct {
|
type ContactsIndexPage struct {
|
||||||
Contacts []*ContactEntry
|
Contacts []*ContactEntry
|
||||||
|
Filters *contactFilterForm
|
||||||
}
|
}
|
||||||
|
|
||||||
func IndexContacts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
func IndexContacts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
conn := getConn(r)
|
conn := getConn(r)
|
||||||
company := mustGetCompany(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{
|
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)
|
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"))
|
htmxRedirect(w, r, companyURI(mustGetCompany(r), "/contacts"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustCollectContactEntries(ctx context.Context, conn *Conn, company *Company, tag string) []*ContactEntry {
|
type contactFilterForm struct {
|
||||||
rows, err := conn.Query(ctx, `
|
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
|
select slug
|
||||||
, business_name
|
, business_name
|
||||||
, email
|
, email
|
||||||
, phone
|
, phone
|
||||||
, tags
|
, tags
|
||||||
from contact
|
from contact
|
||||||
where contact.company_id = $1 and (($2 = '') or (tags @> array[$2]::tag_name[]))
|
where (%s)
|
||||||
order by business_name
|
order by business_name
|
||||||
`, company.Id, tag)
|
`, strings.Join(where, ") AND (")), args...)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
var entries []*ContactEntry
|
var entries []*ContactEntry
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
entry := &ContactEntry{}
|
entry := &ContactEntry{}
|
||||||
err = rows.Scan(&entry.Slug, &entry.Name, &entry.Email, &entry.Phone, &entry.Tags)
|
if err := rows.Scan(&entry.Slug, &entry.Name, &entry.Email, &entry.Phone, &entry.Tags); err != nil {
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
entries = append(entries, entry)
|
entries = append(entries, entry)
|
||||||
|
|
|
@ -18,6 +18,20 @@
|
||||||
|
|
||||||
{{ define "content" }}
|
{{ define "content" }}
|
||||||
{{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.ContactsIndexPage*/ -}}
|
{{- /*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>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
Loading…
Reference in New Issue