From b07fe6cfa2d48be563ad60a8fab894a4f91ddd91 Mon Sep 17 00:00:00 2001
From: jordi fita mas
Date: Wed, 22 Mar 2023 14:59:54 +0100
Subject: [PATCH] Show the add contact form in a modal dialog
---
pkg/contacts.go | 13 +++++++++++--
web/template/contacts/index.gohtml | 1 +
web/template/contacts/new.gohtml | 4 ++--
3 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/pkg/contacts.go b/pkg/contacts.go
index 50c8596..7dc3310 100644
--- a/pkg/contacts.go
+++ b/pkg/contacts.go
@@ -46,7 +46,7 @@ func GetContactForm(w http.ResponseWriter, r *http.Request, params httprouter.Pa
}
func mustRenderNewContactForm(w http.ResponseWriter, r *http.Request, form *contactForm) {
- mustRenderAppTemplate(w, r, "contacts/new.gohtml", form)
+ mustRenderModalTemplate(w, r, "contacts/new.gohtml", form)
}
func mustRenderEditContactForm(w http.ResponseWriter, r *http.Request, form *contactForm) {
@@ -66,12 +66,21 @@ func HandleAddContact(w http.ResponseWriter, r *http.Request, _ httprouter.Param
return
}
if !form.Validate(r.Context(), conn) {
+ if !IsHTMxRequest(r) {
+ w.WriteHeader(http.StatusUnprocessableEntity)
+ }
mustRenderNewContactForm(w, r, form)
return
}
company := mustGetCompany(r)
conn.MustExec(r.Context(), "insert into contact (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code) values ($1, $2, ($12 || $3)::vatin, $4, parse_packed_phone_number($5, $12), $6, $7, $8, $9, $10, $11, $12)", company.Id, form.BusinessName, form.VATIN, form.TradeName, form.Phone, form.Email, form.Web, form.Address, form.City, form.Province, form.PostalCode, form.Country)
- http.Redirect(w, r, companyURI(company, "/contacts"), 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, "/contacts"), http.StatusSeeOther)
+ }
}
func HandleUpdateContact(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
diff --git a/web/template/contacts/index.gohtml b/web/template/contacts/index.gohtml
index 4f57752..d53c72e 100644
--- a/web/template/contacts/index.gohtml
+++ b/web/template/contacts/index.gohtml
@@ -11,6 +11,7 @@
{{( pgettext "New contact" "action" )}}
diff --git a/web/template/contacts/new.gohtml b/web/template/contacts/new.gohtml
index f122173..4b974a6 100644
--- a/web/template/contacts/new.gohtml
+++ b/web/template/contacts/new.gohtml
@@ -15,9 +15,9 @@
{{ define "content" }}
{{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.contactForm*/ -}}
-
+
{{(pgettext "New Contact" "title")}}
-