Compare commits
2 Commits
233e7723c3
...
4b4d7ad87d
Author | SHA1 | Date |
---|---|---|
jordi fita mas | 4b4d7ad87d | |
jordi fita mas | 2df6947577 |
|
@ -184,7 +184,7 @@ func HandleCompanyTaxDetailsForm(w http.ResponseWriter, r *http.Request, _ httpr
|
||||||
company := mustGetCompany(r)
|
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)
|
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) {
|
if IsHTMxRequest(r) {
|
||||||
w.Header().Set("HX-Trigger", "closeModal")
|
w.Header().Set(HxTrigger, "closeModal")
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(w, r, companyURI(company, "/tax-details"), http.StatusSeeOther)
|
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)
|
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)
|
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) {
|
if IsHTMxRequest(r) {
|
||||||
w.Header().Set("HX-Trigger", "closeModal")
|
w.Header().Set(HxTrigger, "closeModal")
|
||||||
w.Header().Set("HX-Refresh", "true")
|
w.Header().Set(HxRefresh, "true")
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(w, r, companyURI(company, "/contacts"), http.StatusSeeOther)
|
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)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
if IsHTMxRequest(r) {
|
if IsHTMxRequest(r) {
|
||||||
w.Header().Set("HX-Trigger", "closeModal")
|
w.Header().Set(HxTrigger, "closeModal")
|
||||||
w.Header().Set("HX-Refresh", "true")
|
w.Header().Set(HxRefresh, "true")
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(w, r, companyURI(mustGetCompany(r), "/contacts/"+slug), http.StatusSeeOther)
|
http.Redirect(w, r, companyURI(mustGetCompany(r), "/contacts/"+slug), http.StatusSeeOther)
|
||||||
|
|
|
@ -5,13 +5,20 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
HxLocation = "HX-Location"
|
||||||
|
HxRefresh = "HX-Refresh"
|
||||||
|
HxRequest = "HX-Request"
|
||||||
|
HxTrigger = "HX-Trigger"
|
||||||
|
)
|
||||||
|
|
||||||
type HTMxLocation struct {
|
type HTMxLocation struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Target string `json:"target"`
|
Target string `json:"target"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsHTMxRequest(r *http.Request) bool {
|
func IsHTMxRequest(r *http.Request) bool {
|
||||||
return r.Header.Get("HX-Request") == "true"
|
return r.Header.Get(HxRequest) == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
func MustMarshalHTMxLocation(location *HTMxLocation) string {
|
func MustMarshalHTMxLocation(location *HTMxLocation) string {
|
||||||
|
|
|
@ -457,22 +457,7 @@ func HandleAddInvoice(w http.ResponseWriter, r *http.Request, _ httprouter.Param
|
||||||
return
|
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))
|
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)
|
closeModalAndRedirect(w, r, form.Location.Val, "/invoices/"+slug, "/invoices")
|
||||||
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) {
|
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 == "" {
|
if slug == "" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
indexUrl := companyURI(mustGetCompany(r), "/invoices")
|
htmxRedirect(w, r, 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 {
|
} else {
|
||||||
slug := params[0].Value
|
slug := params[0].Value
|
||||||
if !form.Validate() {
|
if !form.Validate() {
|
||||||
|
@ -869,22 +845,31 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
viewUrl := companyURI(company, "/invoices/"+slug)
|
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) {
|
if IsHTMxRequest(r) {
|
||||||
w.Header().Set("HX-Trigger", "closeModal")
|
w.Header().Set(HxTrigger, "closeModal")
|
||||||
location := &HTMxLocation{
|
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",
|
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)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(w, r, viewUrl, http.StatusSeeOther)
|
http.Redirect(w, r, uri, http.StatusSeeOther)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -92,8 +92,8 @@ func HandleAddProduct(w http.ResponseWriter, r *http.Request, _ httprouter.Param
|
||||||
taxes := mustSliceAtoi(form.Tax.Selected)
|
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)
|
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) {
|
if IsHTMxRequest(r) {
|
||||||
w.Header().Set("HX-Trigger", "closeModal")
|
w.Header().Set(HxTrigger, "closeModal")
|
||||||
w.Header().Set("HX-Refresh", "true")
|
w.Header().Set(HxRefresh, "true")
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(w, r, companyURI(company, "/products"), http.StatusSeeOther)
|
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)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
if IsHTMxRequest(r) {
|
if IsHTMxRequest(r) {
|
||||||
w.Header().Set("HX-Trigger", "closeModal")
|
w.Header().Set(HxTrigger, "closeModal")
|
||||||
w.Header().Set("HX-Refresh", "true")
|
w.Header().Set(HxRefresh, "true")
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(w, r, companyURI(company, "/products/"+slug), http.StatusSeeOther)
|
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)
|
conn.MustExec(r.Context(), "select change_password($1)", form.Password)
|
||||||
}
|
}
|
||||||
if IsHTMxRequest(r) {
|
if IsHTMxRequest(r) {
|
||||||
w.Header().Set("HX-Trigger", "closeModal")
|
w.Header().Set(HxTrigger, "closeModal")
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
} else {
|
} else {
|
||||||
company := mustGetCompany(r)
|
company := mustGetCompany(r)
|
||||||
|
|
Loading…
Reference in New Issue