From fd435d14d8705bf6cc883d533e6edd8f71a8ff10 Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Thu, 19 Dec 2024 08:31:17 +0100 Subject: [PATCH 1/4] rework filter to exclude brainstorming broadcast --- lib/mindwendel/brainstormings.ex | 15 ++++-------- .../live/brainstorming_live/show.ex | 7 +++--- .../live/brainstorming_live/show.html.heex | 1 + .../live/label_live/captions_component.ex | 23 ++++++++++++------- .../label_live/captions_component.html.heex | 2 +- test/mindwendel/brainstormings_test.exs | 17 ++++++++++++++ 6 files changed, 43 insertions(+), 22 deletions(-) diff --git a/lib/mindwendel/brainstormings.ex b/lib/mindwendel/brainstormings.ex index be5dd12a..53163cc3 100644 --- a/lib/mindwendel/brainstormings.ex +++ b/lib/mindwendel/brainstormings.ex @@ -93,7 +93,8 @@ defmodule Mindwendel.Brainstormings do :moderating_users, labels: from(idea_label in IdeaLabel, order_by: idea_label.position_order) ]) - |> update_last_accessed_at() + + # |> update_last_accessed_at() {:ok, preloaded_brainstorming} end @@ -156,12 +157,12 @@ defmodule Mindwendel.Brainstormings do ) end - updated_brainstorming = + updated_brainstorming_result = brainstorming |> Brainstorming.changeset(attrs) |> Repo.update() - broadcast(updated_brainstorming, :brainstorming_filter_updated) + broadcast(updated_brainstorming_result, :brainstorming_filter_updated) end def update_brainstorming(%Brainstorming{} = brainstorming, attrs) do @@ -292,13 +293,7 @@ defmodule Mindwendel.Brainstormings do Phoenix.PubSub.broadcast( Mindwendel.PubSub, "brainstormings:" <> brainstorming.id, - {event, - brainstorming - |> Repo.preload([ - :users, - :moderating_users, - labels: from(idea_label in IdeaLabel, order_by: idea_label.position_order) - ]), lanes} + {event, brainstorming.filter_labels_ids, lanes} ) {:ok, brainstorming} diff --git a/lib/mindwendel_web/live/brainstorming_live/show.ex b/lib/mindwendel_web/live/brainstorming_live/show.ex index 89fd2184..c514416e 100644 --- a/lib/mindwendel_web/live/brainstorming_live/show.ex +++ b/lib/mindwendel_web/live/brainstorming_live/show.ex @@ -38,6 +38,7 @@ defmodule MindwendelWeb.BrainstormingLive.Show do |> assign(:current_view, socket.view) |> assign(:brainstorming, brainstorming) |> assign(:lanes, lanes) + |> assign(:filtered_labels, brainstorming.filter_labels_ids) |> assign(:current_user, current_user) |> assign(:inspiration, Mindwendel.Help.random_inspiration()) } @@ -120,13 +121,13 @@ defmodule MindwendelWeb.BrainstormingLive.Show do {:noreply, socket} end - def handle_info({:brainstorming_filter_updated, brainstorming, lanes}, socket) do + def handle_info({:brainstorming_filter_updated, filtered_labels, lanes}, socket) do {:noreply, push_patch( socket - |> assign(:brainstorming, brainstorming) + |> assign(:filtered_labels, filtered_labels) |> assign(:lanes, lanes), - to: "/brainstormings/#{brainstorming.id}" + to: "/brainstormings/#{socket.assigns.brainstorming.id}" )} end diff --git a/lib/mindwendel_web/live/brainstorming_live/show.html.heex b/lib/mindwendel_web/live/brainstorming_live/show.html.heex index 0fb0b848..87112ed8 100644 --- a/lib/mindwendel_web/live/brainstorming_live/show.html.heex +++ b/lib/mindwendel_web/live/brainstorming_live/show.html.heex @@ -72,6 +72,7 @@ module={MindwendelWeb.LabelLive.CaptionsComponent} id="captions" brainstorming={@brainstorming} + filtered_labels={@filtered_labels} current_user={@current_user} /> diff --git a/lib/mindwendel_web/live/label_live/captions_component.ex b/lib/mindwendel_web/live/label_live/captions_component.ex index ba471259..26c5f84d 100644 --- a/lib/mindwendel_web/live/label_live/captions_component.ex +++ b/lib/mindwendel_web/live/label_live/captions_component.ex @@ -4,27 +4,34 @@ defmodule MindwendelWeb.LabelLive.CaptionsComponent do alias Mindwendel.Brainstormings def handle_event("set_filter_idea_label", %{"id" => idea_label_id}, socket) do - %{current_user: current_user, brainstorming: brainstorming} = socket.assigns + %{current_user: current_user, brainstorming: brainstorming, filtered_labels: filtered_labels} = + socket.assigns if has_moderating_permission(brainstorming, current_user) do # If the filter is already present, remove it as its toggled. If not, add it. - toggled_filters = build_filter_labels(brainstorming, idea_label_id) - Brainstormings.update_brainstorming(brainstorming, %{filter_labels_ids: toggled_filters}) + toggled_filters = build_filter_labels(filtered_labels, idea_label_id) + + # Brainstorming in socket might be outdated in terms of filter_labels_ids as we dont update the socket on every change. + {:ok, refreshed_brainstorming} = Brainstormings.get_brainstorming(brainstorming.id) + + Brainstormings.update_brainstorming(refreshed_brainstorming, %{ + filter_labels_ids: toggled_filters + }) end {:noreply, socket} end - defp build_filter_labels(_brainstorming, "filter-label-reset" = _idea_label_id) do + defp build_filter_labels(_filtered_labels, "filter-label-reset" = _idea_label_id) do [] end - defp build_filter_labels(brainstorming, idea_label_id) do - if Enum.member?(brainstorming.filter_labels_ids, idea_label_id), + defp build_filter_labels(filtered_labels, idea_label_id) do + if Enum.member?(filtered_labels, idea_label_id), do: - Enum.filter(brainstorming.filter_labels_ids, fn filter_id -> + Enum.filter(filtered_labels, fn filter_id -> filter_id != idea_label_id end), - else: [idea_label_id | brainstorming.filter_labels_ids] + else: [idea_label_id | filtered_labels] end end diff --git a/lib/mindwendel_web/live/label_live/captions_component.html.heex b/lib/mindwendel_web/live/label_live/captions_component.html.heex index 66e04264..56b626f1 100644 --- a/lib/mindwendel_web/live/label_live/captions_component.html.heex +++ b/lib/mindwendel_web/live/label_live/captions_component.html.heex @@ -1,6 +1,6 @@
<%= for brainstorming_idea_label <- @brainstorming.labels do %> - <% filter_active = Enum.member?(@brainstorming.filter_labels_ids, brainstorming_idea_label.id) %> + <% filter_active = Enum.member?(@filtered_labels, brainstorming_idea_label.id) %> <.filter_button filter_active={filter_active} id={"idea-caption-#{uuid()}"} diff --git a/test/mindwendel/brainstormings_test.exs b/test/mindwendel/brainstormings_test.exs index 42e336f8..8516a25a 100644 --- a/test/mindwendel/brainstormings_test.exs +++ b/test/mindwendel/brainstormings_test.exs @@ -27,6 +27,23 @@ defmodule Mindwendel.BrainstormingsTest do } end + describe "update_brainstorming" do + test "updates the brainstorming with filter_labels_ids", %{brainstorming: brainstorming} do + filter_label = Enum.at(brainstorming.labels, 0) + Brainstormings.update_brainstorming(brainstorming, %{filter_labels_ids: [filter_label.id]}) + {:ok, reloaded_brainstorming} = Brainstormings.get_brainstorming(brainstorming.id) + assert reloaded_brainstorming.filter_labels_ids == [filter_label.id] + end + + test "updates the brainstorming with empty filter_labels_ids", %{brainstorming: brainstorming} do + filter_label = Enum.at(brainstorming.labels, 0) + Brainstormings.update_brainstorming(brainstorming, %{filter_labels_ids: [filter_label.id]}) + Brainstormings.update_brainstorming(brainstorming, %{filter_labels_ids: []}) + {:ok, reloaded_brainstorming} = Brainstormings.get_brainstorming(brainstorming.id) + assert reloaded_brainstorming.filter_labels_ids == [] + end + end + describe "get_brainstorming" do test "returns the brainstorming", %{brainstorming: brainstorming} do {:ok, loaded_brainstorming} = Brainstormings.get_brainstorming(brainstorming.id) From d9969168edfa04b762e1d2a73d0361ec3f3818c6 Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Thu, 19 Dec 2024 08:39:35 +0100 Subject: [PATCH 2/4] fix test --- .../live/brainstorming_live/show.ex | 9 +++------ priv/gettext/de/LC_MESSAGES/default.po | 20 +++++++++---------- priv/gettext/default.pot | 20 +++++++++---------- priv/gettext/en/LC_MESSAGES/default.po | 20 +++++++++---------- .../live/label_live/captions_test.exs | 1 + 5 files changed, 34 insertions(+), 36 deletions(-) diff --git a/lib/mindwendel_web/live/brainstorming_live/show.ex b/lib/mindwendel_web/live/brainstorming_live/show.ex index c514416e..5e8c4c12 100644 --- a/lib/mindwendel_web/live/brainstorming_live/show.ex +++ b/lib/mindwendel_web/live/brainstorming_live/show.ex @@ -123,12 +123,9 @@ defmodule MindwendelWeb.BrainstormingLive.Show do def handle_info({:brainstorming_filter_updated, filtered_labels, lanes}, socket) do {:noreply, - push_patch( - socket - |> assign(:filtered_labels, filtered_labels) - |> assign(:lanes, lanes), - to: "/brainstormings/#{socket.assigns.brainstorming.id}" - )} + socket + |> assign(:filtered_labels, filtered_labels) + |> assign(:lanes, lanes)} end def handle_info({:brainstorming_updated, brainstorming}, socket) do diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index 41dc2795..47e8696e 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -26,12 +26,12 @@ msgstr "Wie können wir ..." msgid "Ready?" msgstr "Fertig?" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:201 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:199 #, elixir-autogen, elixir-format msgid "%{name} - Edit" msgstr "%{name} - Editieren" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:178 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:176 #, elixir-autogen, elixir-format msgid "%{name} - New Idea" msgstr "%{name} - Neue Idee" @@ -107,7 +107,7 @@ msgstr "Idee erstellt" msgid "New brainstorming" msgstr "Neues Brainstorming" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:91 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:92 #, elixir-autogen, elixir-format msgid "New idea" msgstr "Neue Idee" @@ -258,7 +258,7 @@ msgstr "Mindwendel Brainstorming" msgid "Share" msgstr "Teilen" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:175 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:176 #, elixir-autogen, elixir-format msgid "Share brainstorming" msgstr "Teile Dein Brainstorming" @@ -278,7 +278,7 @@ msgstr "Download als SVG" msgid "Brainstorming delete are you sure" msgstr "Bist du sicher, dass das Brainstorming gelöscht werden soll?" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:142 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:143 #, elixir-autogen, elixir-format msgid "Update idea" msgstr "Idee bearbeiten" @@ -385,7 +385,7 @@ msgstr "Löschen" msgid "Type the label name" msgstr "Gebe dem Label einen Namen" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:188 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:186 #, elixir-autogen, elixir-format, fuzzy msgid "%{name} - New Lane" msgstr "%{name} - Neue Idee" @@ -402,13 +402,13 @@ msgstr "Name" #: lib/mindwendel_web/live/brainstorming_live/show.html.heex:37 #: lib/mindwendel_web/live/brainstorming_live/show.html.heex:39 -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:125 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:126 #: lib/mindwendel_web/live/lane_live/index_component.html.heex:130 #, elixir-autogen, elixir-format msgid "New lane" msgstr "Neue Spalte" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:159 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:160 #, elixir-autogen, elixir-format, fuzzy msgid "Update lane" msgstr "Spalte bearbeiten" @@ -553,7 +553,7 @@ msgstr "Impressum" msgid "Privacy" msgstr "Datenschutzerklärung" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:108 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:109 #, elixir-autogen, elixir-format msgid "Idea details" msgstr "Detailansicht" @@ -563,7 +563,7 @@ msgstr "Detailansicht" msgid "Give moderating permissions" msgstr "Änderungen erlauben" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:49 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:50 #, elixir-autogen, elixir-format, fuzzy msgid "Brainstorming not found" msgstr "Brainstorming konnte nicht gefunden werden" diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index c94c11b4..471a97d2 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -25,12 +25,12 @@ msgstr "" msgid "Ready?" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:201 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:199 #, elixir-autogen, elixir-format msgid "%{name} - Edit" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:178 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:176 #, elixir-autogen, elixir-format msgid "%{name} - New Idea" msgstr "" @@ -106,7 +106,7 @@ msgstr "" msgid "New brainstorming" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:91 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:92 #, elixir-autogen, elixir-format msgid "New idea" msgstr "" @@ -257,7 +257,7 @@ msgstr "" msgid "Share" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:175 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:176 #, elixir-autogen, elixir-format msgid "Share brainstorming" msgstr "" @@ -277,7 +277,7 @@ msgstr "" msgid "Brainstorming delete are you sure" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:142 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:143 #, elixir-autogen, elixir-format msgid "Update idea" msgstr "" @@ -384,7 +384,7 @@ msgstr "" msgid "Type the label name" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:188 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:186 #, elixir-autogen, elixir-format msgid "%{name} - New Lane" msgstr "" @@ -401,13 +401,13 @@ msgstr "" #: lib/mindwendel_web/live/brainstorming_live/show.html.heex:37 #: lib/mindwendel_web/live/brainstorming_live/show.html.heex:39 -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:125 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:126 #: lib/mindwendel_web/live/lane_live/index_component.html.heex:130 #, elixir-autogen, elixir-format msgid "New lane" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:159 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:160 #, elixir-autogen, elixir-format msgid "Update lane" msgstr "" @@ -552,7 +552,7 @@ msgstr "" msgid "Privacy" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:108 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:109 #, elixir-autogen, elixir-format msgid "Idea details" msgstr "" @@ -562,7 +562,7 @@ msgstr "" msgid "Give moderating permissions" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:49 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:50 #, elixir-autogen, elixir-format msgid "Brainstorming not found" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 977877fc..a8286f97 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -26,12 +26,12 @@ msgstr "" msgid "Ready?" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:201 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:199 #, elixir-autogen, elixir-format msgid "%{name} - Edit" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:178 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:176 #, elixir-autogen, elixir-format msgid "%{name} - New Idea" msgstr "" @@ -107,7 +107,7 @@ msgstr "" msgid "New brainstorming" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:91 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:92 #, elixir-autogen, elixir-format msgid "New idea" msgstr "" @@ -258,7 +258,7 @@ msgstr "Mindwendel Brainstorming" msgid "Share" msgstr "Share" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:175 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:176 #, elixir-autogen, elixir-format msgid "Share brainstorming" msgstr "Share brainstorming" @@ -278,7 +278,7 @@ msgstr "Download as svg" msgid "Brainstorming delete are you sure" msgstr "Are you sure that you want to delete this brainstorming?" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:142 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:143 #, elixir-autogen, elixir-format msgid "Update idea" msgstr "" @@ -385,7 +385,7 @@ msgstr "" msgid "Type the label name" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:188 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:186 #, elixir-autogen, elixir-format, fuzzy msgid "%{name} - New Lane" msgstr "" @@ -402,13 +402,13 @@ msgstr "" #: lib/mindwendel_web/live/brainstorming_live/show.html.heex:37 #: lib/mindwendel_web/live/brainstorming_live/show.html.heex:39 -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:125 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:126 #: lib/mindwendel_web/live/lane_live/index_component.html.heex:130 #, elixir-autogen, elixir-format msgid "New lane" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:159 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:160 #, elixir-autogen, elixir-format, fuzzy msgid "Update lane" msgstr "" @@ -553,7 +553,7 @@ msgstr "" msgid "Privacy" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:108 +#: lib/mindwendel_web/live/brainstorming_live/show.html.heex:109 #, elixir-autogen, elixir-format msgid "Idea details" msgstr "" @@ -563,7 +563,7 @@ msgstr "" msgid "Give moderating permissions" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:49 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:50 #, elixir-autogen, elixir-format, fuzzy msgid "Brainstorming not found" msgstr "" diff --git a/test/mindwendel_web/live/label_live/captions_test.exs b/test/mindwendel_web/live/label_live/captions_test.exs index b1d4a62c..6fc9c5af 100644 --- a/test/mindwendel_web/live/label_live/captions_test.exs +++ b/test/mindwendel_web/live/label_live/captions_test.exs @@ -24,6 +24,7 @@ defmodule MindwendelWeb.LabelLive.CaptionsTest do render_component(CaptionsComponent, id: "captions", brainstorming: preloaded_brainstorming, + filtered_labels: preloaded_brainstorming.filter_labels_ids, current_user: preloaded_user ) From 87bbef8cc75ab7c1537fc4c945e8416190eaac5c Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Thu, 19 Dec 2024 09:53:07 +0100 Subject: [PATCH 3/4] fix modal close bug --- .../components/core_components.ex | 2 ++ .../live/brainstorming_live/show.html.heex | 2 ++ .../live/idea_live/form_component.ex | 2 +- priv/gettext/de/LC_MESSAGES/default.po | 28 +++++++++---------- priv/gettext/default.pot | 28 +++++++++---------- priv/gettext/en/LC_MESSAGES/default.po | 28 +++++++++---------- 6 files changed, 47 insertions(+), 43 deletions(-) diff --git a/lib/mindwendel_web/components/core_components.ex b/lib/mindwendel_web/components/core_components.ex index 06f354d8..3980b022 100644 --- a/lib/mindwendel_web/components/core_components.ex +++ b/lib/mindwendel_web/components/core_components.ex @@ -38,6 +38,7 @@ defmodule MindwendelWeb.CoreComponents do """ attr :id, :string, required: true attr :title, :string, required: false + attr :phx_update, :string, default: "replace" attr :show, :boolean, default: false attr :on_cancel, JS, default: %JS{} slot :inner_block, required: true @@ -53,6 +54,7 @@ defmodule MindwendelWeb.CoreComponents do tabindex="-1" aria-hidden="true" aria-labelledby="{@id}-title" + phx-update={@phx_update} >