From f0f98e200c48e0d6f4b218f516a2cfa9cff15dcf Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Mon, 8 May 2023 12:58:54 +0200 Subject: [PATCH] Add inline tag form for expenses --- pkg/expenses.go | 33 ++++++++++++++++++++++++++++++ pkg/invoices.go | 21 +++++++++++-------- pkg/router.go | 2 ++ web/template/expenses/index.gohtml | 6 +++++- web/template/tags/edit.gohtml | 2 +- web/template/tags/view.gohtml | 2 +- 6 files changed, 55 insertions(+), 11 deletions(-) diff --git a/pkg/expenses.go b/pkg/expenses.go index 698f12c..e09a037 100644 --- a/pkg/expenses.go +++ b/pkg/expenses.go @@ -372,3 +372,36 @@ func (form *expenseFilterForm) Parse(r *http.Request) error { form.TagsCondition.FillValue(r) 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) +} diff --git a/pkg/invoices.go b/pkg/invoices.go index a20274d..4da3a25 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -1030,13 +1030,15 @@ func handleInvoiceAction(w http.ResponseWriter, r *http.Request, action string, } type tagsForm struct { - Slug string - Tags *TagsField + Action string + Slug string + Tags *TagsField } -func newTagsForm(slug string, locale *Locale) *tagsForm { +func newTagsForm(uri string, slug string, locale *Locale) *tagsForm { return &tagsForm{ - Slug: slug, + Action: uri, + Slug: slug, Tags: &TagsField{ Name: "tags-" + slug, Label: pgettext("input", "Tags", locale), @@ -1055,7 +1057,9 @@ func (form *tagsForm) Parse(r *http.Request) error { func ServeEditInvoiceTags(w http.ResponseWriter, r *http.Request, params httprouter.Params) { conn := getConn(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)) { http.NotFound(w, r) 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) { locale := getLocale(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 { http.Error(w, err.Error(), http.StatusBadRequest) return @@ -1075,8 +1081,7 @@ func HandleUpdateInvoiceTags(w http.ResponseWriter, r *http.Request, params http http.Error(w, err.Error(), http.StatusForbidden) return } - slug := conn.MustGetText(r.Context(), "", "update invoice set tags = $1 where slug = $2 returning slug", form.Tags, form.Slug) - if slug == "" { + if conn.MustGetText(r.Context(), "", "update invoice set tags = $1 where slug = $2 returning slug", form.Tags, form.Slug) == "" { http.NotFound(w, r) } mustRenderStandaloneTemplate(w, r, "tags/view.gohtml", form) diff --git a/pkg/router.go b/pkg/router.go index 9d00f18..a83f61c 100644 --- a/pkg/router.go +++ b/pkg/router.go @@ -38,6 +38,8 @@ func NewRouter(db *Db) http.Handler { companyRouter.POST("/expenses", HandleAddExpense) companyRouter.GET("/expenses/:slug", ServeExpenseForm) 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) { mustRenderMainTemplate(w, r, "dashboard.gohtml", nil) }) diff --git a/web/template/expenses/index.gohtml b/web/template/expenses/index.gohtml index ce33f5e..d5a6b06 100644 --- a/web/template/expenses/index.gohtml +++ b/web/template/expenses/index.gohtml @@ -54,7 +54,11 @@ {{ .InvoicerName }} {{ .InvoiceDate|formatDate }} {{ .InvoiceNumber }} - + {{- range $index, $tag := .Tags }} {{- if gt $index 0 }}, {{ end -}} {{ . }} diff --git a/web/template/tags/edit.gohtml b/web/template/tags/edit.gohtml index 5421927..c12a56d 100644 --- a/web/template/tags/edit.gohtml +++ b/web/template/tags/edit.gohtml @@ -1,7 +1,7 @@ {{ define "content" }} {{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.tagsForm*/ -}} -
{{ csrfToken }} {{ putMethod }} diff --git a/web/template/tags/view.gohtml b/web/template/tags/view.gohtml index b0f6217..a4cd9cf 100644 --- a/web/template/tags/view.gohtml +++ b/web/template/tags/view.gohtml @@ -1,6 +1,6 @@ {{ define "content" }} {{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.tagsForm*/ -}} - + {{- range $index, $tag := .Tags.Tags }} {{- if gt $index 0 }}, {{ end -}} {{ . }}