refac: slim up endpoints

This commit is contained in:
Teajey 2024-05-23 22:00:27 +12:00
parent b38e707ae2
commit 37c3f1f20d
Signed by: Teajey
GPG Key ID: 970E790FE834A713
7 changed files with 41 additions and 26 deletions

View File

@ -42,7 +42,7 @@ func (ctx *Context) WishlistDelete(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (ctx *Context) UpdateForeignWishlist(w http.ResponseWriter, r *http.Request) {
func (ctx *Context) ForeignWishlistPost(w http.ResponseWriter, r *http.Request) {
user := ctx.Auth.ExpectUser(r)
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
@ -91,5 +91,5 @@ func (ctx *Context) UpdateForeignWishlist(w http.ResponseWriter, r *http.Request
default:
http.Error(w, "Invalid intent", http.StatusBadRequest)
}
http.Redirect(w, r, "/"+userReference, http.StatusSeeOther)
http.Redirect(w, r, "/list/"+userReference, http.StatusSeeOther)
}

View File

@ -15,7 +15,7 @@ type ForeignWishlistProps struct {
Gifts []db.Gift
}
func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) {
func (ctx *Context) ForeignWishlist(w http.ResponseWriter, r *http.Request) {
userReference := r.PathValue("userReference")
user := ctx.Auth.ExpectUser(r)
if user.Reference == userReference {

View File

@ -31,3 +31,21 @@ func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) {
p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference, HostUrl: env.HostUrl.String()}
templates.Execute(w, "home.gotmpl", p)
}
func (ctx *Context) HomePost(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "Couldn't parse form", http.StatusBadRequest)
return
}
switch r.Form.Get("intent") {
case "add_idea":
ctx.WishlistAdd(w, r)
return
case "delete_idea":
ctx.WishlistDelete(w, r)
return
default:
ctx.TodoUpdate(w, r)
return
}
}

View File

@ -11,15 +11,15 @@ func (ctx *Context) TodoUpdate(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
switch r.Form.Get("mode") {
case "unclaim":
switch r.Form.Get("intent") {
case "unclaim_todo":
unclaims := r.Form["gift"]
err := user.ClaimGifts([]string{}, unclaims)
if err != nil {
http.Error(w, "Failed to update claim...", http.StatusInternalServerError)
return
}
case "complete":
case "complete_todo":
claims := r.Form["gift"]
err := user.CompleteGifts(claims)
if err != nil {
@ -28,7 +28,7 @@ func (ctx *Context) TodoUpdate(w http.ResponseWriter, r *http.Request) {
return
}
default:
http.Error(w, "Invalid mode", http.StatusBadRequest)
http.Error(w, "Invalid intent", http.StatusBadRequest)
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}

10
main.go
View File

@ -35,12 +35,10 @@ func main() {
publicMux.HandleFunc("GET /", authMiddleware.Login)
publicMux.HandleFunc("POST /", authMiddleware.LoginPost)
protectedMux.HandleFunc("GET /", ctx.Home)
protectedMux.HandleFunc("GET /{userReference}", ctx.ViewForeignWishlist)
protectedMux.HandleFunc("POST /{userReference}/update", ctx.UpdateForeignWishlist)
protectedMux.HandleFunc("POST /wishlist/add", ctx.WishlistAdd)
protectedMux.HandleFunc("POST /wishlist/delete", ctx.WishlistDelete)
protectedMux.HandleFunc("POST /todo/update", ctx.TodoUpdate)
protectedMux.HandleFunc("GET /{$}", ctx.Home)
protectedMux.HandleFunc("POST /{$}", ctx.HomePost)
protectedMux.HandleFunc("GET /list/{userReference}", ctx.ForeignWishlist)
protectedMux.HandleFunc("POST /list/{userReference}", ctx.ForeignWishlistPost)
protectedMux.HandleFunc("POST /logout", authMiddleware.LogoutPost)
http.Handle("/", authMiddleware)

View File

@ -32,7 +32,7 @@
<div class="card-body">
<h2>{{.Username}}'s list</h2>
{{with .Gifts}}
<form method="post" action="/{{$.UserReference}}/update" autocomplete="off"
<form method="post" autocomplete="off"
onchange="acceptNames(this, 'claimSubmit', 'claimed', 'unclaimed'); acceptNames(this, 'completeSubmit', 'claimed'); acceptAttribute(this, 'deleteSubmit', 'data-deletable')">
<ul class="list-group mb-3">
{{range .}}
@ -82,7 +82,7 @@
<p>They don't have any gift ideas. Ask them to think of something, or add an idea yourself! 👇 (everyone
except them will be able to see it and claim it)</p>
{{end}}
<form method="post" action="/{{$.UserReference}}/update">
<form method="post">
<div class="input-group mt-3">
<input class="form-control" name="gift_name"
placeholder="This will be invisible to {{.Username}}, but everyone else will be able to see it and possibly claim it."
@ -94,4 +94,4 @@
</section>
</div>
</div>
{{end}}
{{end}}

View File

@ -2,7 +2,7 @@
<div class="flex-grow-1"></div>
<ul class="navbar-nav">
<li class="nav-item"><button class="btn btn-success"
onclick="navigator.clipboard.writeText('{{.HostUrl}}/{{.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
onclick="navigator.clipboard.writeText('{{.HostUrl}}/list/{{.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>
<li class="nav-item">
<div class="dropdown">
@ -28,8 +28,7 @@
<div class="card-body">
<h2>Your wishlist</h2>
{{with .Gifts}}
<form method="post" action="/wishlist/delete" onchange="acceptNames(this, 'deleteSubmit', 'gift')"
autocomplete="off">
<form method="post" onchange="acceptNames(this, 'deleteSubmit', 'gift')" autocomplete="off">
<ul class="list-group mb-3">
{{range .}}
<li class="list-group-item">
@ -40,16 +39,16 @@
</li>
{{end}}
</ul>
<button id="deleteSubmit" class="btn btn-danger mb-3" type="submit" name="mode" value="delete"
<button id="deleteSubmit" class="btn btn-danger mb-3" type="submit" name="intent" value="delete_idea"
disabled>Delete</button>
</form>
{{else}}
<p>Your list is empty. Think of some things to add!</p>
{{end}}
<form method="post" action="/wishlist/add">
<form method="post">
<div class="input-group">
<input class="form-control" name="gift_name" required>
<button class="btn btn-primary" type="submit">Add gift idea</button>
<button class="btn btn-primary" type="submit" name="intent" value="add_idea">Add gift idea</button>
</div>
</form>
</div>
@ -59,7 +58,7 @@
<section class="card">
<div class="card-body">
<h2>Your todo list</h2>
<form method="post" action="/todo/update"
<form method="post"
onchange="acceptNames(this, 'unclaimSubmit', 'gift'); acceptNames(this, 'completeSubmit', 'gift')"
autocomplete="off">
<ul class="list-group mb-3">
@ -77,14 +76,14 @@
</em>
</label>
<span id="todo_detail_{{.Id}}">
for <a href="/{{.RecipientRef}}">{{.RecipientName}}</a>
for <a href="/list/{{.RecipientRef}}">{{.RecipientName}}</a>
</span>
</li>
{{end}}
</ul>
<button id="unclaimSubmit" class="btn btn-warning" type="submit" name="mode" value="unclaim"
<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="mode" value="complete"
<button id="completeSubmit" class="btn btn-success" type="submit" name="mode" value="complete_todo"
disabled>Complete</button>
</form>
</div>