From 658884c2cc608121fe036117fa021181d4d98d35 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Mon, 23 Feb 2026 13:46:47 +0900 Subject: [PATCH] Home resource uses hmc --- http/go.mod | 2 +- http/go.sum | 4 +- http/resources/home.go | 140 +++++++++++++++++++++++++++++++ http/routing/account.go | 13 ++- http/routing/foreign_wishlist.go | 2 +- http/routing/groups.go | 4 +- http/routing/home.go | 32 ++++--- http/templates/base.gotmpl | 30 +++---- http/templates/home.gotmpl | 80 +++++++++++------- http/templates/templates.go | 50 ++++++----- 10 files changed, 273 insertions(+), 84 deletions(-) create mode 100644 http/resources/home.go diff --git a/http/go.mod b/http/go.mod index 02a922f..092397b 100644 --- a/http/go.mod +++ b/http/go.mod @@ -9,7 +9,7 @@ require ( ) require ( - github.com/Teajey/hmc v0.6.0-rc.5 + github.com/Teajey/hmc v0.6.0-rc.12 github.com/gorilla/securecookie v1.1.2 ) diff --git a/http/go.sum b/http/go.sum index 4ededae..1db2e8e 100644 --- a/http/go.sum +++ b/http/go.sum @@ -1,5 +1,5 @@ -github.com/Teajey/hmc v0.6.0-rc.5 h1:snog8SPc/FtQ/CrtAoFWuJa7r35XrSOwE8xw1Y1XtnM= -github.com/Teajey/hmc v0.6.0-rc.5/go.mod h1:9K7SL4RyybNLHooqsZcyFoL6QGqqa2zPgIBidw4qWQY= +github.com/Teajey/hmc v0.6.0-rc.12 h1:Vkx6mJa3pfMlYx4vyI9LfVLHa8fNyl8LikMvXBfDGZk= +github.com/Teajey/hmc v0.6.0-rc.12/go.mod h1:9K7SL4RyybNLHooqsZcyFoL6QGqqa2zPgIBidw4qWQY= github.com/Teajey/rsvp v0.19.0-rc.2 h1:h4V7c3uGlSKajkWKt3fldFV+XTMEI8A4o05PMY8xv9A= github.com/Teajey/rsvp v0.19.0-rc.2/go.mod h1:z0L20VphVg+Ec2+hnpLFTG2MZTrWYFprav1kpxDba0Q= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= diff --git a/http/resources/home.go b/http/resources/home.go new file mode 100644 index 0000000..a217df3 --- /dev/null +++ b/http/resources/home.go @@ -0,0 +1,140 @@ +package resources + +import ( + lishwist "lishwist/core" + "lishwist/http/templates" + + "github.com/Teajey/hmc" +) + +type LinksList struct { + Label string `json:"label" xml:",chardata"` + Links []hmc.Link `json:"links" xml:"Link"` +} + +type HomeProps struct { + hmc.Namespace + Navbar templates.NavCollapse `json:"-"` + Wishlist Wishlist `json:"wishlist"` + Todo Wishlist `json:"todo"` + GroupsList LinksList `json:"groups" xml:"LinksList"` +} + +type Wish struct { + Input hmc.Input `json:"input"` + Recipient *hmc.Link `json:"recipient,omitempty" xml:",omitempty"` + Sent bool `json:"sent" xml:"sent,attr"` +} + +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"` +} + +type Wishlist struct { + Label string `json:"label" xml:",chardata"` + Wishes hmc.Form[Wishes] `json:"wishes"` + NewWish *hmc.Form[hmc.Input] `json:"new_wish"` +} + +func InitialWishlist(currentUserId string, wishes []lishwist.Wish) Wishlist { + wishInputs := 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 + } + + wishInputs[i] = wsh + } + + return Wishlist{ + Label: "Your wishlist", + Wishes: hmc.Form[Wishes]{ + Method: "POST", + Elements: Wishes{ + Items: wishInputs, + Delete: &hmc.Input{ + Label: "Delete", + Type: "submit", + Name: "intent", + Value: "delete", + }, + }, + }, + NewWish: &hmc.Form[hmc.Input]{ + Method: "POST", + Elements: hmc.Input{ + Label: "Add gift idea", + Name: "gift_name", + }, + }, + } +} + +func InitialTodo(todo []lishwist.Wish) Wishlist { + wishInputs := make([]Wish, len(todo)) + + for i, w := range todo { + wsh := Wish{ + Input: hmc.Input{ + Label: w.Name, + Type: "checkbox", + }, + Recipient: &hmc.Link{ + Label: w.RecipientName, + Href: "/lists/" + w.RecipientRef, + }, + Sent: w.Sent, + } + + if w.Sent { + wsh.Input.Disabled = true + } else { + wsh.Input.Name = "claimed" + wsh.Input.Value = w.Id + } + + wishInputs[i] = wsh + } + + return Wishlist{ + Label: "Your todo list", + Wishes: hmc.Form[Wishes]{ + Method: "POST", + Elements: Wishes{ + Items: wishInputs, + Claim: &hmc.Input{ + Label: "Unclaim", + Type: "submit", + Name: "intent", + Value: "unclaim_todo", + }, + Complete: &hmc.Input{ + Label: "Complete", + Type: "submit", + Name: "intent", + Value: "unclaim_todo", + }, + }, + }, + } +} diff --git a/http/routing/account.go b/http/routing/account.go index 3c07bb4..eab7def 100644 --- a/http/routing/account.go +++ b/http/routing/account.go @@ -1,6 +1,7 @@ package routing import ( + "fmt" "log" "net/http" @@ -45,7 +46,17 @@ func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confi return &AccountProps{ Navbar: templates.NavCollapse{ Links: []hmc.Link{{Href: "/", Label: "Home"}}, - User: &templates.User{Name: username}, + User: &templates.NavUser{ + Label: fmt.Sprintf("Logged in as '%s'", username), + Logout: hmc.Form[hmc.Input]{ + Method: "POST", + Action: "/logout", + Elements: hmc.Input{ + Label: "Logout", + Type: "submit", + }, + }, + }, }, PasswordFromAdmin: passwordFromAdmin, Password: hmc.Input{ diff --git a/http/routing/foreign_wishlist.go b/http/routing/foreign_wishlist.go index c8db9ef..91e868e 100644 --- a/http/routing/foreign_wishlist.go +++ b/http/routing/foreign_wishlist.go @@ -39,7 +39,7 @@ func ForeignWishlist(app *lishwist.Session, session *session.Session, w rsvp.Res log.Printf("%q couldn't get wishes of other user %q: %s\n", user.Name, otherUser.Name, err) return response.Error("An error occurred while fetching this user :(").StatusInternalServerError() } - p := foreignWishlistProps{Navbar: templates.UserNavCollapse(user.Name, user.PasswordFromAdmin), CurrentUserId: user.Id, Username: otherUser.Name, Gifts: wishes} + p := foreignWishlistProps{Navbar: templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin), CurrentUserId: user.Id, Username: otherUser.Name, Gifts: wishes} return response.Data("foreign_wishlist.gotmpl", p) } diff --git a/http/routing/groups.go b/http/routing/groups.go index 5e585fb..5c0f718 100644 --- a/http/routing/groups.go +++ b/http/routing/groups.go @@ -36,7 +36,7 @@ func AdminGroup(app *lishwist.Session, w rsvp.ResponseWriter, r *http.Request) r group.Members = slices.Delete(group.Members, index, index+1) } p := GroupProps{ - Navbar: templates.UserNavCollapse(user.Name, user.PasswordFromAdmin), + Navbar: templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin), Group: group, } return response.Data("group_page.gotmpl", p) @@ -59,7 +59,7 @@ func Group(app *lishwist.Session, session *session.Session, w rsvp.ResponseWrite index := group.MemberIndex(user.Id) group.Members = slices.Delete(group.Members, index, index+1) p := GroupProps{ - Navbar: templates.UserNavCollapse(user.Name, user.PasswordFromAdmin), + Navbar: templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin), Group: group, } return response.Data("group_page.gotmpl", p) diff --git a/http/routing/home.go b/http/routing/home.go index eb5a71d..848ce4d 100644 --- a/http/routing/home.go +++ b/http/routing/home.go @@ -1,25 +1,20 @@ package routing import ( + "fmt" "log" "net/http" lishwist "lishwist/core" - "lishwist/http/env" + "lishwist/http/resources" "lishwist/http/response" "lishwist/http/session" "lishwist/http/templates" + "github.com/Teajey/hmc" "github.com/Teajey/rsvp" ) -type HomeProps struct { - Navbar templates.NavCollapse - Gifts []lishwist.Wish - Todo []lishwist.Wish - Groups []lishwist.Group -} - func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { gifts, err := app.GetWishes() if err != nil { @@ -37,10 +32,23 @@ func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter log.Printf("Failed to get groups: %s\n", err) return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError() } - p := HomeProps{Navbar: templates.NavCollapse{ - User: &templates.User{Name: user.Name, CopyList: &templates.CopyList{Domain: env.Configuration.HostUrl, Reference: user.Reference}}, - AccountLink: &templates.AccountLink{Alert: user.PasswordFromAdmin}, - }, Gifts: gifts, Todo: todo, Groups: groups} + groupLinks := make([]hmc.Link, len(groups)) + for i, g := range groups { + groupLinks[i] = hmc.Link{ + Label: g.Name, + Href: fmt.Sprintf("groups/%s", g.Reference), + } + } + p := resources.HomeProps{ + Namespace: hmc.NS(), + Navbar: templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin), + Wishlist: resources.InitialWishlist(user.Id, gifts), + Todo: resources.InitialTodo(todo), + GroupsList: resources.LinksList{ + Label: "Your groups", + Links: groupLinks, + }, + } return response.Data("home.gotmpl", p) } diff --git a/http/templates/base.gotmpl b/http/templates/base.gotmpl index 0675619..c192c32 100644 --- a/http/templates/base.gotmpl +++ b/http/templates/base.gotmpl @@ -48,12 +48,13 @@
diff --git a/http/templates/templates.go b/http/templates/templates.go index e4cf8ed..32fab25 100644 --- a/http/templates/templates.go +++ b/http/templates/templates.go @@ -1,40 +1,52 @@ package templates import ( + "fmt" "html/template" + "lishwist/http/env" "github.com/Teajey/hmc" ) -type AccountLink struct { - Alert bool -} - -type CopyList struct { - Domain string - Reference string -} - -type User struct { - Name string - CopyList *CopyList +type NavUser struct { + Label string `json:"label" xml:",chardata"` + Alert bool `json:"alert,omitempty" xml:",omitempty"` + Link *hmc.Link `json:"link,omitempty" xml:",omitempty"` + Account *hmc.Link `json:"account,omitempty" xml:",omitempty"` + Logout hmc.Form[hmc.Input] `json:"logout"` } type NavCollapse struct { - User *User `json:",omitempty"` - AccountLink *AccountLink `json:",omitempty"` - Links []hmc.Link + Links []hmc.Link + User *NavUser `json:",omitempty"` } func DefaultNavCollapse() NavCollapse { return NavCollapse{Links: []hmc.Link{{Href: "/", Label: "Home"}}} } -func UserNavCollapse(username string, accountAlert bool) NavCollapse { +func UserNavCollapse(username, reference string, accountAlert bool) NavCollapse { return NavCollapse{ - Links: []hmc.Link{{Href: "/", Label: "Home"}}, - User: &User{Name: username}, - AccountLink: &AccountLink{Alert: accountAlert}, + User: &NavUser{ + Label: fmt.Sprintf("Logged in as '%s'", username), + Alert: accountAlert, + Link: &hmc.Link{ + Label: "Share link", + Href: fmt.Sprintf("%s/lists/%s", env.Configuration.HostUrl, reference), + }, + Account: &hmc.Link{ + Label: "Account", + Href: "/account", + }, + Logout: hmc.Form[hmc.Input]{ + Method: "POST", + Action: "/logout", + Elements: hmc.Input{ + Label: "Logout", + Type: "submit", + }, + }, + }, } }