htmx for user actions

This commit is contained in:
perennial
2024-10-04 21:41:35 +10:00
parent e7756ad6f5
commit 07aa3390d0
3 changed files with 34 additions and 6 deletions
+7 -3
View File
@@ -61,25 +61,29 @@
<!-- Bookmark and like buttons -->
<!-- TODO: there's probably a way to align these buttons along the top of the card *without breakage* -->
<div class="col-12 col-xl-4 d-flex justify-content-start justify-content-xl-end align-items-center">
<div id="bookmark-button">
{{- if isset(.BookmarkData) && LoggedIn }}
<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap me-2" onclick="window.location.href='/self/deleteBookmark/{{ .BookmarkID }}'">
<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap me-2" hx-post="/self/deleteBookmark/{{ .BookmarkID }}" hx-target="#bookmark-button" hx-swap="outerHTML">
<i class="bi bi-heart-fill me-2"></i>Bookmarked
</button>
{{- else }}
<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap me-2" onclick="window.location.href='/self/addBookmark/{{ .ID }}'">
<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap me-2" hx-post="/self/addBookmark/{{ .ID }}" hx-target="#bookmark-button" hx-swap="outerHTML">
<i class="bi bi-heart me-2"></i>Bookmark
</button>
{{- end }}
</div>
<div id="like-button">
{{- if .Liked && LoggedIn}}
<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap" disabled>
<i class="bi bi-hand-thumbs-up-fill me-2"></i>Liked
</button>
{{- else }}
<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap" onclick="window.location.href='/self/like/{{ .ID }}'">
<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap" hx-post="/self/like/{{ .ID }}" hx-target="#like-button" hx-swap="outerHTML">
<i class="bi bi-hand-thumbs-up me-2"></i>Like
</button>
{{- end }}
</div>
</div>
</div>
+3 -3
View File
@@ -113,9 +113,9 @@ func DefineRoutes() *mux.Router {
router.HandleFunc("/self", CatchError(routes.LoginUserPage)).Methods("GET")
router.HandleFunc("/self/followingWorks", CatchError(routes.FollowingWorksPage)).Methods("GET")
router.HandleFunc("/self/bookmarks", CatchError(routes.LoginBookmarkPage)).Methods("GET")
router.HandleFunc("/self/addBookmark/{id}", CatchError(routes.AddBookmarkRoute)).Methods("GET")
router.HandleFunc("/self/deleteBookmark/{id}", CatchError(routes.DeleteBookmarkRoute)).Methods("GET")
router.HandleFunc("/self/like/{id}", CatchError(routes.LikeRoute)).Methods("GET")
router.HandleFunc("/self/addBookmark/{id}", CatchError(routes.AddBookmarkRoute)).Methods("POST")
router.HandleFunc("/self/deleteBookmark/{id}", CatchError(routes.DeleteBookmarkRoute)).Methods("POST")
router.HandleFunc("/self/like/{id}", CatchError(routes.LikeRoute)).Methods("POST")
// oEmbed endpoint for embedding Pixiv content
router.HandleFunc("/oembed", CatchError(routes.Oembed)).Methods("GET")
+24
View File
@@ -34,6 +34,14 @@ func AddBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
return err
}
if r.Header.Get("HX-Request") == "true" {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap me-2" hx-post="/self/deleteBookmark/%s" hx-target="#bookmark-button" hx-swap="outerHTML">
<i class="bi bi-heart-fill me-2"></i>Bookmarked
</button>`, id)
return nil
}
utils.RedirectToWhenceYouCame(w, r)
return nil
}
@@ -58,6 +66,14 @@ func DeleteBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
return err
}
if r.Header.Get("HX-Request") == "true" {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap me-2" hx-post="/self/addBookmark/%s" hx-target="#bookmark-button" hx-swap="outerHTML">
<i class="bi bi-heart me-2"></i>Bookmark
</button>`, id)
return nil
}
utils.RedirectToWhenceYouCame(w, r)
return nil
}
@@ -81,6 +97,14 @@ func LikeRoute(w http.ResponseWriter, r *http.Request) error {
return err
}
if r.Header.Get("HX-Request") == "true" {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<button type="button" class="btn custom-btn-secondary bg-charcoal-surface2 text-nowrap" disabled>
<i class="bi bi-hand-thumbs-up-fill me-2"></i>Liked
</button>`)
return nil
}
utils.RedirectToWhenceYouCame(w, r)
return nil
}