From 2df6947577a67b5a90f893708bd1f34f8ae4d519 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Thu, 6 Apr 2023 12:07:20 +0200 Subject: [PATCH] Create constants for the HTMX request and response headers used It works better with an IDE, and less chance of mistyping something without notice. --- pkg/company.go | 2 +- pkg/contacts.go | 8 ++++---- pkg/htmx.go | 9 ++++++++- pkg/invoices.go | 10 +++++----- pkg/products.go | 8 ++++---- pkg/profile.go | 2 +- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/pkg/company.go b/pkg/company.go index d459109..8a5a823 100644 --- a/pkg/company.go +++ b/pkg/company.go @@ -184,7 +184,7 @@ func HandleCompanyTaxDetailsForm(w http.ResponseWriter, r *http.Request, _ httpr company := mustGetCompany(r) conn.MustExec(r.Context(), "update company set business_name = $1, vatin = ($11 || $2)::vatin, trade_name = $3, phone = parse_packed_phone_number($4, $11), email = $5, web = $6, address = $7, city = $8, province = $9, postal_code = $10, country_code = $11, currency_code = $12, invoice_number_format = $13, legal_disclaimer = $14 where company_id = $15", form.BusinessName, form.VATIN, form.TradeName, form.Phone, form.Email, form.Web, form.Address, form.City, form.Province, form.PostalCode, form.Country, form.Currency, form.InvoiceNumberFormat, form.LegalDisclaimer, company.Id) if IsHTMxRequest(r) { - w.Header().Set("HX-Trigger", "closeModal") + w.Header().Set(HxTrigger, "closeModal") w.WriteHeader(http.StatusNoContent) } else { http.Redirect(w, r, companyURI(company, "/tax-details"), http.StatusSeeOther) diff --git a/pkg/contacts.go b/pkg/contacts.go index 0028446..2e8b128 100644 --- a/pkg/contacts.go +++ b/pkg/contacts.go @@ -88,8 +88,8 @@ func HandleAddContact(w http.ResponseWriter, r *http.Request, _ httprouter.Param company := mustGetCompany(r) conn.MustExec(r.Context(), "select add_contact($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)", company.Id, form.BusinessName, form.VATIN, form.TradeName, form.Phone, form.Email, form.Web, form.Address, form.City, form.Province, form.PostalCode, form.Country, form.Tags) if IsHTMxRequest(r) { - w.Header().Set("HX-Trigger", "closeModal") - w.Header().Set("HX-Refresh", "true") + w.Header().Set(HxTrigger, "closeModal") + w.Header().Set(HxRefresh, "true") w.WriteHeader(http.StatusNoContent) } else { http.Redirect(w, r, companyURI(company, "/contacts"), http.StatusSeeOther) @@ -117,8 +117,8 @@ func HandleUpdateContact(w http.ResponseWriter, r *http.Request, params httprout http.NotFound(w, r) } if IsHTMxRequest(r) { - w.Header().Set("HX-Trigger", "closeModal") - w.Header().Set("HX-Refresh", "true") + w.Header().Set(HxTrigger, "closeModal") + w.Header().Set(HxRefresh, "true") w.WriteHeader(http.StatusNoContent) } else { http.Redirect(w, r, companyURI(mustGetCompany(r), "/contacts/"+slug), http.StatusSeeOther) diff --git a/pkg/htmx.go b/pkg/htmx.go index 52ab070..d11b831 100644 --- a/pkg/htmx.go +++ b/pkg/htmx.go @@ -5,13 +5,20 @@ import ( "net/http" ) +const ( + HxLocation = "HX-Location" + HxRefresh = "HX-Refresh" + HxRequest = "HX-Request" + HxTrigger = "HX-Trigger" +) + type HTMxLocation struct { Path string `json:"path"` Target string `json:"target"` } func IsHTMxRequest(r *http.Request) bool { - return r.Header.Get("HX-Request") == "true" + return r.Header.Get(HxRequest) == "true" } func MustMarshalHTMxLocation(location *HTMxLocation) string { diff --git a/pkg/invoices.go b/pkg/invoices.go index e9b4e7e..b73d4b3 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -459,7 +459,7 @@ func HandleAddInvoice(w http.ResponseWriter, r *http.Request, _ httprouter.Param slug := conn.MustGetText(r.Context(), "", "select add_invoice($1, $2, $3, $4, $5, $6, $7)", company.Id, form.Date, form.Customer, form.Notes, form.PaymentMethod, form.Tags, NewInvoiceProductArray(form.Products)) viewUrl := companyURI(company, "/invoices/"+slug) if IsHTMxRequest(r) { - w.Header().Set("HX-Trigger", "closeModal") + w.Header().Set(HxTrigger, "closeModal") location := &HTMxLocation{ Target: "main", } @@ -468,7 +468,7 @@ func HandleAddInvoice(w http.ResponseWriter, r *http.Request, _ httprouter.Param } else { location.Path = companyURI(company, "/invoices") } - w.Header().Set("HX-Location", MustMarshalHTMxLocation(location)) + w.Header().Set(HxLocation, MustMarshalHTMxLocation(location)) w.WriteHeader(http.StatusNoContent) } else { http.Redirect(w, r, viewUrl, http.StatusSeeOther) @@ -849,7 +849,7 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout } indexUrl := companyURI(mustGetCompany(r), "/invoices") if IsHTMxRequest(r) { - w.Header().Set("HX-Location", MustMarshalHTMxLocation(&HTMxLocation{ + w.Header().Set(HxLocation, MustMarshalHTMxLocation(&HTMxLocation{ Path: indexUrl, Target: "main", })) @@ -871,7 +871,7 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout } viewUrl := companyURI(company, "/invoices/"+slug) if IsHTMxRequest(r) { - w.Header().Set("HX-Trigger", "closeModal") + w.Header().Set(HxTrigger, "closeModal") location := &HTMxLocation{ Target: "main", } @@ -880,7 +880,7 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout } else { location.Path = companyURI(company, "/invoices") } - w.Header().Set("HX-Location", MustMarshalHTMxLocation(location)) + w.Header().Set(HxLocation, MustMarshalHTMxLocation(location)) w.WriteHeader(http.StatusNoContent) } else { http.Redirect(w, r, viewUrl, http.StatusSeeOther) diff --git a/pkg/products.go b/pkg/products.go index 2a3ea20..c974bca 100644 --- a/pkg/products.go +++ b/pkg/products.go @@ -92,8 +92,8 @@ func HandleAddProduct(w http.ResponseWriter, r *http.Request, _ httprouter.Param taxes := mustSliceAtoi(form.Tax.Selected) conn.MustExec(r.Context(), "select add_product($1, $2, $3, $4, $5, $6)", company.Id, form.Name, form.Description, form.Price, taxes, form.Tags) if IsHTMxRequest(r) { - w.Header().Set("HX-Trigger", "closeModal") - w.Header().Set("HX-Refresh", "true") + w.Header().Set(HxTrigger, "closeModal") + w.Header().Set(HxRefresh, "true") w.WriteHeader(http.StatusNoContent) } else { http.Redirect(w, r, companyURI(company, "/products"), http.StatusSeeOther) @@ -144,8 +144,8 @@ func HandleUpdateProduct(w http.ResponseWriter, r *http.Request, params httprout http.NotFound(w, r) } if IsHTMxRequest(r) { - w.Header().Set("HX-Trigger", "closeModal") - w.Header().Set("HX-Refresh", "true") + w.Header().Set(HxTrigger, "closeModal") + w.Header().Set(HxRefresh, "true") w.WriteHeader(http.StatusNoContent) } else { http.Redirect(w, r, companyURI(company, "/products/"+slug), http.StatusSeeOther) diff --git a/pkg/profile.go b/pkg/profile.go index 5947ea4..592de7b 100644 --- a/pkg/profile.go +++ b/pkg/profile.go @@ -134,7 +134,7 @@ func HandleProfileForm(w http.ResponseWriter, r *http.Request, _ httprouter.Para conn.MustExec(r.Context(), "select change_password($1)", form.Password) } if IsHTMxRequest(r) { - w.Header().Set("HX-Trigger", "closeModal") + w.Header().Set(HxTrigger, "closeModal") w.WriteHeader(http.StatusNoContent) } else { company := mustGetCompany(r)