From 3cf9f4a364ac91cca30799c8379a682139425e71 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Fri, 15 Apr 2022 13:00:42 -0600 Subject: Add comments and vote models; pub/sub voting on posts --- lib/aggiedit_web/live/post_live/index.ex | 21 ++++++++++++++++++-- lib/aggiedit_web/live/post_live/index.html.heex | 26 ++++++++++++++++++++----- 2 files changed, 40 insertions(+), 7 deletions(-) (limited to 'lib/aggiedit_web/live/post_live') diff --git a/lib/aggiedit_web/live/post_live/index.ex b/lib/aggiedit_web/live/post_live/index.ex index 59ec234..d231ea0 100644 --- a/lib/aggiedit_web/live/post_live/index.ex +++ b/lib/aggiedit_web/live/post_live/index.ex @@ -12,7 +12,14 @@ defmodule AggieditWeb.PostLive.Index do case socket.assigns do %{:room => room} -> if connected?(socket), do: Rooms.subscribe(socket.assigns.room) - {:ok, assign(socket, %{:posts => room |> Repo.preload(posts: [:user, :upload]) |> Map.get(:posts)}), temporary_assigns: [posts: []]} + posts = room + |> Repo.preload(posts: [:user, :upload]) + |> Map.get(:posts) + votes = socket.assigns.current_user + |> Repo.preload(:votes) + |> Map.get(:votes) + |> Enum.reduce(%{}, fn v, a -> Map.put(a, v.post_id, v) end) + {:ok, assign(socket, %{:posts => posts, :votes => votes}), temporary_assigns: [posts: []]} _ -> {:ok, socket} end end @@ -50,6 +57,16 @@ defmodule AggieditWeb.PostLive.Index do |> assign(:post, nil) end + def handle_event(vote, %{"id" => id}, socket) when vote in ["upvote", "downvote"] do + post = Rooms.get_post!(id) + if Roles.guard?(socket.assigns.current_user, :vote, post) do + Rooms.vote_post(post, socket.assigns.current_user, vote) + {:noreply, socket} + else + {:noreply, socket |> put_flash(:error, "You don't have permission to do that.") |> redirect(to: Routes.post_show_path(socket, :show, socket.assigns.room, post))} + end + end + @impl true def handle_event("delete", %{"id" => id}, socket) do post = Rooms.get_post!(id) @@ -62,7 +79,7 @@ defmodule AggieditWeb.PostLive.Index do end @impl true - def handle_info({action, post}, socket) when action in [:post_created, :post_updated, :post_deleted] do + def handle_info({action, post}, socket) when action in [:post_created, :post_updated, :post_deleted, :post_voted] do {:noreply, update(socket, :posts, fn posts -> [posts | post] end)} diff --git a/lib/aggiedit_web/live/post_live/index.html.heex b/lib/aggiedit_web/live/post_live/index.html.heex index efb42cb..89767f8 100644 --- a/lib/aggiedit_web/live/post_live/index.html.heex +++ b/lib/aggiedit_web/live/post_live/index.html.heex @@ -18,13 +18,29 @@
<%= for post <- @posts do %>
- <%= if !is_nil(post.upload) do %> - <%= live_redirect to: Routes.post_show_path(@socket, :show, @room, post) do %> -
+
+ <%= + has_vote = Map.has_key?(@votes, post.id) + is_upvote = has_vote && @votes[post.id].is_up + "" + %> +
+ <%= link "", to: "#", phx_click: "upvote", phx_value_id: post.id, class: "bi bi-arrow-up-circle#{if has_vote && is_upvote, do: "-fill", else: ""}" %> +
+
+ <%= post.score %> +
+
+ <%= link "", to: "#", phx_click: "downvote", phx_value_id: post.id, class: "bi bi-arrow-down-circle#{if has_vote && !is_upvote, do: "-fill", else: ""}" %> +
+
+
+ <%= if !is_nil(post.upload) do %> + <%= live_redirect to: Routes.post_show_path(@socket, :show, @room, post) do %> -
+ <% end %> <% end %> - <% end %> +
<%= live_redirect to: Routes.post_show_path(@socket, :show, @room, post) do %>

<%= post.title %>

-- cgit v1.2.3-70-g09d2