Compare commits
No commits in common. "4b4d7ad87d11db0ab918d75180dcab4219adc08f" and "233e7723c39d3ca3f31302d943cab3244b9d2504" have entirely different histories.
4b4d7ad87d
...
233e7723c3
|
@ -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(HxTrigger, "closeModal")
|
||||
w.Header().Set("HX-Trigger", "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(HxTrigger, "closeModal")
|
||||
w.Header().Set(HxRefresh, "true")
|
||||
w.Header().Set("HX-Trigger", "closeModal")
|
||||
w.Header().Set("HX-Refresh", "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(HxTrigger, "closeModal")
|
||||
w.Header().Set(HxRefresh, "true")
|
||||
w.Header().Set("HX-Trigger", "closeModal")
|
||||
w.Header().Set("HX-Refresh", "true")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
} else {
|
||||
http.Redirect(w, r, companyURI(mustGetCompany(r), "/contacts/"+slug), http.StatusSeeOther)
|
||||
|
|
|
@ -5,20 +5,13 @@ 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(HxRequest) == "true"
|
||||
return r.Header.Get("HX-Request") == "true"
|
||||
}
|
||||
|
||||
func MustMarshalHTMxLocation(location *HTMxLocation) string {
|
||||
|
|
|
@ -457,7 +457,22 @@ 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))
|
||||
closeModalAndRedirect(w, r, form.Location.Val, "/invoices/"+slug, "/invoices")
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func HandleNewInvoiceAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||
|
@ -832,7 +847,16 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout
|
|||
if slug == "" {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
htmxRedirect(w, r, companyURI(mustGetCompany(r), "/invoices"))
|
||||
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)
|
||||
}
|
||||
} else {
|
||||
slug := params[0].Value
|
||||
if !form.Validate() {
|
||||
|
@ -845,31 +869,22 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout
|
|||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
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)
|
||||
viewUrl := companyURI(company, "/invoices/"+slug)
|
||||
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,
|
||||
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, uri, http.StatusSeeOther)
|
||||
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(HxTrigger, "closeModal")
|
||||
w.Header().Set(HxRefresh, "true")
|
||||
w.Header().Set("HX-Trigger", "closeModal")
|
||||
w.Header().Set("HX-Refresh", "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(HxTrigger, "closeModal")
|
||||
w.Header().Set(HxRefresh, "true")
|
||||
w.Header().Set("HX-Trigger", "closeModal")
|
||||
w.Header().Set("HX-Refresh", "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(HxTrigger, "closeModal")
|
||||
w.Header().Set("HX-Trigger", "closeModal")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
} else {
|
||||
company := mustGetCompany(r)
|
||||
|
|
Loading…
Reference in New Issue