Boost the main navigation links with HTMx

I am not sure if, at the end, all pages that now use
mustRenderAppTemplate will be replaced with mustRenderMainTemplate,
but for now i keep them separate to know which routes are already
“boosted”.
This commit is contained in:
jordi fita mas 2023-03-23 10:55:02 +01:00
parent 6e081a1846
commit 41ce5af2ed
7 changed files with 18 additions and 8 deletions

View File

@ -24,7 +24,7 @@ func IndexContacts(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
page := &ContactsIndexPage{ page := &ContactsIndexPage{
Contacts: mustGetContactEntries(r.Context(), conn, company), Contacts: mustGetContactEntries(r.Context(), conn, company),
} }
mustRenderAppTemplate(w, r, "contacts/index.gohtml", page) mustRenderMainTemplate(w, r, "contacts/index.gohtml", page)
} }
func GetContactForm(w http.ResponseWriter, r *http.Request, params httprouter.Params) { func GetContactForm(w http.ResponseWriter, r *http.Request, params httprouter.Params) {

View File

@ -46,7 +46,7 @@ func IndexInvoices(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
Invoices: mustCollectInvoiceEntries(r.Context(), conn, mustGetCompany(r), locale, tag), Invoices: mustCollectInvoiceEntries(r.Context(), conn, mustGetCompany(r), locale, tag),
InvoiceStatuses: mustCollectInvoiceStatuses(r.Context(), conn, locale), InvoiceStatuses: mustCollectInvoiceStatuses(r.Context(), conn, locale),
} }
mustRenderAppTemplate(w, r, "invoices/index.gohtml", page) mustRenderMainTemplate(w, r, "invoices/index.gohtml", page)
} }
func mustCollectInvoiceEntries(ctx context.Context, conn *Conn, company *Company, locale *Locale, tag string) []*InvoiceEntry { func mustCollectInvoiceEntries(ctx context.Context, conn *Conn, company *Company, locale *Locale, tag string) []*InvoiceEntry {

View File

@ -26,7 +26,7 @@ func IndexProducts(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
page := &productsIndexPage{ page := &productsIndexPage{
Products: mustGetProductEntries(r.Context(), conn, company), Products: mustGetProductEntries(r.Context(), conn, company),
} }
mustRenderAppTemplate(w, r, "products/index.gohtml", page) mustRenderMainTemplate(w, r, "products/index.gohtml", page)
} }
func GetProductForm(w http.ResponseWriter, r *http.Request, params httprouter.Params) { func GetProductForm(w http.ResponseWriter, r *http.Request, params httprouter.Params) {

View File

@ -32,7 +32,7 @@ func NewRouter(db *Db) http.Handler {
companyRouter.GET("/invoices/:slug/edit", ServeEditInvoice) companyRouter.GET("/invoices/:slug/edit", ServeEditInvoice)
companyRouter.POST("/invoices/:slug/edit", HandleEditInvoiceAction) companyRouter.POST("/invoices/:slug/edit", HandleEditInvoiceAction)
companyRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { companyRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
mustRenderAppTemplate(w, r, "dashboard.gohtml", nil) mustRenderMainTemplate(w, r, "dashboard.gohtml", nil)
}) })
router := httprouter.New() router := httprouter.New()

View File

@ -100,11 +100,19 @@ func mustRenderAppTemplate(w io.Writer, r *http.Request, filename string, data i
} }
func mustRenderModalTemplate(w io.Writer, r *http.Request, filename string, data interface{}) { func mustRenderModalTemplate(w io.Writer, r *http.Request, filename string, data interface{}) {
layout := "app.gohtml"
if IsHTMxRequest(r) { if IsHTMxRequest(r) {
layout = "modal.gohtml" mustRenderTemplate(w, r, "modal.gohtml", filename, data)
} else {
mustRenderAppTemplate(w, r, filename, data)
}
}
func mustRenderMainTemplate(w io.Writer, r *http.Request, filename string, data interface{}) {
if IsHTMxRequest(r) {
mustRenderTemplate(w, r, "main.gohtml", filename, data)
} else {
mustRenderAppTemplate(w, r, filename, data)
} }
mustRenderTemplate(w, r, layout, filename, data)
} }
func mustRenderWebTemplate(w io.Writer, r *http.Request, filename string, data interface{}) { func mustRenderWebTemplate(w io.Writer, r *http.Request, filename string, data interface{}) {

View File

@ -40,7 +40,7 @@
</ul> </ul>
</details> </details>
</header> </header>
<nav aria-label="{{( pgettext "Main" "title" )}}"> <nav aria-label="{{( pgettext "Main" "title" )}}" data-hx-target="main" data-hx-boost="true">
<ul> <ul>
<li><a href="{{ companyURI "/" }}">{{( pgettext "Dashboard" "nav" )}}</a></li> <li><a href="{{ companyURI "/" }}">{{( pgettext "Dashboard" "nav" )}}</a></li>
<li><a href="{{ companyURI "/invoices" }}">{{( pgettext "Invoices" "nav" )}}</a></li> <li><a href="{{ companyURI "/invoices" }}">{{( pgettext "Invoices" "nav" )}}</a></li>

2
web/template/main.gohtml Normal file
View File

@ -0,0 +1,2 @@
{{- template "breadcrumbs" . }}
{{- template "content" . }}