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 Aug 27, 2024
1 parent 0291d1e commit e4b3048
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 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 @@ -38,12 +38,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 @@ -75,7 +72,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: 20 additions & 2 deletions lib/radiator_web/live/outline_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ defmodule RadiatorWeb.OutlineComponent do
|> reply(:noreply)
end

def handle_event("set_collapsed", _node_id, socket) do
def handle_event("set_collapsed", node_id, socket) do
socket
|> store_node_state(node_id, %{collapsed: true})
|> reply(:noreply)
end

def handle_event("set_expanded", _node_id, socket) do
def handle_event("set_expanded", node_id, socket) do
socket
|> store_node_state(node_id, %{collapsed: false})
|> reply(:noreply)
end

Expand Down Expand Up @@ -130,6 +132,22 @@ defmodule RadiatorWeb.OutlineComponent do
|> reply(:noreply)
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)
})
end

defp get_nodes(id), do: Outline.list_nodes_by_episode_sorted(id)

# defp to_change_form(node_or_changeset, params, action \\ nil) do
Expand Down

0 comments on commit e4b3048

Please sign in to comment.