summaryrefslogtreecommitdiff
path: root/lib/aggiedit_web/live
diff options
context:
space:
mode:
authorLogan Hunt <loganhunt@simponic.xyz>2022-04-15 13:00:42 -0600
committerLogan Hunt <loganhunt@simponic.xyz>2022-04-15 13:00:42 -0600
commit3cf9f4a364ac91cca30799c8379a682139425e71 (patch)
treedb94f64634e0a840b0a5d1eeef43460ef4e8dd21 /lib/aggiedit_web/live
parentdb7c2321cd0af59f9e810e84c7d4eb83ec416458 (diff)
downloadaggiedit-3cf9f4a364ac91cca30799c8379a682139425e71.tar.gz
aggiedit-3cf9f4a364ac91cca30799c8379a682139425e71.zip
Add comments and vote models; pub/sub voting on posts
Diffstat (limited to 'lib/aggiedit_web/live')
-rw-r--r--lib/aggiedit_web/live/post_live/index.ex21
-rw-r--r--lib/aggiedit_web/live/post_live/index.html.heex26
2 files changed, 40 insertions, 7 deletions
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 @@
<div id="posts" phx-update="prepend">
<%= for post <- @posts do %>
<div id={"post-#{post.id}"} class="card d-flex flex-row align-items-center p-2 m-2 shadow">
- <%= if !is_nil(post.upload) do %>
- <%= live_redirect to: Routes.post_show_path(@socket, :show, @room, post) do %>
- <div class="card-image d-flex justify-content-center" style="width: 100px">
+ <div class="d-flex flex-column m-2">
+ <%=
+ has_vote = Map.has_key?(@votes, post.id)
+ is_upvote = has_vote && @votes[post.id].is_up
+ ""
+ %>
+ <div class="d-flex">
+ <span><%= link "", to: "#", phx_click: "upvote", phx_value_id: post.id, class: "bi bi-arrow-up-circle#{if has_vote && is_upvote, do: "-fill", else: ""}" %></span>
+ </div>
+ <div class="d-flex">
+ <%= post.score %>
+ </div>
+ <div class="d-flex">
+ <span><%= link "", to: "#", phx_click: "downvote", phx_value_id: post.id, class: "bi bi-arrow-down-circle#{if has_vote && !is_upvote, do: "-fill", else: ""}" %></span>
+ </div>
+ </div>
+ <div class="m-2 card-image d-flex justify-content-center" style="width: 100px">
+ <%= if !is_nil(post.upload) do %>
+ <%= live_redirect to: Routes.post_show_path(@socket, :show, @room, post) do %>
<img class="fluid-img thumbnail" src={Routes.static_path(@socket, "/uploads/#{post.upload.file}")} />
- </div>
+ <% end %>
<% end %>
- <% end %>
+ </div>
<div class="card-body">
<%= live_redirect to: Routes.post_show_path(@socket, :show, @room, post) do %>
<h4 class="card-title"><%= post.title %></h4>