Skip to content

Commit

Permalink
refactor after local storage pr feedback (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek authored Dec 26, 2024
1 parent 0a2d47f commit ca36ed9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 26 deletions.
10 changes: 5 additions & 5 deletions lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ defmodule Mindwendel.Brainstormings do
"""
def list_brainstormings_for(user_id, limit \\ 3)

def list_brainstormings_for(user_id, limit) when user_id != nil do
def list_brainstormings_for(nil, _) do
[]
end

def list_brainstormings_for(user_id, limit) do
Repo.all(
from brainstorming in Brainstorming,
join: users in assoc(brainstorming, :users),
Expand All @@ -37,10 +41,6 @@ defmodule Mindwendel.Brainstormings do
)
end

def list_brainstormings_for(nil, _) do
[]
end

@doc """
Returns the list of brainstormings.
Expand Down
9 changes: 5 additions & 4 deletions lib/mindwendel/local_storage.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Mindwendel.LocalStorage do
alias Mindwendel.Permissions
alias Mindwendel.Brainstormings.Brainstorming

@moduledoc """
The LocalStorage context. It includes helpers to handle local storage data from clients.
Expand All @@ -15,7 +16,7 @@ defmodule Mindwendel.LocalStorage do
(brainstormings_from_local_storage(brainstormings_from_local_storage) ++
brainstormings_from_session(brainstormings_from_session, user))
|> Enum.uniq_by(& &1["id"])
|> Enum.sort(&(&1["last_accessed_at"] > &2["last_accessed_at"]))
|> Enum.sort_by(& &1["last_accessed_at"], {:desc, DateTime})
end

defp brainstormings_from_local_storage(brainstormings_stored)
Expand Down Expand Up @@ -50,9 +51,9 @@ defmodule Mindwendel.LocalStorage do
[]
end

defp valid_stored_brainstorming?(brainstorming) do
case Ecto.UUID.cast(brainstorming["id"]) do
{:ok, _} -> brainstorming["last_accessed_at"] && brainstorming["name"]
defp valid_stored_brainstorming?(attrs) do
case Ecto.UUID.cast(attrs["id"]) do
{:ok, _} -> Brainstorming.changeset(%Brainstorming{}, attrs).valid?
:error -> false
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/mindwendel/permissions.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
defmodule Mindwendel.Permissions do
alias Mindwendel.Accounts.User

@moduledoc """
The Permissions context.
"""
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)
def has_moderating_permission(brainstorming_id, %User{} = current_user) do
current_user.moderated_brainstormings
|> Enum.map(& &1.id)
|> Enum.member?(brainstorming_id)
end

def has_moderating_permission(_, _) do
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 @@ -92,7 +92,7 @@ defmodule MindwendelWeb.IdeaLive.FormComponent do

# This is a workaround to get the filtered labels for the idea without (!) passing them as a parameter to the form component.
# Unfortunatly, passing either the brainstorming or filter labels directly triggers a re-render of the form component when changing the filter labels and results in a stuck bootstrap modal.
{:ok, brainstorming} = Brainstormings.get_brainstorming(socket.assigns.brainstorming_id)
brainstorming = Brainstormings.get_bare_brainstorming!(socket.assigns.brainstorming_id)
filtered_labels = brainstorming.filter_labels_ids

idea_params_merged =
Expand Down
14 changes: 7 additions & 7 deletions priv/gettext/de/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ msgid "%{name} - New Idea"
msgstr "%{name} - Neue Idee"

#: lib/mindwendel_web/live/comment_live/show_component.html.heex:22
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:18
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:17
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this idea?"
msgstr "Möchtest du die Idee löschen?"
Expand Down Expand Up @@ -196,7 +196,7 @@ msgstr "Nutzername"

#: lib/mindwendel_web/controllers/admin/brainstorming_html/export.html.heex:3
#: lib/mindwendel_web/live/comment_live/show_component.html.heex:40
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:85
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:84
#, elixir-autogen, elixir-format
msgid "By"
msgstr "Von"
Expand Down Expand Up @@ -452,13 +452,13 @@ msgstr "Eine Spalte"
msgid "No lanes available"
msgstr "Keine Spalten vorhanden"

#: lib/mindwendel_web/live/idea_live/card_component.html.heex:25
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:24
#, elixir-autogen, elixir-format, fuzzy
msgid "Edit idea"
msgstr "Idee bearbeiten"

#: lib/mindwendel_web/live/comment_live/show_component.html.heex:30
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:17
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:16
#, elixir-autogen, elixir-format, fuzzy
msgid "Delete idea"
msgstr "Löschen"
Expand All @@ -473,7 +473,7 @@ msgstr "Idee aktualisiert"
msgid "Additional Attachment"
msgstr "Zusätzlicher Anhang"

#: lib/mindwendel_web/live/idea_live/card_component.html.heex:75
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:74
#: lib/mindwendel_web/live/idea_live/form_component.html.heex:43
#: lib/mindwendel_web/live/idea_live/show_component.html.heex:27
#, elixir-autogen, elixir-format
Expand Down Expand Up @@ -530,8 +530,8 @@ msgstr "Löschen"
msgid "No comments available"
msgstr "Keine Kommentare vorhanden"

#: lib/mindwendel_web/live/idea_live/card_component.html.heex:33
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:140
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:32
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:137
#, elixir-autogen, elixir-format, fuzzy
msgid "Show idea"
msgstr "Zeige Idee"
Expand Down
14 changes: 7 additions & 7 deletions priv/gettext/en/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ msgid "%{name} - New Idea"
msgstr ""

#: lib/mindwendel_web/live/comment_live/show_component.html.heex:22
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:18
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:17
#, elixir-autogen, elixir-format
msgid "Are you sure you want to delete this idea?"
msgstr ""
Expand Down Expand Up @@ -196,7 +196,7 @@ msgstr ""

#: lib/mindwendel_web/controllers/admin/brainstorming_html/export.html.heex:3
#: lib/mindwendel_web/live/comment_live/show_component.html.heex:40
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:85
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:84
#, elixir-autogen, elixir-format
msgid "By"
msgstr ""
Expand Down Expand Up @@ -452,13 +452,13 @@ msgstr ""
msgid "No lanes available"
msgstr ""

#: lib/mindwendel_web/live/idea_live/card_component.html.heex:25
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:24
#, elixir-autogen, elixir-format, fuzzy
msgid "Edit idea"
msgstr ""

#: lib/mindwendel_web/live/comment_live/show_component.html.heex:30
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:17
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:16
#, elixir-autogen, elixir-format, fuzzy
msgid "Delete idea"
msgstr ""
Expand All @@ -473,7 +473,7 @@ msgstr ""
msgid "Additional Attachment"
msgstr ""

#: lib/mindwendel_web/live/idea_live/card_component.html.heex:75
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:74
#: lib/mindwendel_web/live/idea_live/form_component.html.heex:43
#: lib/mindwendel_web/live/idea_live/show_component.html.heex:27
#, elixir-autogen, elixir-format
Expand Down Expand Up @@ -530,8 +530,8 @@ msgstr ""
msgid "No comments available"
msgstr ""

#: lib/mindwendel_web/live/idea_live/card_component.html.heex:33
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:140
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:32
#: lib/mindwendel_web/live/idea_live/card_component.html.heex:137
#, elixir-autogen, elixir-format, fuzzy
msgid "Show idea"
msgstr ""
Expand Down
5 changes: 5 additions & 0 deletions test/mindwendel/permissions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ defmodule Mindwendel.PermissionsTest do
brainstorming = Factory.insert!(:brainstorming)
refute Permissions.has_moderating_permission(brainstorming.id, Accounts.get_user(user.id))
end

test "returns false if user is nil" do
brainstorming = Factory.insert!(:brainstorming)
refute Permissions.has_moderating_permission(brainstorming.id, nil)
end
end
end

0 comments on commit ca36ed9

Please sign in to comment.