Skip to content

Commit

Permalink
chore: adjusted create strand overlay implementation in strands live …
Browse files Browse the repository at this point in the history
…view

controlling overlay visibility with assigns instead of routes, avoiding complex handling of strands loading
  • Loading branch information
endoooo committed Jan 5, 2024
1 parent 5a280c6 commit 820a618
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
16 changes: 9 additions & 7 deletions lib/lanttern_web/live/pages/strands/strands_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions lib/lanttern_web/live/pages/strands/strands_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<.filter_buttons type="years" items={@current_years} />,
<.filter_buttons type="subjects" items={@current_subjects} />
</p>
<.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
</.collection_action>
</div>
Expand Down Expand Up @@ -92,16 +92,22 @@
</:actions>
</.slide_over>
<.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</:title>
<.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}
/>
Expand Down
3 changes: 1 addition & 2 deletions test/lanttern_web/live/pages/strands/strands_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 820a618

Please sign in to comment.