Compare commits

..

2 Commits

Author SHA1 Message Date
jordi fita mas 4b4d7ad87d Refactor duplicated logic in invoice for HTMx trigger and location 2023-04-06 12:20:40 +02:00
jordi fita mas 2df6947577 Create constants for the HTMX request and response headers used
It works better with an IDE, and less chance of mistyping something
without notice.
2023-04-06 12:07:20 +02:00
6 changed files with 44 additions and 52 deletions

View File

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

View File

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

View File

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

View File

@ -457,22 +457,7 @@ func HandleAddInvoice(w http.ResponseWriter, r *http.Request, _ httprouter.Param
return
}
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")
location := &HTMxLocation{
Target: "main",
}
if form.Location.Val == "view" {
location.Path = viewUrl
} else {
location.Path = companyURI(company, "/invoices")
}
w.Header().Set("HX-Location", MustMarshalHTMxLocation(location))
w.WriteHeader(http.StatusNoContent)
} else {
http.Redirect(w, r, viewUrl, http.StatusSeeOther)
}
closeModalAndRedirect(w, r, form.Location.Val, "/invoices/"+slug, "/invoices")
}
func HandleNewInvoiceAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
@ -847,16 +832,7 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout
if slug == "" {
http.NotFound(w, r)
}
indexUrl := companyURI(mustGetCompany(r), "/invoices")
if IsHTMxRequest(r) {
w.Header().Set("HX-Location", MustMarshalHTMxLocation(&HTMxLocation{
Path: indexUrl,
Target: "main",
}))
w.WriteHeader(http.StatusNoContent)
} else {
http.Redirect(w, r, indexUrl, http.StatusSeeOther)
}
htmxRedirect(w, r, companyURI(mustGetCompany(r), "/invoices"))
} else {
slug := params[0].Value
if !form.Validate() {
@ -869,23 +845,32 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout
http.NotFound(w, r)
return
}
viewUrl := companyURI(company, "/invoices/"+slug)
if IsHTMxRequest(r) {
w.Header().Set("HX-Trigger", "closeModal")
location := &HTMxLocation{
Target: "main",
}
if form.Location.Val == "view" {
location.Path = viewUrl
} else {
location.Path = companyURI(company, "/invoices")
}
w.Header().Set("HX-Location", MustMarshalHTMxLocation(location))
w.WriteHeader(http.StatusNoContent)
} else {
http.Redirect(w, r, viewUrl, http.StatusSeeOther)
closeModalAndRedirect(w, r, form.Location.Val, "/invoices/"+slug, "/invoices")
}
}
func closeModalAndRedirect(w http.ResponseWriter, r *http.Request, selector string, viewUri string, indexUri string) {
company := mustGetCompany(r)
nextUri := companyURI(company, indexUri)
if IsHTMxRequest(r) {
w.Header().Set(HxTrigger, "closeModal")
if selector == "view" {
nextUri = companyURI(company, viewUri)
}
}
htmxRedirect(w, r, nextUri)
}
func htmxRedirect(w http.ResponseWriter, r *http.Request, uri string) {
if IsHTMxRequest(r) {
w.Header().Set(HxLocation, MustMarshalHTMxLocation(&HTMxLocation{
Path: uri,
Target: "main",
}))
w.WriteHeader(http.StatusNoContent)
} else {
http.Redirect(w, r, uri, http.StatusSeeOther)
}
}
func ServeEditInvoice(w http.ResponseWriter, r *http.Request, params httprouter.Params) {

View File

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

View File

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