Skip to content

Commit e89ea39

Browse files
committed
Remove feed
1 parent d91981b commit e89ea39

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

lib/ex_rss_web/feed_live/index.ex

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule ExRssWeb.FeedLive.Index do
33

44
use ExRssWeb, :live_view
55

6-
alias ExRss.{Entry, Feed, Repo, User}
6+
alias ExRss.{Entry, Feed, Repo, User, FeedRemover}
77

88
def mount(
99
_params,
@@ -165,6 +165,53 @@ defmodule ExRssWeb.FeedLive.Index do
165165
update_feed_position(feed_id, nil, socket)
166166
end
167167

168+
def handle_event("remove_feed", %{"feed-id" => feed_id}, socket) do
169+
current_user = Repo.get!(User, socket.assigns.current_user.id)
170+
171+
multi =
172+
FeedRemover.remove_feed(current_user, %{"id" => feed_id})
173+
174+
case Repo.transaction(multi) do
175+
{:ok, %{feed: _}} ->
176+
feeds_of_current_user = current_user |> Ecto.assoc(:feeds)
177+
178+
feeds_with_counts =
179+
from(
180+
f in feeds_of_current_user,
181+
join: e in Entry,
182+
on: f.id == e.feed_id,
183+
group_by: f.id,
184+
order_by: [desc_nulls_last: f.position],
185+
select: %{
186+
f
187+
| unread_entries_count: filter(count(e.id), e.read == false),
188+
read_entries_count: filter(count(e.id), e.read == true),
189+
has_error: f.retries > 0
190+
}
191+
)
192+
193+
feeds =
194+
feeds_with_counts
195+
|> Repo.all()
196+
|> Repo.preload(
197+
entries: from(e in Entry, where: e.read == false, order_by: [desc: e.posted_at])
198+
)
199+
200+
oldest_unread_entry =
201+
User.oldest_unread_entry(socket.assigns.current_user.id)
202+
203+
socket =
204+
socket
205+
|> assign(:oldest_unread_entry, oldest_unread_entry)
206+
|> stream(:feeds, feeds, reset: true)
207+
208+
{:noreply, socket}
209+
210+
_ ->
211+
{:noreply, socket}
212+
end
213+
end
214+
168215
def update_feed_position(feed_id, position, socket) do
169216
changeset =
170217
Repo.get!(User, socket.assigns.current_user.id)

lib/ex_rss_web/feed_live/index.html.heex

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,40 @@
2626
</div>
2727

2828
<div class="md:shrink-0 flex self-start justify-end mt-1 ml-auto space-x-4">
29+
<div id={"remove-feed-#{feed.id}"} class="group">
30+
<button
31+
aria-label="Remove feed"
32+
class="group-data-[confirm-removal=true]:hidden"
33+
phx-click={
34+
JS.toggle_attribute({"data-confirm-removal", "true", "false"},
35+
to: "#remove-feed-#{feed.id}"
36+
)
37+
}
38+
>
39+
<.icon name="hero-trash-solid" />
40+
</button>
41+
42+
<button
43+
aria-label="Confirm removal of feed"
44+
class="hidden group-data-[confirm-removal=true]:inline-block"
45+
phx-click="remove_feed"
46+
phx-value-feed-id={feed.id}
47+
>
48+
<.icon name="hero-check-circle-solid" />
49+
</button>
50+
<button
51+
aria-label="Cancel removal of feed"
52+
class="hidden group-data-[confirm-removal=true]:inline-block"
53+
phx-click={
54+
JS.toggle_attribute({"data-confirm-removal", "true", "false"},
55+
to: "#remove-feed-#{feed.id}"
56+
)
57+
}
58+
>
59+
<.icon name="hero-x-circle-solid" />
60+
</button>
61+
</div>
62+
2963
<%= if is_nil(feed.position) do %>
3064
<button aria-label="Pin feed" phx-click="pin_feed" phx-value-feed-id={feed.id}>
3165
<.icon name="hero-bookmark-solid" />

0 commit comments

Comments
 (0)