lishwist/http/resources/home.go
2026-02-23 13:50:07 +09:00

141 lines
2.9 KiB
Go

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