diff --git a/pkg/invoices.go b/pkg/invoices.go index e237b2c..03755d1 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -393,7 +393,7 @@ func mustRenderNewInvoiceForm(w http.ResponseWriter, r *http.Request, form *invo locale := getLocale(r) form.Customer.EmptyLabel = gettext("Select a customer to bill.", locale) page := newNewInvoicePage(form, r) - mustRenderAppTemplate(w, r, "invoices/new.gohtml", page) + mustRenderModalTemplate(w, r, "invoices/new.gohtml", page) } func mustRenderNewInvoiceProductsForm(w http.ResponseWriter, r *http.Request, action string, form *invoiceForm) { @@ -404,7 +404,7 @@ func mustRenderNewInvoiceProductsForm(w http.ResponseWriter, r *http.Request, ac Form: form, Products: mustGetProductChoices(r.Context(), conn, company), } - mustRenderAppTemplate(w, r, "invoices/products.gohtml", page) + mustRenderModalTemplate(w, r, "invoices/products.gohtml", page) } func mustGetProductChoices(ctx context.Context, conn *Conn, company *Company) []*productChoice { @@ -457,7 +457,13 @@ 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, $8)", company.Id, form.Number, form.Date, form.Customer, form.Notes, form.PaymentMethod, form.Tags, NewInvoiceProductArray(form.Products)) - http.Redirect(w, r, companyURI(company, "/invoices/"+slug), http.StatusSeeOther) + if IsHTMxRequest(r) { + w.Header().Set("HX-Trigger", "closeModal") + w.Header().Set("HX-Refresh", "true") + w.WriteHeader(http.StatusNoContent) + } else { + http.Redirect(w, r, companyURI(company, "/invoices/"+slug), http.StatusSeeOther) + } } func HandleNewInvoiceAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) { @@ -832,7 +838,12 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout if slug == "" { http.NotFound(w, r) } - http.Redirect(w, r, companyURI(mustGetCompany(r), "/invoices"), http.StatusSeeOther) + if IsHTMxRequest(r) { + w.Header().Set("HX-Refresh", "true") + w.WriteHeader(http.StatusNoContent) + } else { + http.Redirect(w, r, companyURI(mustGetCompany(r), "/invoices"), http.StatusSeeOther) + } } else { slug := params[0].Value if !form.Validate() { @@ -845,7 +856,13 @@ func HandleUpdateInvoice(w http.ResponseWriter, r *http.Request, params httprout http.NotFound(w, r) return } - http.Redirect(w, r, companyURI(company, "/invoices/"+slug), http.StatusSeeOther) + if IsHTMxRequest(r) { + w.Header().Set("HX-Trigger", "closeModal") + w.Header().Set("HX-Refresh", "true") + w.WriteHeader(http.StatusNoContent) + } else { + http.Redirect(w, r, companyURI(company, "/invoices/"+slug), http.StatusSeeOther) + } } } @@ -879,7 +896,7 @@ func newEditInvoicePage(slug string, form *invoiceForm, r *http.Request) *editIn func mustRenderEditInvoiceForm(w http.ResponseWriter, r *http.Request, slug string, form *invoiceForm) { page := newEditInvoicePage(slug, form, r) - mustRenderAppTemplate(w, r, "invoices/edit.gohtml", page) + mustRenderModalTemplate(w, r, "invoices/edit.gohtml", page) } func HandleEditInvoiceAction(w http.ResponseWriter, r *http.Request, params httprouter.Params) { diff --git a/web/static/numerus.js b/web/static/numerus.js index ec8edf3..a607763 100644 --- a/web/static/numerus.js +++ b/web/static/numerus.js @@ -421,6 +421,19 @@ htmx.onLoad((target) => { } }) +htmx.on('htmx:configRequest', function(e) { + const element = e.detail.elt; + if (element && element.nodeName === 'FORM') { + let submitter = e.detail.triggeringEvent.submitter; + if (submitter) { + const action = submitter.attributes['formaction']; + if (action && action.value) { + e.detail.path = action.value; + } + } + } +}) + htmx.on('closeModal', () => { const openDialog = document.querySelector('dialog[open]'); if (!openDialog) { diff --git a/web/template/invoices/edit.gohtml b/web/template/invoices/edit.gohtml index b6ac214..b30627a 100644 --- a/web/template/invoices/edit.gohtml +++ b/web/template/invoices/edit.gohtml @@ -15,9 +15,9 @@ {{ define "content" }} {{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.editInvoicePage*/ -}} -
+

{{ printf (pgettext "Edit Invoice ā€œ%sā€" "title") .Number }}

-
+ {{ csrfToken }} {{ with .Form -}} diff --git a/web/template/invoices/index.gohtml b/web/template/invoices/index.gohtml index 5dfafc4..ef29b24 100644 --- a/web/template/invoices/index.gohtml +++ b/web/template/invoices/index.gohtml @@ -17,6 +17,7 @@ name="action" value="download" >{{( pgettext "Download invoices" "action" )}} {{( pgettext "New invoice" "action" )}}

@@ -68,7 +69,7 @@