Use HX-Location instead of HX-Refresh when editing invoices

This makes reload only the <main> portion of the page, instead of the
whole thing, which to me looks faster; haven’t really measured it.

Like with duplicate, i had to add the location query argument when
inside the view page in order to return back to the same page, not the
index.
This commit is contained in:
jordi fita mas 2023-04-05 10:29:03 +02:00
parent dbfa58699c
commit 233e7723c3
2 changed files with 19 additions and 5 deletions

View File

@ -847,11 +847,15 @@ 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")
if IsHTMxRequest(r) { if IsHTMxRequest(r) {
w.Header().Set("HX-Refresh", "true") w.Header().Set("HX-Location", MustMarshalHTMxLocation(&HTMxLocation{
Path: indexUrl,
Target: "main",
}))
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} else { } else {
http.Redirect(w, r, companyURI(mustGetCompany(r), "/invoices"), http.StatusSeeOther) http.Redirect(w, r, indexUrl, http.StatusSeeOther)
} }
} else { } else {
slug := params[0].Value slug := params[0].Value
@ -865,12 +869,21 @@ 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)
if IsHTMxRequest(r) { if IsHTMxRequest(r) {
w.Header().Set("HX-Trigger", "closeModal") w.Header().Set("HX-Trigger", "closeModal")
w.Header().Set("HX-Refresh", "true") 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) w.WriteHeader(http.StatusNoContent)
} else { } else {
http.Redirect(w, r, companyURI(company, "/invoices/"+slug), http.StatusSeeOther) http.Redirect(w, r, viewUrl, http.StatusSeeOther)
} }
} }
} }
@ -885,6 +898,7 @@ func ServeEditInvoice(w http.ResponseWriter, r *http.Request, params httprouter.
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
form.Location.Val = r.URL.Query().Get("location")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
mustRenderEditInvoiceForm(w, r, slug, form) mustRenderEditInvoiceForm(w, r, slug, form)
} }

View File

@ -16,7 +16,7 @@
href="{{ companyURI "/invoices/new"}}?duplicate={{ .Slug }}&amp;location=view">{{( pgettext "Duplicate" "action" )}}</a> href="{{ companyURI "/invoices/new"}}?duplicate={{ .Slug }}&amp;location=view">{{( pgettext "Duplicate" "action" )}}</a>
<a class="button primary" <a class="button primary"
data-hx-push-url="false" data-hx-swap="beforeend" data-hx-boost="true" data-hx-push-url="false" data-hx-swap="beforeend" data-hx-boost="true"
href="{{ companyURI "/invoices/"}}{{ .Slug }}/edit">{{( pgettext "Edit" "action" )}}</a> href="{{ companyURI "/invoices/"}}{{ .Slug }}/edit?location=view">{{( pgettext "Edit" "action" )}}</a>
<a class="primary button" <a class="primary button"
href="{{ companyURI "/invoices/" }}{{ .Slug }}.pdf" href="{{ companyURI "/invoices/" }}{{ .Slug }}.pdf"
download="{{ .Number}}.pdf">{{( pgettext "Download invoice" "action" )}}</a> download="{{ .Number}}.pdf">{{( pgettext "Download invoice" "action" )}}</a>