Add contact’s inline form for tags

This commit is contained in:
jordi fita mas 2023-05-12 11:32:39 +02:00
parent df37583cc6
commit f639602170
3 changed files with 41 additions and 2 deletions

View File

@ -407,3 +407,36 @@ func (form *contactForm) MustFillFromDatabase(ctx context.Context, conn *Conn, s
form.Country, form.Country,
form.Tags)) form.Tags))
} }
func ServeEditContactTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
conn := getConn(r)
locale := getLocale(r)
company := getCompany(r)
slug := params[0].Value
form := newTagsForm(companyURI(company, "/contacts/"+slug+"/tags"), slug, locale)
if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `select array_to_string(tags, ',') from contact where slug = $1`, form.Slug).Scan(&form.Tags)) {
http.NotFound(w, r)
return
}
mustRenderStandaloneTemplate(w, r, "tags/edit.gohtml", form)
}
func HandleUpdateContactTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
locale := getLocale(r)
conn := getConn(r)
company := getCompany(r)
slug := params[0].Value
form := newTagsForm(companyURI(company, "/contacts/"+slug+"/tags/edit"), slug, locale)
if err := form.Parse(r); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := verifyCsrfTokenValid(r); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
if conn.MustGetText(r.Context(), "", "update contact set tags = $1 where slug = $2 returning slug", form.Tags, form.Slug) == "" {
http.NotFound(w, r)
}
mustRenderStandaloneTemplate(w, r, "tags/view.gohtml", form)
}

View File

@ -20,6 +20,8 @@ func NewRouter(db *Db) http.Handler {
companyRouter.POST("/contacts", HandleAddContact) companyRouter.POST("/contacts", HandleAddContact)
companyRouter.GET("/contacts/:slug", GetContactForm) companyRouter.GET("/contacts/:slug", GetContactForm)
companyRouter.PUT("/contacts/:slug", HandleUpdateContact) companyRouter.PUT("/contacts/:slug", HandleUpdateContact)
companyRouter.PUT("/contacts/:slug/tags", HandleUpdateContactTags)
companyRouter.GET("/contacts/:slug/tags/edit", ServeEditContactTags)
companyRouter.GET("/products", IndexProducts) companyRouter.GET("/products", IndexProducts)
companyRouter.POST("/products", HandleAddProduct) companyRouter.POST("/products", HandleAddProduct)
companyRouter.GET("/products/:slug", GetProductForm) companyRouter.GET("/products/:slug", GetProductForm)

View File

@ -51,10 +51,14 @@
<td><a href="{{ companyURI "/contacts/"}}{{ .Slug }}" data-hx-target="main" data-hx-boost="true">{{ .Name }}</a></td> <td><a href="{{ companyURI "/contacts/"}}{{ .Slug }}" data-hx-target="main" data-hx-boost="true">{{ .Name }}</a></td>
<td><a href="mailto:{{ .Email }}">{{ .Email }}</a></td> <td><a href="mailto:{{ .Email }}">{{ .Email }}</a></td>
<td><a href="tel:{{ .Phone }}">{{ .Phone }}</a></td> <td><a href="tel:{{ .Phone }}">{{ .Phone }}</a></td>
<td> <td
data-hx-get="{{companyURI "/contacts/"}}{{ .Slug }}/tags/edit"
data-hx-target="this"
data-hx-swap="outerHTML"
>
{{- range $index, $tag := .Tags }} {{- range $index, $tag := .Tags }}
{{- if gt $index 0 }}, {{ end -}} {{- if gt $index 0 }}, {{ end -}}
<a href="?tag={{ . }}" data-hx-target="main" data-hx-boost="true">{{ . }}</a> {{ . }}
{{- end }} {{- end }}
</td> </td>
<td class="actions"> <td class="actions">