Home resource uses hmc

This commit is contained in:
Teajey 2026-02-23 13:46:47 +09:00
parent ab713f6662
commit 658884c2cc
Signed by: Teajey
GPG Key ID: 970E790FE834A713
10 changed files with 273 additions and 84 deletions

View File

@ -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
)

View File

@ -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=

140
http/resources/home.go Normal file
View File

@ -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",
},
},
},
}
}

View File

@ -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{

View File

@ -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)
}

View File

@ -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)

View File

@ -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)
}

View File

@ -48,12 +48,13 @@
<div class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<div class="navbar-brand">Lishwist</div>
{{with .}}
{{with .User}}
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarToggle"
aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>{{if and .AccountLink .AccountLink.Alert}} <span
<span class="navbar-toggler-icon"></span>{{if .Alert}} <span
class="badge text-bg-danger">!</span>{{end}}
</button>
{{end}}
<div class="collapse navbar-collapse" id="navbarToggle">
{{with .Links}}
<nav>
@ -69,26 +70,28 @@
<div class="flex-grow-1"></div>
{{with .User}}
<ul class="navbar-nav">
{{with .CopyList}}
<li class="nav-item"><button class="btn btn-success"
onclick="navigator.clipboard.writeText('{{.Domain}}/lists/{{.Reference}}'); alert('The share link to your wishlist has been copied to your clipboard. Anyone with the link will be able to claim gifts for you. Share it with someone!');">Copy
share link</button></li>
{{end}}
<li class="nav-item">
<button
class="btn btn-success"
onclick="navigator.clipboard.writeText('{{.Link.Href}}'); alert('The share link to your wishlist has been copied to your clipboard. Anyone with the link will be able to claim gifts for you. Share it with someone!');">
{{.Link.Label}}
</button>
</li>
<li class="nav-item">
<div class="dropdown">
<button class="nav-link dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Logged in as '{{.Name}}'{{if and $.AccountLink $.AccountLink.Alert}} <span class="badge text-bg-danger">!</span>{{end}}
{{.Label}}{{if .Alert}} <span class="badge text-bg-danger">!</span>{{end}}
</button>
<ul class="dropdown-menu">
{{with $.AccountLink}}
<li>
<a class="dropdown-item" href="/account">Account{{if .Alert}} <span
<a class="dropdown-item" href="{{.Account.Href}}">{{.Account.Label}}{{if .Alert}} <span
class="badge text-bg-danger">!</span>{{end}}</a>
</li>
{{end}}
<li>
<form class="d-contents" method="post" action="/logout">
<button class="dropdown-item" type="submit">Logout</button>
<form class="d-contents" method="{{.Logout.Method}}" action="{{.Logout.Action}}">
{{- with .Logout.Element}}
<button class="dropdown-item" type="{{.Type}}">{{.Label}}</button>
{{- end}}
</form>
</li>
</ul>
@ -97,7 +100,6 @@
</ul>
{{end}}
</div>
{{end}}
</div>
</div>
{{end}}

View File

