Skip to content

Commit

Permalink
WIP: store state after collapsing/ellapsing
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Sep 2, 2024
1 parent 14e1454 commit 40c4746
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
9 changes: 3 additions & 6 deletions lib/radiator_web/live/episode_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ defmodule RadiatorWeb.EpisodeLive.Index do
def handle_params(params, _uri, socket) do
episode = get_selected_episode(params)

if connected?(socket) and episode do
Dispatch.subscribe(episode.id)
end

socket =
if connected?(socket) do
if connected?(socket) and episode do
Dispatch.subscribe(episode.id)
# TODO: Not too sure wether we should use one key for all episodes or one key per episode.
storage_key = "radiator-episode-#{episode.id}"

Expand Down Expand Up @@ -74,7 +71,7 @@ defmodule RadiatorWeb.EpisodeLive.Index do
end
end

defp serialize_to_token(state_data) do
def serialize_to_token(state_data) do
salt = Application.get_env(:radiator, RadiatorWeb.Endpoint)[:live_view][:signing_salt]
Phoenix.Token.encrypt(RadiatorWeb.Endpoint, salt, state_data)
end
Expand Down
22 changes: 21 additions & 1 deletion lib/radiator_web/live/outline_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ defmodule RadiatorWeb.OutlineComponent do
|> reply(:noreply)
end

def handle_event("toggle_collapse", %{"uuid" => _uuid}, socket) do
def handle_event("toggle_collapse", %{"uuid" => uuid}, socket) do
socket
|> store_node_state(uuid, %{collapsed: true})
|> reply(:noreply)
end

Expand Down Expand Up @@ -138,5 +139,24 @@ defmodule RadiatorWeb.OutlineComponent do
to_form(changeset, as: "node", id: "form-#{changeset.data.uuid}")
end

defp store_node_state(socket, node_id, data) do
state_to_store = %{collapsed: data.collapsed}

current_session_info =
if Map.has_key?(socket.assigns, :user_session_info) do
socket.assigns.user_session_info
else
%{}
end

state = Map.put(current_session_info, node_id, state_to_store)

socket
|> push_event("store", %{
key: socket.assigns.user_session_info,
data: RadiatorWeb.EpisodeLive.Index.serialize_to_token(state)

Check warning on line 157 in lib/radiator_web/live/outline_component.ex

View workflow job for this annotation

GitHub Actions / Build & Test

Nested modules could be aliased at the top of the invoking module.
})
end

defp generate_event_id(id), do: Ecto.UUID.generate() <> ":" <> id
end

0 comments on commit 40c4746

Please sign in to comment.