Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modal closings when bainstorming changes #531

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mindwendel/local_storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Mindwendel.LocalStorage do
"name" => brainstorming.name,
"id" => brainstorming.id,
"admin_url_id" =>
if(Permissions.has_moderating_permission(brainstorming, user),
if(Permissions.has_moderating_permission(brainstorming.id, user),
do: brainstorming.admin_url_id,
else: nil
)
Expand Down
4 changes: 2 additions & 2 deletions lib/mindwendel/permissions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defmodule Mindwendel.Permissions do
@moduledoc """
The Permissions context.
"""
def has_moderating_permission(brainstorming, current_user) when current_user != nil do
Enum.member?(current_user.moderated_brainstormings |> Enum.map(& &1.id), brainstorming.id)
def has_moderating_permission(brainstorming_id, current_user) when current_user != nil do
Enum.member?(current_user.moderated_brainstormings |> Enum.map(& &1.id), brainstorming_id)
end

def has_moderating_permission(_, _) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
defmodule MindwendelWeb.BrainstormingLive.ShareComponent do
use MindwendelWeb, :live_component

alias Mindwendel.Permissions

def handle_event("toggle_url_secret", _value, socket) do
%{brainstorming: brainstorming, uri: uri, current_user: current_user} = socket.assigns

if has_moderating_permission(brainstorming, current_user) do
if Permissions.has_moderating_permission(brainstorming.id, current_user) do
new_uri = create_download_link(brainstorming, uri)
{:noreply, assign(socket, :uri, new_uri)}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</button>
</div>
<div class="input-group mt-3">
<%= if has_moderating_permission(@brainstorming, @current_user) do %>
<%= if has_moderating_permission(@brainstorming.id, @current_user) do %>
<.input
name="admin_url_id"
type="checkbox"
Expand Down
7 changes: 2 additions & 5 deletions lib/mindwendel_web/live/brainstorming_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,8 @@ defmodule MindwendelWeb.BrainstormingLive.Show do
# therefore we patch the url to reload it

{:noreply,
push_patch(
socket
|> assign(:brainstorming, brainstorming),
to: "/brainstormings/#{brainstorming.id}"
)}
socket
|> assign(:brainstorming, brainstorming)}
end

def handle_info({:user_updated, user}, socket) do
Expand Down
17 changes: 9 additions & 8 deletions lib/mindwendel_web/live/brainstorming_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
data-name={@brainstorming.name}
data-last-accessed-at={@brainstorming.last_accessed_at}
data-admin-url-id={
if has_moderating_permission(@brainstorming, @current_user),
if has_moderating_permission(@brainstorming.id, @current_user),
do: @brainstorming.admin_url_id,
else: nil
}
Expand All @@ -30,7 +30,7 @@
</div>

<div class="d-flex justify-content-end flex-wrap">
<%= if has_moderating_permission(@brainstorming, @current_user) do %>
<%= if has_moderating_permission(@brainstorming.id, @current_user) do %>
<.link
patch={~p"/brainstormings/#{@brainstorming.id}/new_lane"}
class="btn btn-primary m-1 d-inline-flex align-items-center"
Expand All @@ -48,7 +48,7 @@
<i class="bi-share-fill"></i>
</.link>

