Skip to content

Commit

Permalink
Fix pinned messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alxlion committed Nov 24, 2023
1 parent 90311d3 commit 3958e80
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 37 deletions.
49 changes: 17 additions & 32 deletions lib/claper_web/live/event_live/manage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule ClaperWeb.EventLive.Manage do
|> assign(:state, event.presentation_file.presentation_state)
|> assign(:pinned_posts, list_pinned_posts(socket, event.uuid))
|> assign(:all_posts, list_all_posts(socket, event.uuid))
|> assign(:pinned_post_count, length(list_pinned_posts(socket, event.uuid)))
|> assign(:polls, list_polls(socket, event.presentation_file.id))
|> assign(:forms, list_forms(socket, event.presentation_file.id))
|> assign(:embeds, list_embeds(socket, event.presentation_file.id))
Expand All @@ -57,20 +58,10 @@ defmodule ClaperWeb.EventLive.Manage do
|> form_at_position(false)
|> embed_at_position(false)

{:ok, socket, temporary_assigns: [posts: [], form_submits: []]}
{:ok, socket, temporary_assigns: [all_posts: [], pinned_posts: [], form_submits: []]}
end
end

defp delete_post_from_list(posts, deleted_post) do
Enum.reject(posts, fn post -> post.id == deleted_post.id end)
end

defp update_post_in_list(posts, updated_post) do
Enum.map(posts, fn post ->
if post.id == updated_post.id, do: updated_post, else: post
end)
end

defp is_leader(%{assigns: %{current_user: current_user}} = _socket, event) do
Claper.Events.is_leaded_by(current_user.email, event) || event.user.id == current_user.id
end
Expand All @@ -93,42 +84,41 @@ defmodule ClaperWeb.EventLive.Manage do

@impl true
def handle_info({:post_updated, updated_post}, socket) do
updated_posts = update_post_in_list(socket.assigns.all_posts, updated_post)
updated_pinned_posts = update_post_in_list(socket.assigns.pinned_posts, updated_post)

{:noreply,
socket
|> assign(:all_posts, updated_posts)
|> assign(:pinned_posts, updated_pinned_posts)}
|> update(:all_posts, fn posts -> [updated_post | posts] end)
|> update(:pinned_posts, fn posts -> [updated_post | posts] end)
}
end

@impl true
def handle_info({:post_deleted, deleted_post}, socket) do
{:noreply,
socket
|> update(:all_posts, fn posts -> [deleted_post | posts] end)
|> update(:pinned_posts, fn pinned_posts ->
delete_post_from_list(pinned_posts, deleted_post)
end)}
|> update(:pinned_posts, fn posts -> [deleted_post | posts] end)
|> update(:pinned_post_count, fn pinned_post_count -> pinned_post_count - if deleted_post.pinned, do: 1, else: 0 end)
}
end

@impl true
def handle_info({:post_pinned, _post}, socket) do
def handle_info({:post_pinned, post}, socket) do
updated_socket =
socket
|> update(:all_posts, fn _all_posts -> socket.assigns.all_posts end)
|> update(:pinned_posts, fn _pinned_posts -> socket.assigns.pinned_posts end)
|> update(:all_posts, fn all_posts -> [post | all_posts] end)
|> update(:pinned_posts, fn pinned_posts -> [post | pinned_posts] end)
|> assign(:pinned_post_count, socket.assigns.pinned_post_count + 1)

{:noreply, updated_socket}
end

@impl true
def handle_info({:post_unpinned, _post}, socket) do
def handle_info({:post_unpinned, post}, socket) do
updated_socket =
socket
|> update(:all_posts, fn _all_posts -> socket.assigns.all_posts end)
|> update(:pinned_posts, fn _pinned_posts -> socket.assigns.pinned_posts end)

|> update(:all_posts, fn all_posts -> [post | all_posts] end)
|> update(:pinned_posts, fn pinned_posts -> [post | pinned_posts] end)
|> assign(:pinned_post_count, socket.assigns.pinned_post_count - 1)
{:noreply, updated_socket}
end

Expand Down Expand Up @@ -737,12 +727,7 @@ defmodule ClaperWeb.EventLive.Manage do
defp pin(post, socket) do
{:ok, _updated_post} = Claper.Posts.toggle_pin_post(post)

updated_socket =
socket
|> assign(:all_posts, list_all_posts(socket, socket.assigns.event.uuid))
|> assign(:pinned_posts, list_pinned_posts(socket, socket.assigns.event.uuid))

{:noreply, updated_socket}
{:noreply, socket}
end

defp embed_at_position(
Expand Down
8 changes: 4 additions & 4 deletions lib/claper_web/live/event_live/manage.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -704,14 +704,14 @@

<div class="grid grid-cols-1 grid-rows-2 md:grid-rows-3" style="height: 100%;">
<div class="bg-gray-200 md:row-span-2 border-2">
<ul class="fixed z-20 flex items-center bg-gray-200 space-x-3 px-2 w-full py-2">
<ul id="menu" phx-update="replace" class="fixed z-20 flex items-center bg-gray-200 space-x-3 px-2 w-full py-2">
<li class={"rounded-md #{if @list_tab==:posts, do: 'bg-secondary-600 text-white' ,
else: 'bg-white text-gray-600' } px-2 py-0.5 text-sm shadow-sm"}>
<%= link(gettext("Messages"), to: "#", phx_click: "list-tab", phx_value_tab: :posts) %>
</li>
<li class={"rounded-md #{if @list_tab==:pinned_posts, do: 'bg-secondary-600 text-white' ,
else: 'bg-white text-gray-600' } px-2 py-0.5 text-sm shadow-sm"}>
<%= link(gettext("Pinned messages") <> " (#{length(@pinned_posts)})",
<%= link(gettext("Pinned messages") <> " (#{@pinned_post_count})",
to: "#",
phx_click: "list-tab",
phx_value_tab: :pinned_posts
Expand Down Expand Up @@ -935,14 +935,14 @@
<div
id="pinned-post-list"
class="overflow-y-auto pb-5 pt-8 px-5"
phx-update="replace"
phx-update="append"
data-posts-nb={Enum.count(@pinned_posts)}
phx-hook="ScrollIntoDiv"
data-target="#pinned-post-list"
>
<%= for post <- @pinned_posts do %>
<div
class={if post.__meta__.state == :deleted, do: "hidden"}
class={if post.__meta__.state == :deleted || !post.pinned, do: "hidden"}
id={"#{post.id}-post"}
>
<div class="px-4 pb-2 pt-3 rounded-b-lg rounded-tr-lg bg-white relative shadow-md text-black break-all mt-4">
Expand Down
2 changes: 1 addition & 1 deletion lib/claper_web/live/event_live/post_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule ClaperWeb.EventLive.PostComponent do
<img src="/images/icons/ellipsis-horizontal-white.svg" class="h-5" />
</button>
<%= if @post.name || is_a_leader(@post, @event, @leaders) do %>
<%= if @post.name || is_a_leader(@post, @event, @leaders) || is_pinned(@post) do %>
<div class="inline-flex items-center">
<%= if @post.name do %>
<p class="text-white text-xs font-semibold mb-2 mr-2"><%= @post.name %></p>
Expand Down

0 comments on commit 3958e80

Please sign in to comment.