Skip to content

Commit

Permalink
do not save the page on every change
Browse files Browse the repository at this point in the history
and preserve state
  • Loading branch information
leandrocp committed Jan 11, 2024
1 parent 5cc028d commit d3e1f57
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/beacon/live_admin/live/page_editor_live/edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,13 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Edit do
{:reply, %{"ast" => ast}, socket}
end

def handle_event("update_page_ast", %{"id" => id, "ast" => ast}, socket) do
page = Content.get_page(socket.assigns.beacon_page.site, id, preloads: [:layout])

case Content.update_page(socket.assigns.beacon_page.site, page, %{"ast" => ast}) do
{:ok, page} ->
{:noreply, assign(socket, :page, page)}
def handle_event("update_page_ast", %{"ast" => ast}, socket) do
send_update(Beacon.LiveAdmin.PageEditorLive.FormComponent,
id: "page-editor-form-edit",
ast: ast
)

# FIXME: handle update ast error
{:error, _changeset} ->
throw("How should we handle this?")
end
{:noreply, socket}
end

@impl true
Expand Down
24 changes: 24 additions & 0 deletions lib/beacon/live_admin/live/page_editor_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.FormComponent do
alias Beacon.LiveAdmin.Config
alias Beacon.LiveAdmin.Content
alias Beacon.LiveAdmin.WebAPI
alias Ecto.Changeset

@impl true
def update(%{site: site, page: page} = assigns, socket) do
Expand Down Expand Up @@ -41,6 +42,29 @@ defmodule Beacon.LiveAdmin.PageEditorLive.FormComponent do
{:ok, assign_form(socket, changeset)}
end

def update(%{ast: ast}, socket) do
template = Beacon.Template.HEEx.HEExDecoder.decode(ast)
params = Map.merge(socket.assigns.form.params, %{"template" => template})
changeset = Content.change_page(socket.assigns.site, socket.assigns.page, params)

case Changeset.apply_action(changeset, :update) do
{:ok, page} ->
# TODO: remove web api
%{data: builder_page} = WebAPI.Page.show(page.site, page)

{:ok,
socket
|> assign_form(changeset)
|> assign(:template, page.template)
|> assign(:changed_template, page.template)
|> assign(:builder_page, builder_page)}

_ ->
# TODO: handle errors
{:ok, socket}
end
end

@impl true
def handle_event(
"validate",
Expand Down

0 comments on commit d3e1f57

Please sign in to comment.