<%= if has_moderating_permission(@brainstorming, @current_user) do %>
<%= if has_moderating_permission(@brainstorming.id, @current_user) do %>
<.link
href={~p"/admin/brainstormings/#{@brainstorming.admin_url_id}/edit"}
class="btn btn-secondary m-1"
Expand Down Expand Up @@ -96,7 +96,7 @@
module={MindwendelWeb.IdeaLive.FormComponent}
id={:new}
action={:new}
brainstorming={@brainstorming}
brainstorming_id={@brainstorming.id}
filtered_labels={@filtered_labels}
current_user={@current_user}
idea={@idea}
Expand All @@ -114,7 +114,7 @@
module={MindwendelWeb.IdeaLive.ShowComponent}
id={:show}
action={:show}
brainstorming={@brainstorming}
brainstorming_id={@brainstorming.id}
current_user={@current_user}
idea={@idea}
/>
Expand All @@ -131,7 +131,7 @@
module={MindwendelWeb.LaneLive.FormComponent}
id={:new}
action={:new}
brainstorming={@brainstorming}
brainstorming_id={@brainstorming.id}
current_user={@current_user}
lane={@lane}
/>
Expand All @@ -148,7 +148,7 @@
module={MindwendelWeb.IdeaLive.FormComponent}
id={:update}
action={:update}
brainstorming={@brainstorming}
brainstorming_id={@brainstorming.id}
current_user={@current_user}
idea={@idea}
/>
Expand All @@ -165,7 +165,7 @@
module={MindwendelWeb.LaneLive.FormComponent}
id={:update}
action={:update}
brainstorming={@brainstorming}
brainstorming_id={@brainstorming.id}
current_user={@current_user}
lane={@lane}
/>
Expand All @@ -176,6 +176,7 @@
show
on_cancel={JS.patch(~p"/brainstormings/#{@brainstorming.id}")}
title={gettext("Share brainstorming")}
phx_update="ignore"
>
<.live_component
module={MindwendelWeb.BrainstormingLive.ShareComponent}
Expand Down
12 changes: 6 additions & 6 deletions lib/mindwendel_web/live/comment_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ defmodule MindwendelWeb.CommentLive.FormComponent do
def handle_event("close", _, socket) do
# The close button is either pressed inside the comment component, where a comment might be edited, or inside the "new comment" form.
# Depending on the location, either patch back to the brainstorming or simply change back to view mode inside the comment.
%{brainstorming: brainstorming, comment: comment} = socket.assigns
%{brainstorming_id: brainstorming_id, comment: comment} = socket.assigns

case socket.assigns.action do
:new ->
{:noreply,
push_patch(
socket
|> assign(:brainstorming, brainstorming),
to: "/brainstormings/#{brainstorming.id}"
socket,
to: "/brainstormings/#{brainstorming_id}"
)}

:update ->
Expand All @@ -67,9 +66,10 @@ defmodule MindwendelWeb.CommentLive.FormComponent do
end

defp save_comment(socket, :update, comment_params) do
%{current_user: current_user, comment: comment, brainstorming: brainstorming} = socket.assigns
%{current_user: current_user, comment: comment, brainstorming_id: brainstorming_id} =
socket.assigns

if has_moderating_or_ownership_permission(brainstorming, comment, current_user) do
if has_moderating_or_ownership_permission(brainstorming_id, comment, current_user) do
comment_params_merged =
comment_params
|> Map.put("user_id", comment.user_id || current_user.id)
Expand Down
5 changes: 3 additions & 2 deletions lib/mindwendel_web/live/comment_live/show_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ defmodule MindwendelWeb.CommentLive.ShowComponent do
end

def handle_event("delete_comment", _, socket) do
%{brainstorming: brainstorming, comment: comment, current_user: current_user} = socket.assigns
%{brainstorming_id: brainstorming_id, comment: comment, current_user: current_user} =
socket.assigns

if has_moderating_or_ownership_permission(brainstorming, comment, current_user) do
if has_moderating_or_ownership_permission(brainstorming_id, comment, current_user) do
Comments.delete_comment(socket.assigns.comment)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/mindwendel_web/live/comment_live/show_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<.live_component
module={MindwendelWeb.CommentLive.FormComponent}
id={"comment-form-#{@comment.id}"}
brainstorming={@brainstorming}
brainstorming_id={@brainstorming_id}
current_user={@current_user}
comment={@comment}
idea={@idea}
action={:update}
/>
<% else %>
<div class="card-title">
<%= if has_moderating_or_ownership_permission(@brainstorming, @comment, @current_user) do %>
<%= if has_moderating_or_ownership_permission(@brainstorming_id, @comment, @current_user) do %>
<.link
class="float-end ms-3 mb-3"
phx-click="delete_comment"
Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel_web/live/idea_live/card_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule MindwendelWeb.IdeaLive.CardComponent do

