Skip to content

Commit

Permalink
merge session and local storage data
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek committed Dec 18, 2024
1 parent c9915a9 commit b508f85
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
20 changes: 13 additions & 7 deletions lib/mindwendel/brainstormings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ defmodule Mindwendel.Brainstormings do
"""
def list_brainstormings_for(user_id, limit \\ 3) do
Repo.all(
from brainstorming in Brainstorming,
join: users in assoc(brainstorming, :users),
where: users.id == ^user_id,
order_by: [desc: brainstorming.inserted_at],
limit: ^limit
)
case user_id do
nil ->
[]

_ ->
Repo.all(
from brainstorming in Brainstorming,
join: users in assoc(brainstorming, :users),
where: users.id == ^user_id,
order_by: [desc: brainstorming.last_accessed_at],
limit: ^limit
)
end
end

@doc """
Expand Down
9 changes: 6 additions & 3 deletions lib/mindwendel_web/live/brainstorming_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ defmodule MindwendelWeb.BrainstormingLive.Show do

@impl true
def handle_event("brainstormings_from_local_storage", brainstormings_stored, socket) do
# Brainstormings are used from session data and local storage. Session data can be removed later and is only used for a transition period.
valid_stored_brainstormings =
if is_list(brainstormings_stored),
do: brainstormings_stored |> Enum.filter(&valid_stored_brainstorming?/1),
else: []
prepare_initial_brainstormings(
brainstormings_stored,
Brainstormings.list_brainstormings_for(socket.assigns.current_user.id),
socket.assigns.current_user
)

{:noreply, assign(socket, :brainstormings_stored, valid_stored_brainstormings)}
end
Expand Down
48 changes: 45 additions & 3 deletions lib/mindwendel_web/live/live_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,55 @@ defmodule MindwendelWeb.LiveHelpers do
FeatureFlag.enabled?(:feature_file_upload)
end

def format_iso8601(iso8601) do
def format_iso8601(iso8601) when iso8601 != nil do
case DateTime.from_iso8601(iso8601) do
{:ok, date_time, _} -> Timex.from_now(date_time)
{:error, _} -> iso8601
{:ok, date_time, _} -> date_time
{:error, _} -> nil
end
end

def format_iso8601(_) do
nil
end

def prepare_brainstormings_from_local_storage(brainstormings_stored) do
if is_list(brainstormings_stored) do
brainstormings_stored
|> Enum.map(fn e ->
Map.put(e, "last_accessed_at", format_iso8601(e["last_accessed_at"]))
end)
|> Enum.filter(&valid_stored_brainstorming?/1)
else
[]
end
end

def prepare_brainstormings_from_session(brainstormings, user) do
Enum.map(brainstormings, fn brainstorming ->
%{
"last_accessed_at" => brainstorming.last_accessed_at,
"name" => brainstorming.name,
"id" => brainstorming.id,
"admin_url_id" =>
if(has_moderating_permission(brainstorming, user),
do: brainstorming.admin_url_id,
else: nil
)
}
end)
end

def prepare_initial_brainstormings(
brainstormings_from_local_storage,
brainstormings_from_session,
user
) do
(prepare_brainstormings_from_local_storage(brainstormings_from_local_storage) ++
prepare_brainstormings_from_session(brainstormings_from_session, user))
|> Enum.uniq()
|> Enum.sort(&(&1["last_accessed_at"] > &2["last_accessed_at"]))
end

def valid_stored_brainstorming?(brainstorming) do
case Ecto.UUID.cast(brainstorming["id"]) do
{:ok, _} -> brainstorming["last_accessed_at"] && brainstorming["name"]
Expand Down
15 changes: 8 additions & 7 deletions lib/mindwendel_web/live/start_live/home.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ defmodule MindwendelWeb.StartLive.Home do

@impl true
def mount(_, session, socket) do
current_user =
Mindwendel.Services.SessionService.get_current_user_id(session)
|> Mindwendel.Accounts.get_user()
current_user_id = Mindwendel.Services.SessionService.get_current_user_id(session)

form =
%Brainstorming{}
Expand All @@ -19,17 +17,20 @@ defmodule MindwendelWeb.StartLive.Home do

{:ok,
socket
|> assign(:current_user, current_user)
|> assign(:current_user, Mindwendel.Accounts.get_user(current_user_id))
|> assign(:form, form)
|> assign(:brainstormings_stored, [])}
end

@impl true
def handle_event("brainstormings_from_local_storage", brainstormings_stored, socket) do
# Brainstormings are used from session data and local storage. Session data can be removed later and is only used for a transition period.
valid_stored_brainstormings =
if is_list(brainstormings_stored),
do: brainstormings_stored |> Enum.filter(&valid_stored_brainstorming?/1),
else: []
prepare_initial_brainstormings(
brainstormings_stored,
Brainstormings.list_brainstormings_for(socket.assigns.current_user.id),
socket.assigns.current_user
)

{:noreply, assign(socket, :brainstormings_stored, valid_stored_brainstormings)}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mindwendel_web/live/start_live/home.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
{brainstorming["name"]}
</.link>
<span class="badge rounded-pill bg-light text-dark">
{format_iso8601(brainstorming["last_accessed_at"])}
{Timex.from_now(brainstorming["last_accessed_at"])}
</span>
</h5>
<% end %>
Expand Down

0 comments on commit b508f85

Please sign in to comment.