Compare commits

..

No commits in common. "1caf06934fdc60ce35be4d9876ead98457dbdc0e" and "b5dfb135df50a68193cc8dc6b0ede1fb9f4f092e" have entirely different histories.

3 changed files with 63 additions and 83 deletions

View File

@ -37,26 +37,11 @@ type Wishes struct {
type Wishlist struct { type Wishlist struct {
Label string `json:"label" xml:",chardata"` Label string `json:"label" xml:",chardata"`
Wishes *hmc.Form[Wishes] `json:"wishes"` Wishes hmc.Form[Wishes] `json:"wishes"`
NewWish *hmc.Form[hmc.Input] `json:"new_wish"` NewWish *hmc.Form[hmc.Input] `json:"new_wish"`
} }
func InitialWishlist(wishes []lishwist.Wish) Wishlist { func InitialWishlist(currentUserId string, 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)) wishInputs := make([]hmc.Option, len(wishes))
items := make([]Wish, len(wishes)) items := make([]Wish, len(wishes))
@ -77,41 +62,41 @@ func InitialWishlist(wishes []lishwist.Wish) Wishlist {
} }
} }
list.Wishes = &hmc.Form[Wishes]{ return Wishlist{
Method: "POST", Label: "Your wishlist",
Elements: Wishes{ Wishes: hmc.Form[Wishes]{
Items: items, Method: "POST",
Select: hmc.Select{ Elements: Wishes{
Label: "Wishes", Items: items,
Multiple: true, Select: hmc.Select{
Name: "wish", Label: "Wishes",
Required: true, Multiple: true,
Options: wishInputs, Name: "wish",
Required: true,
Options: wishInputs,
},
Intent: hmc.Select{
Label: "Intent",
Required: true,
Name: "intent",
Options: []hmc.Option{{
Label: "Delete",
Value: "delete",
}},
},
}, },
Intent: hmc.Select{ },
Label: "Intent", NewWish: &hmc.Form[hmc.Input]{
Required: true, Method: "POST",
Name: "intent", Elements: hmc.Input{
Options: []hmc.Option{{ Label: "Add gift idea",
Label: "Delete", Name: "gift_name",
Value: "delete",
}},
}, },
}, },
} }
return list
} }
func InitialTodo(todo []lishwist.Wish) Wishlist { func InitialTodo(todo []lishwist.Wish) Wishlist {
list := Wishlist{
Label: "Your todo list",
}
if len(todo) < 1 {
return list
}
wishInputs := make([]Wish, len(todo)) wishInputs := make([]Wish, len(todo))
options := make([]hmc.Option, len(todo)) options := make([]hmc.Option, len(todo))
@ -144,36 +129,37 @@ func InitialTodo(todo []lishwist.Wish) Wishlist {
options[i] = opt options[i] = opt
} }
list.Wishes = &hmc.Form[Wishes]{ return Wishlist{
Method: "POST", Label: "Your todo list",
Elements: Wishes{ Wishes: hmc.Form[Wishes]{
Items: wishInputs, Method: "POST",
Select: hmc.Select{ Elements: Wishes{
Label: "Todos", Items: wishInputs,
Multiple: true, Select: hmc.Select{
Name: "todo", Label: "Todos",
Required: true, Multiple: true,
Options: options, Name: "todo",
}, Required: true,
Intent: hmc.Select{ Options: options,
Label: "Intent", },
Required: true, Intent: hmc.Select{
Name: "intent", Label: "Intent",
Options: []hmc.Option{ Required: true,
{ Name: "intent",
Label: "Unclaim", Options: []hmc.Option{
Value: "unclaim_todo", {
}, Label: "Unclaim",
{ Value: "unclaim_todo",
Label: "Complete", },
Value: "complete_todo", {
Label: "Complete",
Value: "complete_todo",
},
}, },
}, },
}, },
}, },
} }
return list
} }
func (w Wishlist) ExtractFormValues(form url.Values) { func (w Wishlist) ExtractFormValues(form url.Values) {
@ -181,10 +167,6 @@ func (w Wishlist) ExtractFormValues(form url.Values) {
w.NewWish.Elements.ExtractFormValue(form) w.NewWish.Elements.ExtractFormValue(form)
} }
if w.Wishes == nil {
return
}
err := w.Wishes.Elements.Select.ExtractFormValue(form) err := w.Wishes.Elements.Select.ExtractFormValue(form)
if err != nil { if err != nil {
w.Wishes.Elements.Select.Error = err.Error() w.Wishes.Elements.Select.Error = err.Error()

View File

@ -58,7 +58,7 @@ func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter
p := resources.HomeProps{ p := resources.HomeProps{
Namespace: hmc.NS(), Namespace: hmc.NS(),
Navbar: navbar, Navbar: navbar,
Wishlist: resources.InitialWishlist(gifts), Wishlist: resources.InitialWishlist(user.Id, gifts),
Todo: resources.InitialTodo(todo), Todo: resources.InitialTodo(todo),
GroupsList: resources.LinksList{ GroupsList: resources.LinksList{
Label: "Your groups", Label: "Your groups",
@ -90,7 +90,7 @@ func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter
p.Warning = "Extraneous form values" p.Warning = "Extraneous form values"
} }
if p.Wishlist.Wishes != nil && p.Wishlist.Wishes.Elements.Intent.Value() == "delete" { if p.Wishlist.Wishes.Elements.Intent.Value() == "delete" {
wishesToDelete := slices.Collect(p.Wishlist.Wishes.Elements.Select.Values()) wishesToDelete := slices.Collect(p.Wishlist.Wishes.Elements.Select.Values())
err := app.RevokeWishes(wishesToDelete...) err := app.RevokeWishes(wishesToDelete...)
if err != nil { if err != nil {
@ -109,16 +109,16 @@ func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter
} }
} }
if p.Todo.Wishes != nil && p.Todo.Wishes.Elements.Intent.Value() == "unclaim_todo" { todosToChange := slices.Collect(p.Todo.Wishes.Elements.Select.Values())
todosToChange := slices.Collect(p.Todo.Wishes.Elements.Select.Values())
if p.Todo.Wishes.Elements.Intent.Value() == "unclaim_todo" {
err := app.ClaimWishes([]string{}, todosToChange) err := app.ClaimWishes([]string{}, todosToChange)
if err != nil { if err != nil {
log.Printf("Failed to unclaim wish: %s\n", err) log.Printf("Failed to unclaim wish: %s\n", err)
p.Todo.Wishes.Elements.Intent.Error = "Something went wrong" p.Todo.Wishes.Elements.Intent.Error = "Something went wrong"
return rsvp.Data(p).StatusInternalServerError() return rsvp.Data(p).StatusInternalServerError()
} }
} else if p.Todo.Wishes != nil && p.Todo.Wishes.Elements.Intent.Value() == "complete_todo" { } else if p.Todo.Wishes.Elements.Intent.Value() == "complete_todo" {
todosToChange := slices.Collect(p.Todo.Wishes.Elements.Select.Values())
err := app.CompleteWishes(todosToChange) err := app.CompleteWishes(todosToChange)
if err != nil { if err != nil {
log.Printf("Failed to complete wish: %s\n", err) log.Printf("Failed to complete wish: %s\n", err)

View File

@ -14,8 +14,7 @@
<section class="card mb-4"> <section class="card mb-4">
<div class="card-body"> <div class="card-body">
<h2>{{.Label}}</h2> <h2>{{.Label}}</h2>
{{with .Wishes}} {{with .Wishes.Elements}}
{{with .Elements}}
<form method="post" onchange="acceptNames(this, 'deleteSubmit', 'wish')" autocomplete="off"> <form method="post" onchange="acceptNames(this, 'deleteSubmit', 'wish')" autocomplete="off">
<ul class="list-group mb-3"> <ul class="list-group mb-3">
{{range .Items}} {{range .Items}}
@ -31,7 +30,6 @@
<button id="deleteSubmit" class="btn btn-danger mb-3" name="intent" value="delete" <button id="deleteSubmit" class="btn btn-danger mb-3" name="intent" value="delete"
disabled>Delete</button> disabled>Delete</button>
</form> </form>
{{end}}
{{else}} {{else}}
<p>Your list is empty. Think of some things to add!</p> <p>Your list is empty. Think of some things to add!</p>
{{end}} {{end}}