lishwist/http/resources/home.go
2026-11-18 00:11:55 +09:00

198 lines
3.6 KiB
Go

package resources
import (
lishwist "lishwist/core"
"lishwist/http/templates"
"net/url"
"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
Error string
Warning string
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"`
Select hmc.Select
Intent hmc.Select
}
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(wishes []lishwist.Wish) Wishlist {
list := Wishlist{
Label: "Your wishlist",
NewWish: &hmc.Form[hmc.Input]{
Method: "POST",
Elements: hmc.Input{
Label: "Add gift idea",
Name: "gift_name",
},
},
}
if len(wishes) < 1 {
return list
}
wishInputs := make([]hmc.Option, len(wishes))
items := make([]Wish, len(wishes))
for i, w := range wishes {
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",
},
}
}
list.Wishes = &hmc.Form[Wishes]{
Method: "POST",
Elements: Wishes{
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",
}},
},
},
}
return list
}
func InitialTodo(todo []lishwist.Wish) Wishlist {
list := Wishlist{
Label: "Your todo list",
}
if len(todo) < 1 {
return list
}
wishInputs := make([]Wish, len(todo))
options := make([]hmc.Option, 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,
}
opt := hmc.Option{
Label: w.Name,
Value: w.Id,
}
if w.Sent {
wsh.Input.Disabled = true
opt.Disabled = true
} else {
wsh.Input.Name = "todo"
wsh.Input.Value = w.Id
}
wishInputs[i] = wsh
options[i] = opt
}
list.Wishes = &hmc.Form[Wishes]{
Method: "POST",
Elements: Wishes{
Items: wishInputs,
Select: hmc.Select{
Label: "Todos",
Multiple: true,
Name: "todo",
Required: true,
Options: options,
},
Intent: hmc.Select{
Label: "Intent",
Required: true,
Name: "intent",
Options: []hmc.Option{
{
Label: "Unclaim",
Value: "unclaim_todo",
},
{
Label: "Complete",
Value: "complete_todo",
},
},
},
},
}
return list
}
func (w Wishlist) ExtractFormValues(form url.Values) {
if w.NewWish != nil {
w.NewWish.Elements.ExtractFormValue(form)
}
if w.Wishes == nil {
return
}
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()
}
}