From 40c4746c3629e12cad9e06d99e201312a2b117c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20W=C3=B6ginger?= Date: Tue, 27 Aug 2024 19:30:06 +0200 Subject: [PATCH] WIP: store state after collapsing/ellapsing --- lib/radiator_web/live/episode_live/index.ex | 9 +++------ lib/radiator_web/live/outline_component.ex | 22 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/radiator_web/live/episode_live/index.ex b/lib/radiator_web/live/episode_live/index.ex index 54ab691a..386ef94f 100644 --- a/lib/radiator_web/live/episode_live/index.ex +++ b/lib/radiator_web/live/episode_live/index.ex @@ -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}" @@ -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 diff --git a/lib/radiator_web/live/outline_component.ex b/lib/radiator_web/live/outline_component.ex index b09bf6b4..718f57cf 100644 --- a/lib/radiator_web/live/outline_component.ex +++ b/lib/radiator_web/live/outline_component.ex @@ -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 @@ -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) + }) + end + defp generate_event_id(id), do: Ecto.UUID.generate() <> ":" <> id end