fix: remove watchlist item with htmx delete swap

This commit is contained in:
2026-04-06 19:48:46 +02:00
parent 5effd901c3
commit da60c9d6fd
3 changed files with 22 additions and 13 deletions

View File

@@ -101,14 +101,15 @@ func (h *WatchlistHandler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.
return
}
animeIDStr := r.URL.Path[len("/api/watchlist/"):]
animeID, err := strconv.ParseInt(animeIDStr, 10, 64)
// Parse the path to get anime ID (path is /api/watchlist/{id} possibly with query params)
path := r.URL.Path[len("/api/watchlist/"):]
animeID, err := strconv.ParseInt(path, 10, 64)
if err != nil {
http.Error(w, "invalid anime ID", http.StatusBadRequest)
return
}
// Get anime info before deleting
// Get anime info before deleting (for dropdown refresh on anime page)
anime, err := h.db.GetAnime(r.Context(), animeID)
if err != nil {
http.Error(w, "anime not found", http.StatusNotFound)
@@ -125,6 +126,14 @@ func (h *WatchlistHandler) HandleDeleteWatchlist(w http.ResponseWriter, r *http.
}
w.Header().Set("HX-Trigger", `{"toast": "removed from watchlist"}`)
// If called from watchlist page, just return empty (hx-swap="delete" handles removal)
if r.URL.Query().Get("from") == "watchlist" {
w.WriteHeader(http.StatusOK)
return
}
// Otherwise return updated dropdown for anime page
templates.WatchlistDropdown(int(animeID), anime.Title, anime.ImageUrl, "").Render(r.Context(), w)
}

View File

@@ -58,9 +58,9 @@ templ Watchlist(entries []database.GetUserWatchListRow, layout string, currentSt
<div class="watchlist-status">{ entry.Status }</div>
<button
class="remove-btn"
hx-delete={ string(templ.URL(fmt.Sprintf("/api/watchlist/%d", entry.AnimeID))) }
hx-delete={ string(templ.URL(fmt.Sprintf("/api/watchlist/%d?from=watchlist", entry.AnimeID))) }
hx-target={ fmt.Sprintf("#watchlist-entry-%d", entry.AnimeID) }
hx-swap="outerHTML"
hx-swap="delete"
>&times;</button>
</div>
}
@@ -92,9 +92,9 @@ templ Watchlist(entries []database.GetUserWatchListRow, layout string, currentSt
<td class="actions-cell">
<button
class="remove-link"
hx-delete={ string(templ.URL(fmt.Sprintf("/api/watchlist/%d", entry.AnimeID))) }
hx-delete={ string(templ.URL(fmt.Sprintf("/api/watchlist/%d?from=watchlist", entry.AnimeID))) }
hx-target={ fmt.Sprintf("#watchlist-entry-%d", entry.AnimeID) }
hx-swap="outerHTML"
hx-swap="delete"
style="background: none; border: none; cursor: pointer;"
>remove</button>
</td>

View File

@@ -455,9 +455,9 @@ func Watchlist(entries []database.GetUserWatchListRow, layout string, currentSta
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var33 string
templ_7745c5c3_Var33, templ_7745c5c3_Err = templ.JoinStringErrs(string(templ.URL(fmt.Sprintf("/api/watchlist/%d", entry.AnimeID))))
templ_7745c5c3_Var33, templ_7745c5c3_Err = templ.JoinStringErrs(string(templ.URL(fmt.Sprintf("/api/watchlist/%d?from=watchlist", entry.AnimeID))))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/watchlist.templ`, Line: 61, Col: 86}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/watchlist.templ`, Line: 61, Col: 101}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var33))
if templ_7745c5c3_Err != nil {
@@ -476,7 +476,7 @@ func Watchlist(entries []database.GetUserWatchListRow, layout string, currentSta
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 42, "\" hx-swap=\"outerHTML\">&times;</button></div>")
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 42, "\" hx-swap=\"delete\">&times;</button></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@@ -587,9 +587,9 @@ func Watchlist(entries []database.GetUserWatchListRow, layout string, currentSta
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var42 string
templ_7745c5c3_Var42, templ_7745c5c3_Err = templ.JoinStringErrs(string(templ.URL(fmt.Sprintf("/api/watchlist/%d", entry.AnimeID))))
templ_7745c5c3_Var42, templ_7745c5c3_Err = templ.JoinStringErrs(string(templ.URL(fmt.Sprintf("/api/watchlist/%d?from=watchlist", entry.AnimeID))))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/watchlist.templ`, Line: 95, Col: 88}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/watchlist.templ`, Line: 95, Col: 103}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var42))
if templ_7745c5c3_Err != nil {
@@ -608,7 +608,7 @@ func Watchlist(entries []database.GetUserWatchListRow, layout string, currentSta
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 54, "\" hx-swap=\"outerHTML\" style=\"background: none; border: none; cursor: pointer;\">remove</button></td></tr>")
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 54, "\" hx-swap=\"delete\" style=\"background: none; border: none; cursor: pointer;\">remove</button></td></tr>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}