Skip to content

Commit

Permalink
Move shared logic to its own method
Browse files Browse the repository at this point in the history
  • Loading branch information
cibernox committed Dec 13, 2024
1 parent 5090a65 commit 1d43f5e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
16 changes: 4 additions & 12 deletions lib/beacon/live_admin/live/page_editor_live/edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Edit do
@moduledoc false
require Logger
use Beacon.LiveAdmin.PageBuilder
use Beacon.LiveAdmin.PageEditorLive.ElementSelection
alias Beacon.LiveAdmin.Client.Content
alias Beacon.LiveAdmin.WebAPI

Expand Down Expand Up @@ -38,7 +39,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Edit do
@impl true
def handle_event("set_template", %{"value" => value}, socket) do
send_update(Beacon.LiveAdmin.PageEditorLive.FormComponent,
id: "page-editor-form-edit",
id: "page-editor-form",
template: value
)

Expand Down Expand Up @@ -71,18 +72,9 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Edit do
{:reply, %{"ast" => ast}, socket}
end

def handle_event("update_page_ast", %{"ast" => ast}, socket) do
send_update(Beacon.LiveAdmin.PageEditorLive.FormComponent, id: "page-editor-form-edit", ast: ast)
{:noreply, socket}
end

def handle_event("select_element", %{"path" => path}, socket) do
{:noreply, assign(socket, selected_element_path: path)}
end

@impl true
def handle_info({:element_changed, {path, payload}}, socket) do
send_update(Beacon.LiveAdmin.PageEditorLive.FormComponent, id: "page-editor-form-edit", path: path, payload: payload)
send_update(Beacon.LiveAdmin.PageEditorLive.FormComponent, id: "page-editor-form", path: path, payload: payload)
{:noreply, socket}
end

Expand All @@ -91,7 +83,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Edit do
~H"""
<.live_component
module={Beacon.LiveAdmin.PageEditorLive.FormComponent}
id="page-editor-form-edit"
id="page-editor-form"
live_action={@live_action}
page_title={@page_title}
site={@beacon_page.site}
Expand Down
16 changes: 16 additions & 0 deletions lib/beacon/live_admin/live/page_editor_live/element_selection.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Beacon.LiveAdmin.PageEditorLive.ElementSelection do
@moduledoc false
defmacro __using__(opts) do
quote location: :keep, bind_quoted: [opts: opts] do
def handle_event("select_element", %{"path" => path}, socket) do
{:noreply, assign(socket, selected_element_path: path)}
end

@impl true
def handle_info({:element_changed, {path, payload}}, socket) do
send_update(Beacon.LiveAdmin.PageEditorLive.FormComponent, id: "page-editor-form", path: path, payload: payload)
{:noreply, socket}
end
end
end
end
7 changes: 4 additions & 3 deletions lib/beacon/live_admin/live/page_editor_live/new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.New do
@moduledoc false

use Beacon.LiveAdmin.PageBuilder
use Beacon.LiveAdmin.PageEditorLive.ElementSelection
alias Beacon.LiveAdmin.Client.Content
alias Beacon.LiveAdmin.WebAPI

Expand Down Expand Up @@ -56,7 +57,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.New do
@impl true
def handle_event("set_template", %{"value" => value}, socket) do
send_update(Beacon.LiveAdmin.PageEditorLive.FormComponent,
id: "page-editor-form-new",
id: "page-editor-form",
template: value
)

Expand Down Expand Up @@ -91,7 +92,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.New do

def handle_event("update_page_ast", %{"ast" => ast}, socket) do
send_update(Beacon.LiveAdmin.PageEditorLive.FormComponent,
id: "page-editor-form-new",
id: "page-editor-form",
ast: ast
)

Expand All @@ -103,7 +104,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.New do
~H"""
<.live_component
module={Beacon.LiveAdmin.PageEditorLive.FormComponent}
id="page-editor-form-new"
id="page-editor-form"
live_action={@live_action}
page_title={@page_title}
site={@beacon_page.site}
Expand Down

0 comments on commit 1d43f5e

Please sign in to comment.