-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor idea cards to live components (#475)
- Loading branch information
1 parent
afd6137
commit 4f0acd1
Showing
12 changed files
with
288 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule MindwendelWeb.IdeaLive.CardComponent do | ||
use MindwendelWeb, :live_component | ||
alias Mindwendel.Ideas | ||
alias Mindwendel.IdeaLabels | ||
alias Mindwendel.Likes | ||
|
||
@impl true | ||
def handle_event("delete_idea", %{"id" => id}, socket) do | ||
idea = Ideas.get_idea!(id) | ||
|
||
%{current_user: current_user, brainstorming: brainstorming} = socket.assigns | ||
|
||
if current_user.id in [idea.user_id | brainstorming.moderating_users |> Enum.map(& &1.id)] do | ||
{:ok, _} = Ideas.delete_idea(idea) | ||
end | ||
|
||
# broadcast will take care of the removal from the list | ||
{:noreply, socket} | ||
end | ||
|
||
def handle_event("like", %{"id" => id}, socket) do | ||
Likes.add_like(id, socket.assigns.current_user.id) | ||
|
||
{:noreply, socket} | ||
end | ||
|
||
def handle_event("unlike", %{"id" => id}, socket) do | ||
Likes.delete_like(id, socket.assigns.current_user.id) | ||
|
||
{:noreply, socket} | ||
end | ||
|
||
def handle_event( | ||
"add_idea_label_to_idea", | ||
%{ | ||
"idea-id" => idea_id, | ||
"idea-label-id" => idea_label_id | ||
}, | ||
socket | ||
) do | ||
idea = Ideas.get_idea!(idea_id) | ||
idea_label = IdeaLabels.get_idea_label(idea_label_id) | ||
|
||
case(IdeaLabels.add_idea_label_to_idea(idea, idea_label)) do | ||
{:ok, _idea} -> | ||
{:noreply, socket} | ||
|
||
{:error, _changeset} -> | ||
{:noreply, socket} | ||
end | ||
end | ||
|
||
def handle_event( | ||
"remove_idea_label_from_idea", | ||
%{ | ||
"idea-id" => idea_id, | ||
"idea-label-id" => idea_label_id | ||
}, | ||
socket | ||
) do | ||
idea = Ideas.get_idea!(idea_id) | ||
idea_label = IdeaLabels.get_idea_label(idea_label_id) | ||
|
||
IdeaLabels.remove_idea_label_from_idea(idea, idea_label) | ||
{:noreply, socket} | ||
end | ||
end |
152 changes: 152 additions & 0 deletions
152
lib/mindwendel_web/live/idea_live/card_component.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<div | ||
class={"card-mindwendel m-3 shadow-sm p-2 rounded IndexComponent__IdeaCard #{@width_class}"} | ||
role={if has_move_permission(@brainstorming, @current_user), do: "button", else: ""} | ||
data-testid={@idea.id} | ||
data-id={@idea.id} | ||
data-brainstorming-id={@idea.brainstorming_id} | ||
data-lane-id={@idea.lane_id} | ||
data-position={@idea.position_order} | ||
> | ||
<div class="card-body-mindwendel-idea"> | ||
<%= if has_moderating_or_ownership_permission(@brainstorming, @idea, @current_user) do %> | ||
<.link | ||
class="float-end ms-3 mb-3" | ||
phx-click="delete_idea" | ||
phx-target={@myself} | ||
phx-value-id={@idea.id} | ||
title={gettext("Delete idea")} | ||
data-confirm={gettext("Are you sure you want to delete this idea?")} | ||
> | ||
<i class="bi bi-x text-secondary"></i> | ||
</.link> | ||
<.link | ||
patch={~p"/brainstormings/#{@brainstorming.id}/ideas/#{@idea.id}/edit"} | ||
class="float-end ms-3 mb-3" | ||
title={gettext("Edit idea")} | ||
> | ||
<i class="bi bi-pencil text-secondary"></i> | ||
</.link> | ||
<% end %> | ||
<.link | ||
patch={~p"/brainstormings/#{@brainstorming.id}/ideas/#{@idea.id}"} | ||
class="float-end ms-3 mb-3" | ||
title={gettext("Show idea")} | ||
> | ||
<i class="bi bi-eye text-secondary"></i> | ||
</.link> | ||
|
||
<%= for idea_label <- Enum.sort_by(@idea.idea_labels, &(&1.position_order)) do %> | ||
<span | ||
id={"idea-label-#{uuid()}"} | ||
class="IndexComponent__IdeaLabelBadge mb-3" | ||
data-testid={idea_label.id} | ||
data-color={idea_label.color} | ||
phx-hook="SetIdeaLabelBackgroundColor" | ||
> | ||
<%= idea_label.name %> | ||
</span> | ||
<% end %> | ||
|
||
<%= unless @idea.link do %> | ||
<p class="card-body-mindwendel-idea-text"><%= raw(@idea.body) %></p> | ||
<% end %> | ||
|
||
<%= if @idea.link do %> | ||
<.link href={@idea.link.url}> | ||
<%= raw(@idea.body) %> | ||
</.link> | ||
<hr /> | ||
<div class="row"> | ||
<div class="col-md-3"> | ||
<img src={@idea.link.img_preview_url} class="preview-url" /> | ||
</div> | ||
<div class="col-md-9"> | ||
<p class="fw-bold"><%= @idea.link.title %></p> | ||
<p><%= @idea.link.description %></p> | ||
</div> | ||
</div> | ||
<% end %> | ||
|
||
<%= if length(@idea.files) > 0 do %> | ||
<%= for attachment <- @idea.files do %> | ||
<i class="bi bi-file-earmark"></i> | ||
<p class="text-muted"> | ||
<.link href={~p"/files/#{attachment.id}"} download={attachment.name}> | ||
<%= attachment.name || gettext("No filename") %> | ||
</.link> | ||
</p> | ||
<% end %> | ||
<% end %> | ||
</div> | ||
|
||
<div class="card-footer-mindwendel"> | ||
<div class="d-block"> | ||
<small class="text-muted"> | ||
<%= gettext("By") %> <%= Gettext.gettext(MindwendelWeb.Gettext, @idea.username) %> <%= Timex.format!( | ||
@idea.inserted_at, | ||
"{relative}", | ||
:relative | ||
) %> | ||
</small> | ||
</div> | ||
<div class="d-inline IndexComponent__IdeaLabelSection"> | ||
<%= for brainstorming_idea_label <- @brainstorming.labels do %> | ||
<%= unless Enum.find(@idea.idea_labels, fn idea_label -> idea_label.id == brainstorming_idea_label.id end) do %> | ||
<.link | ||
class="text-decoration-none me-1" | ||
data-testid={brainstorming_idea_label.id} | ||
phx-click="add_idea_label_to_idea" | ||
phx-target={@myself} | ||
phx-value-idea-id={@idea.id} | ||
title={"Label #{brainstorming_idea_label.name}"} | ||
phx-value-idea-label-id={brainstorming_idea_label.id} | ||
> | ||
<i | ||
id={"idea-label-#{uuid()}"} | ||
class="IndexComponent__IdeaLabel" | ||
data-testid={brainstorming_idea_label.id} | ||
data-color={brainstorming_idea_label.color} | ||
phx-hook="SetIdeaLabelColor" | ||
> | ||
</i> | ||
</.link> | ||
<% else %> | ||
<.link | ||
class="text-decoration-none me-1" | ||
data-testid={brainstorming_idea_label.id} | ||
phx-click="remove_idea_label_from_idea" | ||
phx-target={@myself} | ||
phx-value-idea-id={@idea.id} | ||
title={"Label #{brainstorming_idea_label.name}"} | ||
phx-value-idea-label-id={brainstorming_idea_label.id} | ||
> | ||
<i | ||
id={"idea-label-#{uuid()}"} | ||
class="IndexComponent__IdeaLabel--active" | ||
data-testid={brainstorming_idea_label.id} | ||
data-color={brainstorming_idea_label.color} | ||
phx-hook="SetIdeaLabelColor" | ||
> | ||
</i> | ||
</.link> | ||
<% end %> | ||
<% end %> | ||
</div> | ||
<div class="float-end ms-2"> | ||
<span> | ||
<%= @idea.comments_count %> | ||
<i class="bi-chat"></i> | ||
</span> | ||
<span class="me-1"><%= length(@idea.likes) %></span> | ||
<%= if Mindwendel.Likes.exists_user_in_likes?(@idea.likes, @current_user.id) do %> | ||
<.link phx-click="unlike" phx-target={@myself} phx-value-id={@idea.id} title="Unlike"> | ||
<i class="bi-arrow-up-circle-fill"></i> | ||
</.link> | ||
<% else %> | ||
<.link phx-click="like" phx-target={@myself} phx-value-id={@idea.id} title="Like"> | ||
<i class="bi-arrow-up-circle"></i> | ||
</.link> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> |
Oops, something went wrong.