diff options
| author | Ben Sima <ben@bsima.me> | 2025-11-18 16:11:54 -0500 |
|---|---|---|
| committer | Ben Sima <ben@bsima.me> | 2025-11-18 16:11:54 -0500 |
| commit | 3e27d3f6c56da3451c15235a62e59073bc6a60fa (patch) | |
| tree | d942a8efebb83dbe316378cc9613185a2282b389 | |
| parent | 2bd0fcfb9a8dd7d8e1a7564bd8c747e7290ef626 (diff) | |
Add 'Add to my feed' button to public feed for logged-in users
When viewing the public feed (or any episode list), logged-in users
now see: - 'Add to my feed' button if episode is not in their feed -
'In your feed' button (disabled) if they already have it
Implementation: - Added user_button logic to EpisodeList component
- Checks Core.Database.user_has_episode() for each episode - Shows
blue outline button for add action - Shows gray disabled button when
already added - Buttons displayed in flex row with admin buttons
Updated add_episode_to_feed endpoint: - Changed to redirect back
to referer page after adding - Uses HX-Redirect to reload the page
showing updated state - This allows the button to change from 'Add
to my feed' to 'In your feed'
All tests passing (48 tests)
| -rw-r--r-- | Biz/PodcastItLater/Web.py | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/Biz/PodcastItLater/Web.py b/Biz/PodcastItLater/Web.py index ab46811..6d00649 100644 --- a/Biz/PodcastItLater/Web.py +++ b/Biz/PodcastItLater/Web.py @@ -570,6 +570,42 @@ class EpisodeList(Component[AnyChildren, EpisodeListAttrs]): ], ) + # "Add to my feed" button for logged-in users + user_button: html.div | html.button = html.div() + if user: + # Check if user already has this episode + user_has_episode = Core.Database.user_has_episode( + user["id"], + episode["id"], + ) + if user_has_episode: + user_button = html.button( + html.i(classes=["bi", "bi-check-circle-fill", "me-1"]), + "In your feed", + disabled=True, + classes=[ + "btn", + "btn-sm", + "btn-secondary", + "mt-2", + "ms-2", + ], + ) + else: + user_button = html.button( + html.i(classes=["bi", "bi-plus-circle", "me-1"]), + "Add to my feed", + hx_post=f"/episode/{episode['id']}/add-to-feed", + hx_swap="none", + classes=[ + "btn", + "btn-sm", + "btn-outline-primary", + "mt-2", + "ms-2", + ], + ) + episode_items.append( html.div( html.div( @@ -618,8 +654,12 @@ class EpisodeList(Component[AnyChildren, EpisodeListAttrs]): ) if episode.get("original_url") else html.div(), - # Admin button to add/remove from public feed - admin_button, + # Buttons row (admin and user buttons) + html.div( + admin_button, + user_button, + classes=["d-flex", "flex-wrap"], + ), classes=["card-body"], ), classes=["card", "mb-3"], @@ -1707,13 +1747,13 @@ def add_episode_to_feed(request: Request, episode_id: int) -> Response: # Track the "added" event Core.Database.track_episode_metric(episode_id, "added", user_id) + # Reload the current page to show updated button state + # Check referer to determine where to redirect + referer = request.headers.get("referer", "/") return Response( - '<div class="alert alert-success">' - '<i class="bi bi-check-circle me-2"></i>' - "Added to your feed! " - '<a href="/" class="alert-link">View your feed</a>' - "</div>", + "", status_code=200, + headers={"HX-Redirect": referer}, ) |
