From 820a618844efe1004043a245042c30d526bd2b32 Mon Sep 17 00:00:00 2001 From: endoooo Date: Thu, 4 Jan 2024 22:46:54 -0300 Subject: [PATCH] chore: adjusted create strand overlay implementation in strands live view controlling overlay visibility with assigns instead of routes, avoiding complex handling of strands loading --- .../live/pages/strands/strands_live.ex | 16 +++++++++------- .../live/pages/strands/strands_live.html.heex | 14 ++++++++++---- .../live/pages/strands/strands_live_test.exs | 3 +-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/lanttern_web/live/pages/strands/strands_live.ex b/lib/lanttern_web/live/pages/strands/strands_live.ex index 2e093434..40d22c88 100644 --- a/lib/lanttern_web/live/pages/strands/strands_live.ex +++ b/lib/lanttern_web/live/pages/strands/strands_live.ex @@ -51,6 +51,7 @@ defmodule LantternWeb.StrandsLive do def mount(_params, _session, socket) do {:ok, socket + |> assign(:is_creating_strand, false) |> assign(:current_subjects, []) |> assign(:current_years, [])} end @@ -73,6 +74,14 @@ defmodule LantternWeb.StrandsLive do # event handlers @impl true + def handle_event("create-strand", _params, socket) do + {:noreply, assign(socket, :is_creating_strand, true)} + end + + def handle_event("cancel-strand-creation", _params, socket) do + {:noreply, assign(socket, :is_creating_strand, false)} + end + def handle_event("load-more", _params, socket) do {:noreply, load_strands(socket)} end @@ -101,13 +110,6 @@ defmodule LantternWeb.StrandsLive do |> push_navigate(to: path(socket, ~p"/strands?#{params}"))} end - # info handlers - - @impl true - def handle_info({StrandFormComponent, {:saved, strand}}, socket) do - {:noreply, stream_insert(socket, :strands, strand)} - end - # helpers defp handle_assigns(socket, params) do diff --git a/lib/lanttern_web/live/pages/strands/strands_live.html.heex b/lib/lanttern_web/live/pages/strands/strands_live.html.heex index 745486e5..c32c60c9 100644 --- a/lib/lanttern_web/live/pages/strands/strands_live.html.heex +++ b/lib/lanttern_web/live/pages/strands/strands_live.html.heex @@ -6,7 +6,7 @@ <.filter_buttons type="years" items={@current_years} />, <.filter_buttons type="subjects" items={@current_subjects} />

- <.collection_action type="link" patch={~p"/strands/new"} icon_name="hero-plus-circle"> + <.collection_action type="button" icon_name="hero-plus-circle" phx-click="create-strand"> Create new strand @@ -92,16 +92,22 @@ <.slide_over - :if={@live_action == :new} + :if={@is_creating_strand} id="strand-form-overlay" show={true} - on_cancel={JS.patch(~p"/strands")} + on_cancel={JS.push("cancel-strand-creation")} > <:title>New strand <.live_component module={StrandFormComponent} id={:new} - strand={%Strand{curriculum_items: [], subjects: [], years: []}} + strand={ + %Strand{ + curriculum_items: [], + subjects: @current_subjects, + years: @current_years + } + } action={:new} navigate={fn strand -> ~p"/strands/#{strand}" end} /> diff --git a/test/lanttern_web/live/pages/strands/strands_live_test.exs b/test/lanttern_web/live/pages/strands/strands_live_test.exs index 37724ec9..2abb5753 100644 --- a/test/lanttern_web/live/pages/strands/strands_live_test.exs +++ b/test/lanttern_web/live/pages/strands/strands_live_test.exs @@ -52,8 +52,7 @@ defmodule LantternWeb.StrandsLiveTest do {:ok, view, _html} = live(conn, @live_view_path) # open create strand overlay - view |> element("a", "Create new strand") |> render_click() - assert_patch(view, "/strands/new") + view |> element("button", "Create new strand") |> render_click() assert view |> has_element?("h2", "New strand") # add subject