From e1f137578ae1cb7dda638b857fb94bbfdb3dbc99 Mon Sep 17 00:00:00 2001 From: Miguel Camba Date: Fri, 20 Oct 2023 22:01:10 +0200 Subject: [PATCH] It works!! --- .../live_admin/live/page_editor_live/edit.ex | 9 +++------ lib/beacon/live_admin/web_api/component.ex | 14 ++++++++++++++ .../live_admin/{web_api.ex => web_api/page.ex} | 0 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 lib/beacon/live_admin/web_api/component.ex rename lib/beacon/live_admin/{web_api.ex => web_api/page.ex} (100%) diff --git a/lib/beacon/live_admin/live/page_editor_live/edit.ex b/lib/beacon/live_admin/live/page_editor_live/edit.ex index 4634a142..33b85ce0 100644 --- a/lib/beacon/live_admin/live/page_editor_live/edit.ex +++ b/lib/beacon/live_admin/live/page_editor_live/edit.ex @@ -5,6 +5,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Edit do require IEx use Beacon.LiveAdmin.PageBuilder alias Beacon.LiveAdmin.Content + alias Beacon.LiveAdmin.WebAPI @impl true def menu_link("/pages", :edit), do: {:submenu, "Pages"} @@ -58,12 +59,8 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Edit do def handle_event("render_component_in_page", %{ "component_id" => component_id, "page_id" => page_id }, socket) do page = Content.get_page(socket.assigns.beacon_page.site, page_id) component = Content.get_component(socket.assigns.beacon_page.site, component_id) - # path = for segment <- String.split(page.path, "/"), segment != "", do: segment - # beacon_live_data = Beacon.DataSource.live_data(page.site, path, []) - # {:ok, ast} = Beacon.Template.HEEx.JSONEncoder.encode(component.site, component.body, assigns) - # data = Map.put(data(component), :ast, ast) - # %{data: data} - {:reply, %{ "ast" => "sample_data" }, socket} + %{data: %{ ast: ast } } = WebAPI.Component.show_ast(socket.assigns.beacon_page.site, component, page) + {:reply, %{ "ast" => ast }, socket} end def handle_event("update_page_ast", %{ "id" => id, "ast" => ast }, socket) do diff --git a/lib/beacon/live_admin/web_api/component.ex b/lib/beacon/live_admin/web_api/component.ex new file mode 100644 index 00000000..ddbf1fe4 --- /dev/null +++ b/lib/beacon/live_admin/web_api/component.ex @@ -0,0 +1,14 @@ +defmodule Beacon.LiveAdmin.WebAPI.Component do + @moduledoc """ + Calls Beacon Web API through the cluster. + """ + + import Beacon.LiveAdmin.Cluster, only: [call: 4] + + def show_ast(site, component, page) do + path = for segment <- String.split(page.path, "/"), segment != "", do: segment + # beacon_live_data = Beacon.DataSource.live_data(site, path, []) + beacon_live_data = call(site, Beacon.DataSource, :live_data, [site, path, []]) + call(site, BeaconWeb.API.ComponentJSON, :show_ast, [%{component: component, assigns: %{beacon_live_data: beacon_live_data}}]) + end +end diff --git a/lib/beacon/live_admin/web_api.ex b/lib/beacon/live_admin/web_api/page.ex similarity index 100% rename from lib/beacon/live_admin/web_api.ex rename to lib/beacon/live_admin/web_api/page.ex