Skip to content

Commit 7b6cf83

Browse files
Refactors live subscriptions
1 parent b121b79 commit 7b6cf83

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

lib/reddit_web/live/subscriptions_live.ex

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,17 @@ defmodule RedditWeb.SubscriptionsLive do
77
import Ecto.Query, only: [where: 2]
88

99
def mount(_params, %{"current_user" => u_id, "community" => c_id}, socket) do
10-
if Subscription |> where(user_id: ^u_id) |> where(community_id: ^c_id) |> Repo.all() !== [] do
11-
socket =
12-
assign(socket,
13-
user: u_id,
14-
community: c_id,
15-
status: "Leave Community",
16-
btn_status: "secondary",
17-
disable_with: "Leaving...")
10+
subscribed? = subscribed?(u_id, c_id)
1811

19-
{:ok , socket}
20-
else
21-
socket =
12+
socket =
2213
assign(socket,
2314
user: u_id,
2415
community: c_id,
25-
status: "Join Community",
26-
btn_status: "success",
27-
disable_with: "Joining...")
16+
status: get_status(subscribed?),
17+
btn_status: get_btn_status(subscribed?),
18+
disable_with: get_disable_with(subscribed?))
2819

29-
{:ok , socket}
30-
end
20+
{:ok , socket}
3121
end
3222

3323
def render(assigns) do
@@ -43,34 +33,41 @@ defmodule RedditWeb.SubscriptionsLive do
4333
end
4434

4535
def handle_event("toggle-status", %{"user" => u_id, "community" => c_id}, socket) do
46-
if Subscription |> where(user_id: ^u_id) |> where(community_id: ^c_id) |> Repo.all() !== [] do
47-
Subscription.delete_old_relationship(u_id, c_id)
36+
subscribed? = build_subscription(subscribed?(u_id, c_id), u_id, c_id)
4837

49-
socket =
38+
socket =
5039
assign(socket,
51-
user: u_id,
52-
community: c_id,
53-
status: "Join Community",
54-
btn_status: "success",
55-
disable_with: "Joining...")
40+
status: get_status(subscribed?),
41+
btn_status: get_btn_status(subscribed?),
42+
disable_with: get_disable_with(subscribed?))
5643

57-
:timer.sleep(500)
44+
:timer.sleep(500)
5845

59-
{:noreply , socket}
60-
else
61-
Subscription.build_relationship(u_id, c_id)
46+
{:noreply , socket}
47+
end
6248

63-
socket =
64-
assign(socket,
65-
user: u_id,
66-
community: c_id,
67-
status: "Leave Community",
68-
btn_status: "secondary",
69-
disable_with: "Leaving...")
49+
defp subscribed?(user, community) do
50+
Subscription
51+
|> where(user_id: ^user)
52+
|> where(community_id: ^community)
53+
|> Repo.all()
54+
end
7055

71-
:timer.sleep(500)
56+
defp build_subscription(subscribed?, user, community) when subscribed? == [] do
57+
Subscription.build_relationship(user, community)
7258

73-
{:noreply , socket}
74-
end
59+
subscribed?(user, community)
7560
end
61+
defp build_subscription(_subscribed?, user, community) do
62+
Subscription.delete_old_relationship(user, community)
63+
64+
subscribed?(user, community)
65+
end
66+
67+
defp get_status(subscribed?) when subscribed? == [], do: "Join Community"
68+
defp get_status(_subscribed?), do: "Leave Community"
69+
defp get_btn_status(subscribed?) when subscribed? == [], do: "success"
70+
defp get_btn_status(_subscribed?), do: "secondary"
71+
defp get_disable_with(subscribed?) when subscribed? == [], do: "Joining..."
72+
defp get_disable_with(_subscribed?), do: "Leaving..."
7673
end

0 commit comments

Comments
 (0)