@ -10,83 +10,98 @@
{{template "navbar" .Navbar}}
<div class="overflow-y-scroll flex-grow-1">
<div class="container py-5">
{{- with .Wishlist}}
<section class="card mb-4">
<div class="card-body">
<h2>Your wishlist</h2>
{{with .Gifts}}
<h2>{{.Label}}</h2>
{{with .Wishes.Elements}}
<form method="post" onchange="acceptNames(this, 'deleteSubmit', 'gift')" autocomplete="off">
<ul class="list-group mb-3">
{{range .}}
{{range .Items}}
<li class="list-group-item">
<input id="wishlist_select_{{.Id}}" class="form-check-input" type="checkbox" name="gift"
value="{{.Id}}">
<label class="form-check-label stretched-link" for="wishlist_select_{{.Id}}">
{{.Name}}
<input id="wishlist_select_{{.Input.Value}}" class="form-check-input" type="{{.Input.Type}}" name="{{.Input.Name}}"
value="{{.Input.Value}}">
<label class="form-check-label stretched-link" for="wishlist_select_{{.Input.Value}}">
{{.Input.Label}}
</label>
</li>
{{end}}
</ul>
<button id="deleteSubmit" class="btn btn-danger mb-3" type="submit" name="intent" value="delete_idea"
disabled>Delete</button>
<button id="deleteSubmit" class="btn btn-danger mb-3" type="{{.Delete.Type}}" name="{{.Delete.Name}}" value="{{.Delete.Value}}"
disabled>{{.Delete.Label}}</button>
</form>
{{else}}
<p>Your list is empty. Think of some things to add!</p>
{{end}}
<form method="post">
{{- with .NewWish}}
<form method="{{.Method}}">
{{- with .Elements}}
<div class="input-group">
<input class="form-control" name="gift_name" required placeholder="Write a gift idea here" autofocus>
<button class="btn btn-primary" type="submit" name="intent" value="add_idea">Add gift idea</button>
<input class="form-control" name="{{.Name}}" {{- if .Required}} required {{- end}} placeholder="Write a gift idea here" autofocus>
<button class="btn btn-primary" type="submit" name="intent" value="add_idea">{{.Label}}</button>
</div>
{{- end}}
</form>
{{- end}}
</div>
</section>
{{- end}}
{{- with .Todo}}
<section class="card mb-4">
<div class="card-body">
<h2>Your todo list</h2>
{{with .Todo}}
<form method="post"
<h2>{{.Label}}</h2>
{{with .Wishes}}
<form method="{{.Method}}"
onchange="acceptNames(this, 'unclaimSubmit', 'gift'); acceptNames(this, 'completeSubmit', 'gift')"
autocomplete="off">
{{- with .Elements}}
<ul class="list-group mb-3">
{{range .}}
<li class="list-group-item{{if .Sent}} list-group-item-light{{end}}">
<input id="todo_select_{{.Id}}" class="form-check-input" type="checkbox" {{if .Sent}}
aria-describedby="todo_detail_{{.Id}}" disabled{{else}} name="gift" value="{{.Id}}" {{end}}>
<label for="todo_select_{{.Id}}" class="form-check-label">
{{range .Items}}
<li class="list-group-item{{if .Input.Disabled}} list-group-item-light{{end}}">
<input id="todo_select_{{.Input.Value}}" class="form-check-input" type="{{.Input.Type}}" {{if .Input.Disabled}}
aria-describedby="todo_detail_{{.Input.Value}}" disabled{{else}} name="{{.Input.Name}}" value="{{.Input.Value}}" {{end}}>
<label for="todo_select_{{.Input.Value}}" class="form-check-label">
<em>
{{if .Sent}}
<s>{{.Name}}</s>
{{if .Input.Disabled}}
<s>{{.Input.Label}}</s>
{{else}}
{{.Name}}
{{.Input.Label}}
{{end}}
</em>
</label>
<span id="todo_detail_{{.Id}}">
for <a href="/lists/{{.RecipientRef}}">{{.RecipientName}}</a>
<span id="todo_detail_{{.Input.Value}}">
for <a href="{{.Recipient.Href}}">{{.Recipient.Label}}</a>
</span>
</li>
{{end}}
</ul>
<button id="unclaimSubmit" class="btn btn-warning" type="submit" name="intent" value="unclaim_todo"
disabled>Unclaim</button>
<button id="completeSubmit" class="btn btn-success" type="submit" name="intent" value="complete_todo"
disabled>Complete</button>
{{- with .Claim}}
<button id="unclaimSubmit" class="btn btn-warning" type="{{.Type}}" name="{{.Name}}" value="{{.Value}}"
disabled>{{.Label}}</button>
{{- end}}
{{- with .Complete}}
<button id="completeSubmit" class="btn btn-success" type="{{.Type}}" name="{{.Name}}" value="{{.Value}}"
disabled>{{.Label}}</button>
{{- end}}
{{- end}}
</form>
{{else}}
<p class="mb-0">When you claim gifts for others, they will appear here.</p>
{{end}}
</div>
</section>
{{- end}}
{{with .GroupsList}}
<section class="card">
<div class="card-body">
<h2>Your groups</h2>
{{with .Groups}}
<h2>{{.Label}}</h2>
{{with .Links}}
<ul class="list-group">
{{range .}}
<li class="list-group-item">
<a href="/groups/{{.Reference}}">{{.Name}}</a>
<a href="{{.Href}}">{{.Label}}</a>
</li>
{{end}}
</ul>
@ -95,6 +110,7 @@
{{end}}
</div>
</section>
{{end}}
</div>
</div>
</div>

View File

@ -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
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",
},
},
},
}
}