%{current_user: current_user, brainstorming: brainstorming} = socket.assigns

if has_moderating_or_ownership_permission(brainstorming, idea, current_user) do
if has_moderating_or_ownership_permission(brainstorming.id, idea, current_user) do
{:ok, _} = Ideas.delete_idea(idea)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel_web/live/idea_live/card_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
data-position={@idea.position_order}
>
<div class="card-body-mindwendel-idea">
<%= if has_moderating_or_ownership_permission(@brainstorming, @idea, @current_user) do %>
<%= if has_moderating_or_ownership_permission(@brainstorming.id, @idea, @current_user) do %>
<.link
class="float-end ms-3 mb-3"
phx-click="delete_idea"
Expand Down
10 changes: 5 additions & 5 deletions lib/mindwendel_web/live/idea_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ defmodule MindwendelWeb.IdeaLive.FormComponent do
end

def handle_event("delete_attachment", %{"id" => id}, socket) do
%{current_user: current_user, brainstorming: brainstorming, idea: idea} = socket.assigns
%{current_user: current_user, brainstorming_id: brainstorming_id, idea: idea} = socket.assigns

if has_moderating_or_ownership_permission(brainstorming, idea, current_user) do
if has_moderating_or_ownership_permission(brainstorming_id, idea, current_user) do
attachment = Attachments.get_attached_file(id)
Attachments.delete_attached_file(attachment)
end
Expand All @@ -57,9 +57,9 @@ defmodule MindwendelWeb.IdeaLive.FormComponent do
defp save_idea(socket, :update, idea_params) do
idea = Ideas.get_idea!(idea_params["id"])

%{current_user: current_user, brainstorming: brainstorming} = socket.assigns
%{current_user: current_user, brainstorming_id: brainstorming_id} = socket.assigns

if has_moderating_or_ownership_permission(brainstorming, idea, current_user) do
if has_moderating_or_ownership_permission(brainstorming_id, idea, current_user) do
tmp_attachments = prepare_attachments(socket)

idea_params_merged =
Expand All @@ -75,7 +75,7 @@ defmodule MindwendelWeb.IdeaLive.FormComponent do
{:noreply,
socket
|> put_flash(:info, gettext("Idea updated"))
|> push_patch(to: ~p"/brainstormings/#{brainstorming.id}")}
|> push_patch(to: ~p"/brainstormings/#{brainstorming_id}")}

{:error, %Ecto.Changeset{} = changeset} ->
remove_tmp_attachments(tmp_attachments)
Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel_web/live/idea_live/form_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<.input field={@form[:lane_id]} type="hidden" />
<:actions>
<.link
patch={~p"/brainstormings/#{@brainstorming.id}"}
patch={~p"/brainstormings/#{@brainstorming_id}"}
class="btn btn-secondary form-cancel me-2"
title={gettext("Close")}
>
Expand Down
4 changes: 2 additions & 2 deletions lib/mindwendel_web/live/idea_live/show_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
id={comment.id}
comment={comment}
idea={@idea}
brainstorming={@brainstorming}
brainstorming_id={@brainstorming_id}
current_user={@current_user}
live_action={:show}
/>
Expand All @@ -54,7 +54,7 @@
<.live_component
module={MindwendelWeb.CommentLive.FormComponent}
id={:new}
brainstorming={@brainstorming}
brainstorming_id={@brainstorming_id}
current_user={@current_user}
idea={@idea}
action={:new}
Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel_web/live/label_live/captions_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule MindwendelWeb.LabelLive.CaptionsComponent do
%{current_user: current_user, brainstorming: brainstorming, filtered_labels: filtered_labels} =
socket.assigns

