From b5dfb135df50a68193cc8dc6b0ede1fb9f4f092e Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:29:34 +0900 Subject: [PATCH] Complete old home page hmc-related refactor --- http/resources/home.go | 121 ++++++++++++++++++++++++------------ http/routing/home.go | 88 ++++++++++++++++++++++---- http/server/server.go | 3 +- http/templates/base.gotmpl | 10 ++- http/templates/home.gotmpl | 25 ++++---- http/templates/templates.go | 1 + 6 files changed, 178 insertions(+), 70 deletions(-) diff --git a/http/resources/home.go b/http/resources/home.go index a217df3..64bab47 100644 --- a/http/resources/home.go +++ b/http/resources/home.go @@ -3,6 +3,7 @@ package resources import ( lishwist "lishwist/core" "lishwist/http/templates" + "net/url" "github.com/Teajey/hmc" ) @@ -14,6 +15,8 @@ type LinksList struct { type HomeProps struct { hmc.Namespace + Error string + Warning string Navbar templates.NavCollapse `json:"-"` Wishlist Wishlist `json:"wishlist"` Todo Wishlist `json:"todo"` @@ -27,10 +30,9 @@ type Wish struct { } type Wishes struct { - Items []Wish `json:"items"` - Claim *hmc.Input `json:"claim,omitempty" xml:",omitempty"` - Complete *hmc.Input `json:"complete,omitempty" xml:",omitempty"` - Delete *hmc.Input `json:"delete,omitempty" xml:",omitempty"` + Items []Wish `json:"items"` + Select hmc.Select + Intent hmc.Select } type Wishlist struct { @@ -40,30 +42,24 @@ type Wishlist struct { } func InitialWishlist(currentUserId string, wishes []lishwist.Wish) Wishlist { - wishInputs := make([]Wish, len(wishes)) + wishInputs := make([]hmc.Option, len(wishes)) + items := make([]Wish, len(wishes)) + for i, w := range wishes { - isMine := w.ClaimantId == currentUserId - // createdByMe := w.CreatorId == currentUserId - // anothersIdea := w.RecipientId != w.CreatorId - - wsh := Wish{ - Input: hmc.Input{ - Label: w.Name, - Type: "checkbox", - }, - } - - if isMine { - wsh.Input.Name = "claimed" - wsh.Input.Value = w.Id - } else if w.ClaimantId != "" { - wsh.Input.Disabled = true - } else { - wsh.Input.Name = "unclaimed" - wsh.Input.Value = w.Id + wsh := hmc.Option{ + Label: w.Name, + Value: w.Id, } wishInputs[i] = wsh + items[i] = Wish{ + Input: hmc.Input{ + Label: w.Name, + Value: w.Id, + Type: "checkbox", + Name: "wish", + }, + } } return Wishlist{ @@ -71,12 +67,22 @@ func InitialWishlist(currentUserId string, wishes []lishwist.Wish) Wishlist { Wishes: hmc.Form[Wishes]{ Method: "POST", Elements: Wishes{ - Items: wishInputs, - Delete: &hmc.Input{ - Label: "Delete", - Type: "submit", - Name: "intent", - Value: "delete", + Items: items, + Select: hmc.Select{ + Label: "Wishes", + Multiple: true, + Name: "wish", + Required: true, + Options: wishInputs, + }, + Intent: hmc.Select{ + Label: "Intent", + Required: true, + Name: "intent", + Options: []hmc.Option{{ + Label: "Delete", + Value: "delete", + }}, }, }, }, @@ -92,6 +98,7 @@ func InitialWishlist(currentUserId string, wishes []lishwist.Wish) Wishlist { func InitialTodo(todo []lishwist.Wish) Wishlist { wishInputs := make([]Wish, len(todo)) + options := make([]hmc.Option, len(todo)) for i, w := range todo { wsh := Wish{ @@ -105,15 +112,21 @@ func InitialTodo(todo []lishwist.Wish) Wishlist { }, Sent: w.Sent, } + opt := hmc.Option{ + Label: w.Name, + Value: w.Id, + } if w.Sent { wsh.Input.Disabled = true + opt.Disabled = true } else { - wsh.Input.Name = "claimed" + wsh.Input.Name = "todo" wsh.Input.Value = w.Id } wishInputs[i] = wsh + options[i] = opt } return Wishlist{ @@ -122,19 +135,45 @@ func InitialTodo(todo []lishwist.Wish) Wishlist { Method: "POST", Elements: Wishes{ Items: wishInputs, - Claim: &hmc.Input{ - Label: "Unclaim", - Type: "submit", - Name: "intent", - Value: "unclaim_todo", + Select: hmc.Select{ + Label: "Todos", + Multiple: true, + Name: "todo", + Required: true, + Options: options, }, - Complete: &hmc.Input{ - Label: "Complete", - Type: "submit", - Name: "intent", - Value: "unclaim_todo", + Intent: hmc.Select{ + Label: "Intent", + Required: true, + Name: "intent", + Options: []hmc.Option{ + { + Label: "Unclaim", + Value: "unclaim_todo", + }, + { + Label: "Complete", + Value: "complete_todo", + }, + }, }, }, }, } } + +func (w Wishlist) ExtractFormValues(form url.Values) { + if w.NewWish != nil { + w.NewWish.Elements.ExtractFormValue(form) + } + + err := w.Wishes.Elements.Select.ExtractFormValue(form) + if err != nil { + w.Wishes.Elements.Select.Error = err.Error() + } + + err = w.Wishes.Elements.Intent.ExtractFormValue(form) + if err != nil { + w.Wishes.Elements.Intent.Error = err.Error() + } +} diff --git a/http/routing/home.go b/http/routing/home.go index 848ce4d..ff7392d 100644 --- a/http/routing/home.go +++ b/http/routing/home.go @@ -3,7 +3,9 @@ package routing import ( "fmt" "log" + "maps" "net/http" + "slices" lishwist "lishwist/core" "lishwist/http/resources" @@ -15,7 +17,19 @@ import ( "github.com/Teajey/rsvp" ) +func mapKeysIntersect(a, b map[string][]string) []string { + var out []string + for k := range a { + if _, ok := b[k]; ok { + out = append(out, k) + } + } + return out +} + func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { + w.DefaultTemplateName("home.gotmpl") + gifts, err := app.GetWishes() if err != nil { log.Printf("Failed to get gifts: %s\n", err) @@ -39,9 +53,11 @@ func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter Href: fmt.Sprintf("groups/%s", g.Reference), } } + navbar := templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin) + navbar.Links = nil p := resources.HomeProps{ Namespace: hmc.NS(), - Navbar: templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin), + Navbar: navbar, Wishlist: resources.InitialWishlist(user.Id, gifts), Todo: resources.InitialTodo(todo), GroupsList: resources.LinksList{ @@ -49,21 +65,67 @@ func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter Links: groupLinks, }, } - return response.Data("home.gotmpl", p) -} -func HomePost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { - err := r.ParseForm() + if r.Method == http.MethodGet { + return rsvp.Data(p) + } + + err = r.ParseForm() if err != nil { - return response.Error("Failed to parse form").StatusBadRequest() + p.Error = "Failed to parse form" + return rsvp.Data(p).StatusUnprocessableEntity() } - switch r.Form.Get("intent") { - case "add_idea": - return WishlistAdd(app, w, r) - case "delete_idea": - return WishlistDelete(app, w, r) - default: - return TodoUpdate(app, w, r) + if len(r.Form) < 1 { + p.Error = "No parameters provided" + return rsvp.Data(p).StatusBadRequest() } + + wishForm := maps.Clone(r.Form) + p.Wishlist.ExtractFormValues(wishForm) + p.Todo.ExtractFormValues(r.Form) + + extra := mapKeysIntersect(wishForm, r.Form) + if len(extra) > 0 { + p.Warning = "Extraneous form values" + } + + if p.Wishlist.Wishes.Elements.Intent.Value() == "delete" { + wishesToDelete := slices.Collect(p.Wishlist.Wishes.Elements.Select.Values()) + err := app.RevokeWishes(wishesToDelete...) + if err != nil { + log.Printf("Failed to revoke wishes: %s\n", err) + p.Wishlist.Wishes.Elements.Intent.Error = "Something went wrong" + return rsvp.Data(p).StatusInternalServerError() + } + } + + if p.Wishlist.NewWish.Elements.Value != "" { + _, err := app.MakeWish(p.Wishlist.NewWish.Elements.Value) + if err != nil { + log.Printf("Failed to make wish: %s\n", err) + p.Wishlist.NewWish.Elements.Error = "Something went wrong" + return rsvp.Data(p).StatusInternalServerError() + } + } + + todosToChange := slices.Collect(p.Todo.Wishes.Elements.Select.Values()) + + if p.Todo.Wishes.Elements.Intent.Value() == "unclaim_todo" { + err := app.ClaimWishes([]string{}, todosToChange) + if err != nil { + log.Printf("Failed to unclaim wish: %s\n", err) + p.Todo.Wishes.Elements.Intent.Error = "Something went wrong" + return rsvp.Data(p).StatusInternalServerError() + } + } else if p.Todo.Wishes.Elements.Intent.Value() == "complete_todo" { + err := app.CompleteWishes(todosToChange) + if err != nil { + log.Printf("Failed to complete wish: %s\n", err) + p.Todo.Wishes.Elements.Intent.Error = "Something went wrong" + return rsvp.Data(p).StatusInternalServerError() + } + } + + return rsvp.Data(p).StatusSeeOther(".") } diff --git a/http/server/server.go b/http/server/server.go index cd6ce8e..38f6369 100644 --- a/http/server/server.go +++ b/http/server/server.go @@ -72,11 +72,10 @@ func Create(useSecureCookies bool) *mux.Serve { r.Private.HandleFunc("POST /account", routing.AccountPost) r.Private.HandleFunc("POST /groups/{groupReference}", routing.GroupPost) - r.Private.HandleFunc("POST /home", routing.HomePost) r.Private.HandleFunc("POST /lists/{userReference}", routing.ForeignWishlistPost) r.Private.HandleFunc("POST /logout", routing.LogoutPost) r.Private.HandleFunc("POST /users/{userReference}", routing.UserPost) - r.Private.HandleFunc("POST /{$}", routing.HomePost) + r.Private.HandleFunc("POST /{$}", routing.Home) // Deprecated r.HandleFunc("GET /group/{groupReference}", middleware.PrefixMovedPermanently("/group/", "/groups/")) diff --git a/http/templates/base.gotmpl b/http/templates/base.gotmpl index c192c32..03ca82d 100644 --- a/http/templates/base.gotmpl +++ b/http/templates/base.gotmpl @@ -70,6 +70,7 @@
{{with .User}}