Add inline tag form for expenses

This commit is contained in:
jordi fita mas 2023-05-08 12:58:54 +02:00
parent 664088c748
commit f0f98e200c
6 changed files with 55 additions and 11 deletions

View File

@ -372,3 +372,36 @@ func (form *expenseFilterForm) Parse(r *http.Request) error {
form.TagsCondition.FillValue(r) form.TagsCondition.FillValue(r)
return nil return nil
} }
func ServeEditExpenseTags(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, "/expenses/"+slug+"/tags"), slug, locale)
if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `select array_to_string(tags, ',') from expense where slug = $1`, form.Slug).Scan(&form.Tags)) {
http.NotFound(w, r)
return
}
mustRenderStandaloneTemplate(w, r, "tags/edit.gohtml", form)
}
func HandleUpdateExpenseTags(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, "/expenses/"+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 expense 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

@ -1030,12 +1030,14 @@ func handleInvoiceAction(w http.ResponseWriter, r *http.Request, action string,
} }
type tagsForm struct { type tagsForm struct {
Action string
Slug string Slug string
Tags *TagsField Tags *TagsField
} }
func newTagsForm(slug string, locale *Locale) *tagsForm { func newTagsForm(uri string, slug string, locale *Locale) *tagsForm {
return &tagsForm{ return &tagsForm{
Action: uri,
Slug: slug, Slug: slug,
Tags: &TagsField{ Tags: &TagsField{
Name: "tags-" + slug, Name: "tags-" + slug,
@ -1055,7 +1057,9 @@ func (form *tagsForm) Parse(r *http.Request) error {
func ServeEditInvoiceTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) { func ServeEditInvoiceTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
conn := getConn(r) conn := getConn(r)
locale := getLocale(r) locale := getLocale(r)
form := newTagsForm(params[0].Value, locale) company := getCompany(r)
slug := params[0].Value
form := newTagsForm(companyURI(company, "/invoices/"+slug+"/tags"), slug, locale)
if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `select array_to_string(tags, ',') from invoice where slug = $1`, form.Slug).Scan(&form.Tags)) { if notFoundErrorOrPanic(conn.QueryRow(r.Context(), `select array_to_string(tags, ',') from invoice where slug = $1`, form.Slug).Scan(&form.Tags)) {
http.NotFound(w, r) http.NotFound(w, r)
return return
@ -1066,7 +1070,9 @@ func ServeEditInvoiceTags(w http.ResponseWriter, r *http.Request, params httprou
func HandleUpdateInvoiceTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) { func HandleUpdateInvoiceTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
locale := getLocale(r) locale := getLocale(r)
conn := getConn(r) conn := getConn(r)
form := newTagsForm(params[0].Value, locale) company := getCompany(r)
slug := params[0].Value
form := newTagsForm(companyURI(company, "/invoices/"+slug+"/tags/edit"), slug, locale)
if err := form.Parse(r); err != nil { if err := form.Parse(r); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
@ -1075,8 +1081,7 @@ func HandleUpdateInvoiceTags(w http.ResponseWriter, r *http.Request, params http
http.Error(w, err.Error(), http.StatusForbidden) http.Error(w, err.Error(), http.StatusForbidden)
return return
} }
slug := conn.MustGetText(r.Context(), "", "update invoice set tags = $1 where slug = $2 returning slug", form.Tags, form.Slug) if conn.MustGetText(r.Context(), "", "update invoice set tags = $1 where slug = $2 returning slug", form.Tags, form.Slug) == "" {
if slug == "" {
http.NotFound(w, r) http.NotFound(w, r)
} }
mustRenderStandaloneTemplate(w, r, "tags/view.gohtml", form) mustRenderStandaloneTemplate(w, r, "tags/view.gohtml", form)

View File

@ -38,6 +38,8 @@ func NewRouter(db *Db) http.Handler {
companyRouter.POST("/expenses", HandleAddExpense) companyRouter.POST("/expenses", HandleAddExpense)
companyRouter.GET("/expenses/:slug", ServeExpenseForm) companyRouter.GET("/expenses/:slug", ServeExpenseForm)
companyRouter.PUT("/expenses/:slug", HandleUpdateExpense) companyRouter.PUT("/expenses/:slug", HandleUpdateExpense)
companyRouter.PUT("/expenses/:slug/tags", HandleUpdateExpenseTags)
companyRouter.GET("/expenses/:slug/tags/edit", ServeEditExpenseTags)
companyRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { companyRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
mustRenderMainTemplate(w, r, "dashboard.gohtml", nil) mustRenderMainTemplate(w, r, "dashboard.gohtml", nil)
}) })

View File

@ -54,7 +54,11 @@
<td>{{ .InvoicerName }}</td> <td>{{ .InvoicerName }}</td>
<td>{{ .InvoiceDate|formatDate }}</td> <td>{{ .InvoiceDate|formatDate }}</td>
<td>{{ .InvoiceNumber }}</td> <td>{{ .InvoiceNumber }}</td>
<td> <td
data-hx-get="{{companyURI "/expenses/"}}{{ .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 -}}
{{ . }} {{ . }}

View File

@ -1,7 +1,7 @@
{{ define "content" }} {{ define "content" }}
{{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.tagsForm*/ -}} {{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.tagsForm*/ -}}
<td data-hx-target="this" data-hx-swap="outerHTML"> <td data-hx-target="this" data-hx-swap="outerHTML">
<form action="{{companyURI "/invoices/"}}{{ .Slug }}/tags" method="POST" <form action="{{ .Action }}" method="POST"
data-hx-push-url="false" data-hx-boost="true" data-hx-trigger="numerus-tags-out,submit"> data-hx-push-url="false" data-hx-boost="true" data-hx-trigger="numerus-tags-out,submit">
{{ csrfToken }} {{ csrfToken }}
{{ putMethod }} {{ putMethod }}

View File

@ -1,6 +1,6 @@
{{ define "content" }} {{ define "content" }}
{{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.tagsForm*/ -}} {{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.tagsForm*/ -}}
<td data-hx-get="{{companyURI "/invoices/"}}{{ .Slug }}/tags/edit" data-hx-target="this" data-hx-swap="outerHTML"> <td data-hx-get="{{ .Action }}" data-hx-target="this" data-hx-swap="outerHTML">
{{- range $index, $tag := .Tags.Tags }} {{- range $index, $tag := .Tags.Tags }}
{{- if gt $index 0 }}, {{ end -}} {{- if gt $index 0 }}, {{ end -}}
{{ . }} {{ . }}