if has_moderating_permission(brainstorming, current_user) do
if has_moderating_permission(brainstorming.id, current_user) do
# If the filter is already present, remove it as its toggled. If not, add it.
toggled_filters = build_filter_labels(filtered_labels, idea_label_id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
color={brainstorming_idea_label.color}
label_id={brainstorming_idea_label.id}
phx-target={@myself}
disabled={!has_moderating_permission(@brainstorming, @current_user)}
disabled={!has_moderating_permission(@brainstorming.id, @current_user)}
>
{brainstorming_idea_label.name}
</.filter_button>
<% end %>
<%= if has_moderating_permission(@brainstorming, @current_user) do %>
<%= if has_moderating_permission(@brainstorming.id, @current_user) do %>
<.filter_button
label_id="filter-label-reset"
color="grey"
Expand Down
13 changes: 7 additions & 6 deletions lib/mindwendel_web/live/lane_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule MindwendelWeb.LaneLive.FormComponent do
use MindwendelWeb, :live_component

alias Mindwendel.Lanes
alias Mindwendel.Permissions

@impl true
def update(%{lane: lane} = assigns, socket) do
Expand All @@ -24,9 +25,9 @@ defmodule MindwendelWeb.LaneLive.FormComponent do
end

def handle_event("save", %{"lane" => lane_params}, socket) do
%{current_user: current_user, brainstorming: brainstorming} = socket.assigns
%{current_user: current_user, brainstorming_id: brainstorming_id} = socket.assigns

if has_moderating_permission(brainstorming, current_user) do
if Permissions.has_moderating_permission(brainstorming_id, current_user) do
save_lane(socket, socket.assigns.action, lane_params)
else
{:noreply, socket}
Expand All @@ -36,31 +37,31 @@ defmodule MindwendelWeb.LaneLive.FormComponent do
defp save_lane(socket, :update, lane_params) do
lane = Lanes.get_lane!(lane_params["id"])

%{brainstorming: brainstorming} = socket.assigns
%{brainstorming_id: brainstorming_id} = socket.assigns

case Lanes.update_lane(lane, lane_params) do
{:ok, _lane} ->
{:noreply,
socket
|> put_flash(:info, gettext("Lane updated"))
|> push_event("submit-success", %{to: "#lane-modal"})
|> push_navigate(to: ~p"/brainstormings/#{brainstorming.id}")}
|> push_navigate(to: ~p"/brainstormings/#{brainstorming_id}")}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end

defp save_lane(socket, :new, lane_params) do
%{brainstorming: brainstorming} = socket.assigns
%{brainstorming_id: brainstorming_id} = socket.assigns

case Lanes.create_lane(lane_params) do
{:ok, _lane} ->
{:noreply,
socket
|> put_flash(:info, gettext("Lane created successfully"))
|> push_event("submit-success", %{to: "#lane-modal"})
|> push_navigate(to: ~p"/brainstormings/#{brainstorming.id}")}
|> push_navigate(to: ~p"/brainstormings/#{brainstorming_id}")}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel_web/live/lane_live/form_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<.input field={@form[:name]} type="text" label={gettext("Name")} phx-debounce={300} />
<:actions>
<.link
patch={~p"/brainstormings/#{@brainstorming.id}"}
patch={~p"/brainstormings/#{@brainstorming_id}"}
class="btn btn-secondary form-cancel me-2"
title={gettext("Close")}
>
Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel_web/live/lane_live/index_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule MindwendelWeb.LaneLive.IndexComponent do

%{current_user: current_user, brainstorming: brainstorming} = socket.assigns

if has_moderating_permission(brainstorming, current_user) do
if has_moderating_permission(brainstorming.id, current_user) do
{:ok, _} = Lanes.delete_lane(lane)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
else: ""

moderating_disabled =
unless has_moderating_permission(@brainstorming, @current_user),
unless has_moderating_permission(@brainstorming.id, @current_user),
do: "disabled",
else: "" %>
<.link
Expand Down
Loading
Loading