Create constants for the HTMX request and response headers used
It works better with an IDE, and less chance of mistyping something without notice.
This commit is contained in:
parent
233e7723c3
commit
2df6947577
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue