Show the add contact form in a modal dialog

This commit is contained in:
jordi fita mas 2023-03-22 14:59:54 +01:00
parent b1e3afc48b
commit b07fe6cfa2
3 changed files with 14 additions and 4 deletions

View File

@ -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) { 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) { func mustRenderEditContactForm(w http.ResponseWriter, r *http.Request, form *contactForm) {
@ -66,13 +66,22 @@ func HandleAddContact(w http.ResponseWriter, r *http.Request, _ httprouter.Param
return return
} }
if !form.Validate(r.Context(), conn) { if !form.Validate(r.Context(), conn) {
if !IsHTMxRequest(r) {
w.WriteHeader(http.StatusUnprocessableEntity)
}
mustRenderNewContactForm(w, r, form) mustRenderNewContactForm(w, r, form)
return return
} }
company := mustGetCompany(r) 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) 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)
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) http.Redirect(w, r, companyURI(company, "/contacts"), http.StatusSeeOther)
} }
}
func HandleUpdateContact(w http.ResponseWriter, r *http.Request, params httprouter.Params) { func HandleUpdateContact(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
conn := getConn(r) conn := getConn(r)

View File

@ -11,6 +11,7 @@
</p> </p>
<p> <p>
<a class="primary button" <a class="primary button"
data-hx-push-url="false" data-hx-swap="beforeend" data-hx-boost="true"
href="{{ companyURI "/contacts/new" }}">{{( pgettext "New contact" "action" )}}</a> href="{{ companyURI "/contacts/new" }}">{{( pgettext "New contact" "action" )}}</a>
</p> </p>
</nav> </nav>

View File

@ -15,9 +15,9 @@
{{ define "content" }} {{ define "content" }}
{{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.contactForm*/ -}} {{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.contactForm*/ -}}
<section class="dialog-content"> <section class="dialog-content" id="new-contact-dialog-content" data-hx-target="this">
<h2>{{(pgettext "New Contact" "title")}}</h2> <h2>{{(pgettext "New Contact" "title")}}</h2>
<form method="POST" action="{{ companyURI "/contacts" }}"> <form method="POST" action="{{ companyURI "/contacts" }}" data-hx-boost="true" data-hx-select="#new-contact-dialog-content">
{{ csrfToken }} {{ csrfToken }}
{{ template "input-field" .BusinessName | addInputAttr "autofocus" }} {{ template "input-field" .BusinessName | addInputAttr "autofocus" }}
{{ template "input-field" .VATIN }} {{ template "input-field" .VATIN }}