Skip to content

Commit ad1da0f

Browse files
adds live join community
1 parent 5eedac1 commit ad1da0f

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed
Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,76 @@
11
defmodule RedditWeb.SubscriptionsLive do
22
use RedditWeb, :live_view
33

4-
def mount(_params, %{"current_user" => user_id}, socket) do
5-
socket =
4+
alias Reddit.Community.Subscription
5+
alias Reddit.Repo
6+
7+
import Ecto.Query, only: [where: 2]
8+
9+
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...")
18+
19+
{:ok , socket}
20+
else
21+
socket =
622
assign(socket,
7-
user: user_id,
23+
user: u_id,
24+
community: c_id,
825
status: "Join Community",
926
btn_status: "success",
1027
disable_with: "Joining...")
1128

12-
{:ok , socket}
29+
{:ok , socket}
30+
end
1331
end
1432

1533
def render(assigns) do
1634
~L"""
17-
<button class="btn btn-<%= @btn_status %> text-uppercase float-right" phx-click="toggle-status"
18-
phx-disable-with=<%= @disable_with %>>
35+
<button class="btn btn-<%= @btn_status %> text-uppercase float-right"
36+
phx-value-user="<%= @user %>"
37+
phx-value-community="<%= @community %>"
38+
phx-click="toggle-status"
39+
phx-disable-with=<%= @disable_with %>>
1940
<%= @status %>
2041
</button>
2142
"""
2243
end
2344

24-
def handle_event("toggle-status", _, socket) do
25-
socket =
45+
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)
48+
49+
socket =
50+
assign(socket,
51+
user: u_id,
52+
community: c_id,
53+
status: "Join Community",
54+
btn_status: "success",
55+
disable_with: "Joining...")
56+
57+
:timer.sleep(500)
58+
59+
{:noreply , socket}
60+
else
61+
Subscription.build_relationship(u_id, c_id)
62+
63+
socket =
2664
assign(socket,
65+
user: u_id,
66+
community: c_id,
2767
status: "Leave Community",
2868
btn_status: "secondary",
2969
disable_with: "Leaving...")
3070

31-
:timer.sleep(500)
71+
:timer.sleep(500)
3272

33-
{:noreply, socket}
73+
{:noreply , socket}
74+
end
3475
end
3576
end

lib/reddit_web/templates/community/show.html.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</div>
55
<div class="col-sm-4">
66
<%= if Pow.Plug.current_user(@conn) do %>
7-
<%= live_render(@conn, RedditWeb.SubscriptionsLive, session: %{"current_user" => @current_user.id}) %>
7+
<%= live_render(@conn, RedditWeb.SubscriptionsLive, session: %{"current_user" => @current_user.id, "community" => @community.id}) %>
88
<% end %>
99
</div>
1010
</div>

0 commit comments

Comments
 (0)