Skip to content

Commit

Permalink
Extract label filter to own variable instead of using brainstorming (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek authored Dec 19, 2024
1 parent 21d1680 commit d572326
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 84 deletions.
22 changes: 12 additions & 10 deletions lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ defmodule Mindwendel.Brainstormings do
:moderating_users,
labels: from(idea_label in IdeaLabel, order_by: idea_label.position_order)
])
|> update_last_accessed_at()

{:ok, preloaded_brainstorming}
end
Expand Down Expand Up @@ -156,12 +155,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
Expand Down Expand Up @@ -248,6 +247,15 @@ defmodule Mindwendel.Brainstormings do
Brainstorming.changeset(brainstorming, attrs)
end

@doc """
Updates the last_accessed_at field of a brainstorming.
## Examples
iex> update_last_accessed_at(brainstorming)
%Brainstorming{last_accessed_at: ...}
"""
def update_last_accessed_at(brainstorming) do
Repo.update(Brainstorming.changeset_with_upated_last_accessed_at(brainstorming))
brainstorming
Expand Down Expand Up @@ -292,13 +300,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}
Expand Down
2 changes: 2 additions & 0 deletions lib/mindwendel_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,6 +54,7 @@ defmodule MindwendelWeb.CoreComponents do
tabindex="-1"
aria-hidden="true"
aria-labelledby="{@id}-title"
phx-update={@phx_update}
>
<div class="modal-dialog modal-lg">
<div class="modal-content">
Expand Down
13 changes: 6 additions & 7 deletions lib/mindwendel_web/live/brainstorming_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule MindwendelWeb.BrainstormingLive.Show do

case Brainstormings.get_brainstorming(id) do
{:ok, brainstorming} ->
Brainstormings.update_last_accessed_at(brainstorming)
admin_secret = get_connect_params(socket)["adminSecret"]

if Brainstormings.validate_admin_secret(brainstorming, admin_secret) do
Expand All @@ -38,6 +39,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())
}
Expand Down Expand Up @@ -120,14 +122,11 @@ 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(:lanes, lanes),
to: "/brainstormings/#{brainstorming.id}"
)}
socket
|> assign(:filtered_labels, filtered_labels)
|> assign(:lanes, lanes)}
end

def handle_info({:brainstorming_updated, brainstorming}, socket) do
Expand Down
3 changes: 3 additions & 0 deletions lib/mindwendel_web/live/brainstorming_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
module={MindwendelWeb.LabelLive.CaptionsComponent}
id="captions"
brainstorming={@brainstorming}
filtered_labels={@filtered_labels}
current_user={@current_user}
/>

Expand All @@ -89,12 +90,14 @@
show
on_cancel={JS.patch(~p"/brainstormings/#{@brainstorming.id}")}
title={gettext("New idea")}
phx_update="ignore"
>
<.live_component
module={MindwendelWeb.IdeaLive.FormComponent}
id={:new}
action={:new}
brainstorming={@brainstorming}
filtered_labels={@filtered_labels}
current_user={@current_user}
idea={@idea}
/>
Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel_web/live/idea_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule MindwendelWeb.IdeaLive.FormComponent do
|> Map.put("user_id", socket.assigns.current_user.id)
|> Map.put(
"idea_labels",
IdeaLabels.get_idea_labels(socket.assigns.brainstorming.filter_labels_ids)
IdeaLabels.get_idea_labels(socket.assigns.filtered_labels)
)
|> Map.put("tmp_attachments", tmp_attachments)

Expand Down
23 changes: 15 additions & 8 deletions lib/mindwendel_web/live/label_live/captions_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="d-flex justify-content-end">
<%= 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()}"}
Expand Down
38 changes: 19 additions & 19 deletions priv/gettext/de/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -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:200
#, 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:177
#, elixir-autogen, elixir-format
msgid "%{name} - New Idea"
msgstr "%{name} - Neue Idee"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:178
#, elixir-autogen, elixir-format
msgid "Share brainstorming"
msgstr "Teile Dein Brainstorming"
Expand All @@ -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:145
#, elixir-autogen, elixir-format
msgid "Update idea"
msgstr "Idee bearbeiten"
Expand Down Expand Up @@ -319,43 +319,43 @@ msgstr "Nutzern das Verschieben und Sortieren von Ideen erlauben"
msgid "Back"
msgstr "Zurück"

#: lib/mindwendel_web/components/core_components.ex:528
#: lib/mindwendel_web/components/core_components.ex:530
#, elixir-autogen, elixir-format
msgid "Actions"
msgstr ""

#: lib/mindwendel_web/components/core_components.ex:156
#: lib/mindwendel_web/components/core_components.ex:158
#, elixir-autogen, elixir-format
msgid "Attempting to reconnect"
msgstr ""

#: lib/mindwendel_web/components/core_components.ex:147
#: lib/mindwendel_web/components/core_components.ex:149
#, elixir-autogen, elixir-format
msgid "Error!"
msgstr ""

#: lib/mindwendel_web/components/core_components.ex:168
#: lib/mindwendel_web/components/core_components.ex:170
#, elixir-autogen, elixir-format
msgid "Hang in there while we get back on track"
msgstr ""

#: lib/mindwendel_web/components/core_components.ex:163
#: lib/mindwendel_web/components/core_components.ex:165
#, elixir-autogen, elixir-format
msgid "Something went wrong!"
msgstr ""

#: lib/mindwendel_web/components/core_components.ex:146
#: lib/mindwendel_web/components/core_components.ex:148
#, elixir-autogen, elixir-format
msgid "Success!"
msgstr ""

#: lib/mindwendel_web/components/core_components.ex:151
#: lib/mindwendel_web/components/core_components.ex:153
#, elixir-autogen, elixir-format
msgid "We can't find the internet"
msgstr ""

#: lib/mindwendel_web/components/core_components.ex:71
#: lib/mindwendel_web/components/core_components.ex:126
#: lib/mindwendel_web/components/core_components.ex:73
#: lib/mindwendel_web/components/core_components.ex:128
#, elixir-autogen, elixir-format, fuzzy
msgid "close"
msgstr "Schließen"
Expand Down Expand Up @@ -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:187
#, elixir-autogen, elixir-format, fuzzy
msgid "%{name} - New Lane"
msgstr "%{name} - Neue Idee"
Expand All @@ -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:128
#: 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:162
#, elixir-autogen, elixir-format, fuzzy
msgid "Update lane"
msgstr "Spalte bearbeiten"
Expand Down Expand Up @@ -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:111
#, elixir-autogen, elixir-format
msgid "Idea details"
msgstr "Detailansicht"
Expand All @@ -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:51
#, elixir-autogen, elixir-format, fuzzy
msgid "Brainstorming not found"
msgstr "Brainstorming konnte nicht gefunden werden"
Loading

0 comments on commit d572326

Please sign in to comment.