diff --git a/lib/decentralised_book_index_web/live/dbi_server_live/edit.ex b/lib/decentralised_book_index_web/live/dbi_server_live/edit.ex new file mode 100644 index 0000000..a9c4d35 --- /dev/null +++ b/lib/decentralised_book_index_web/live/dbi_server_live/edit.ex @@ -0,0 +1,56 @@ +defmodule DecentralisedBookIndexWeb.DbiServerLive.Edit do + use DecentralisedBookIndexWeb, :live_view + + alias DecentralisedBookIndex.Metadata + + @impl true + def render(assigns) do + ~H""" + <.live_component + module={DecentralisedBookIndexWeb.DbiServerLive.FormComponent} + id={(@dbi_server && @dbi_server.id) || :new} + title={@page_title} + current_user={@current_user} + action={@live_action} + dbi_server={@dbi_server} + /> + """ + end + + @impl true + def mount(_params, _session, socket) do + {:ok, + socket + |> assign_new(:current_user, fn -> nil end)} + end + + @impl true + def handle_params(params, _url, socket) do + socket = + socket + |> assign(:params, params) + |> apply_action(socket.assigns.live_action, params) + + {:noreply, socket} + end + + defp apply_action(socket, :edit, %{"id" => id}) do + socket + |> assign(:page_title, "Edit Server") + |> assign( + :dbi_server, + Ash.get!(DecentralisedBookIndex.Metadata.DBIServer, id, actor: socket.assigns.current_user) + ) + end + + defp apply_action(socket, :new, _params) do + socket + |> assign(:page_title, "New Server") + |> assign(:dbi_server, nil) + end + + @impl true + def handle_info({DecentralisedBookIndexWeb.AuthorLive.FormComponent, {:saved, author}}, socket) do + {:noreply, socket} + end +end diff --git a/lib/decentralised_book_index_web/live/dbi_server_live/form_component.ex b/lib/decentralised_book_index_web/live/dbi_server_live/form_component.ex index 62465f6..da5c80c 100644 --- a/lib/decentralised_book_index_web/live/dbi_server_live/form_component.ex +++ b/lib/decentralised_book_index_web/live/dbi_server_live/form_component.ex @@ -26,6 +26,9 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.FormComponent do <.save_button phx-disable-with="Saving..."> Save + <.cancel_button phx-click="cancel" phx-target={@myself}> + Cancel + @@ -54,7 +57,7 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.FormComponent do socket = socket |> put_flash(:info, "Server #{socket.assigns.form.source.type}d successfully") - |> push_patch(to: socket.assigns.patch) + |> push_redirect(to: patch_url(socket.assigns.action, dbi_server.id)) {:noreply, socket} @@ -63,6 +66,17 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.FormComponent do end end + def handle_event("cancel", _params, socket) do + dbi_server = socket.assigns.dbi_server + dbi_server_id = if dbi_server == nil, do: nil, else: dbi_server.id + + socket = + socket + |> push_redirect(to: patch_url(socket.assigns.action, dbi_server_id)) + + {:noreply, socket} + end + defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) defp assign_form(%{assigns: %{dbi_server: dbi_server}} = socket) do @@ -81,4 +95,12 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.FormComponent do assign(socket, form: to_form(form)) end + + defp patch_url(action, server_id) do + case action do + :edit -> ~p"/servers/#{server_id}" + :new -> ~p"/servers" + _ -> ~p"/servers" + end + end end diff --git a/lib/decentralised_book_index_web/live/dbi_server_live/index.ex b/lib/decentralised_book_index_web/live/dbi_server_live/index.ex index badaa91..8f18216 100644 --- a/lib/decentralised_book_index_web/live/dbi_server_live/index.ex +++ b/lib/decentralised_book_index_web/live/dbi_server_live/index.ex @@ -67,26 +67,6 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do /> <% end %> - -
-
- - <.modal - :if={@live_action in [:new, :edit]} - id="dbi_server-modal" - show - on_cancel={JS.patch(~p"/servers")} - > - <.live_component - module={DecentralisedBookIndexWeb.DbiServerLive.FormComponent} - id={(@dbi_server && @dbi_server.id) || :new} - title={@page_title} - current_user={@current_user} - action={@live_action} - dbi_server={@dbi_server} - patch={~p"/servers"} - /> - """ end diff --git a/lib/decentralised_book_index_web/live/dbi_server_live/show.ex b/lib/decentralised_book_index_web/live/dbi_server_live/show.ex index 25155c8..b98b376 100644 --- a/lib/decentralised_book_index_web/live/dbi_server_live/show.ex +++ b/lib/decentralised_book_index_web/live/dbi_server_live/show.ex @@ -8,7 +8,7 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Show do {@dbi_server.name} <:actions> - <.link patch={~p"/servers/#{@dbi_server}/show/edit"} phx-click={JS.push_focus()}> + <.link patch={~p"/servers/#{@dbi_server}/edit"} phx-click={JS.push_focus()}> <.edit_button> Edit @@ -25,23 +25,6 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Show do <.back navigate={~p"/servers"}>Back to servers - - <.modal - :if={@live_action == :edit} - id="dbi_server-modal" - show - on_cancel={JS.patch(~p"/servers/#{@dbi_server}")} - > - <.live_component - module={DecentralisedBookIndexWeb.DbiServerLive.FormComponent} - id={@dbi_server.id} - title={@page_title} - action={@live_action} - current_user={@current_user} - dbi_server={@dbi_server} - patch={~p"/servers/#{@dbi_server}"} - /> - """ end diff --git a/lib/decentralised_book_index_web/live/publisher_live/edit.ex b/lib/decentralised_book_index_web/live/publisher_live/edit.ex new file mode 100644 index 0000000..81400b8 --- /dev/null +++ b/lib/decentralised_book_index_web/live/publisher_live/edit.ex @@ -0,0 +1,56 @@ +defmodule DecentralisedBookIndexWeb.PublisherLive.Edit do + use DecentralisedBookIndexWeb, :live_view + + alias DecentralisedBookIndex.Metadata + + @impl true + def render(assigns) do + ~H""" + <.live_component + module={DecentralisedBookIndexWeb.PublisherLive.FormComponent} + id={(@publisher && @publisher.id) || :new} + title={@page_title} + current_user={@current_user} + action={@live_action} + publisher={@publisher} + /> + """ + end + + @impl true + def mount(_params, _session, socket) do + {:ok, + socket + |> assign_new(:current_user, fn -> nil end)} + end + + @impl true + def handle_params(params, _url, socket) do + socket = + socket + |> assign(:params, params) + |> apply_action(socket.assigns.live_action, params) + + {:noreply, socket} + end + + defp apply_action(socket, :edit, %{"id" => id}) do + socket + |> assign(:page_title, "Edit Publisher") + |> assign( + :publisher, + Ash.get!(DecentralisedBookIndex.Metadata.Publisher, id, actor: socket.assigns.current_user) + ) + end + + defp apply_action(socket, :new, _params) do + socket + |> assign(:page_title, "New Publisher") + |> assign(:publisher, nil) + end + + @impl true + def handle_info({DecentralisedBookIndexWeb.AuthorLive.FormComponent, {:saved, author}}, socket) do + {:noreply, socket} + end +end diff --git a/lib/decentralised_book_index_web/live/publisher_live/form_component.ex b/lib/decentralised_book_index_web/live/publisher_live/form_component.ex index d58b63d..d04cdbb 100644 --- a/lib/decentralised_book_index_web/live/publisher_live/form_component.ex +++ b/lib/decentralised_book_index_web/live/publisher_live/form_component.ex @@ -27,6 +27,9 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.FormComponent do <.save_button phx-disable-with="Saving..."> Save + <.cancel_button phx-click="cancel" phx-target={@myself}> + Cancel + @@ -55,7 +58,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.FormComponent do socket = socket |> put_flash(:info, "Publisher #{socket.assigns.form.source.type}d successfully") - |> push_patch(to: socket.assigns.patch) + |> push_redirect(to: patch_url(socket.assigns.action, publisher.id)) {:noreply, socket} @@ -64,6 +67,17 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.FormComponent do end end + def handle_event("cancel", _params, socket) do + publisher = socket.assigns.publisher + publisher_id = if publisher == nil, do: nil, else: publisher.id + + socket = + socket + |> push_redirect(to: patch_url(socket.assigns.action, publisher_id)) + + {:noreply, socket} + end + defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) defp assign_form(%{assigns: %{publisher: publisher}} = socket) do @@ -82,4 +96,12 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.FormComponent do assign(socket, form: to_form(form)) end + + defp patch_url(action, publisher_id) do + case action do + :edit -> ~p"/publishers/#{publisher_id}" + :new -> ~p"/publishers" + _ -> ~p"/publishers" + end + end end diff --git a/lib/decentralised_book_index_web/live/publisher_live/index.ex b/lib/decentralised_book_index_web/live/publisher_live/index.ex index a0f365f..33422ce 100644 --- a/lib/decentralised_book_index_web/live/publisher_live/index.ex +++ b/lib/decentralised_book_index_web/live/publisher_live/index.ex @@ -55,23 +55,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do /> <% end %> - - <.modal - :if={@live_action in [:new, :edit]} - id="publisher-modal" - show - on_cancel={JS.patch(~p"/publishers")} - > - <.live_component - module={DecentralisedBookIndexWeb.PublisherLive.FormComponent} - id={(@publisher && @publisher.id) || :new} - title={@page_title} - current_user={@current_user} - action={@live_action} - publisher={@publisher} - patch={~p"/publishers"} - /> - """ end @@ -105,7 +88,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do |> apply_action(socket.assigns.live_action, params) {:noreply, socket} - #apply_action(socket, socket.assigns.live_action, params)} end defp apply_action(socket, :edit, %{"id" => id}) do @@ -137,16 +119,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do {:noreply, stream_insert(socket, :publishers, publisher)} end - @impl true - def handle_event("delete", %{"id" => id}, socket) do - publisher = - Ash.get!(DecentralisedBookIndex.Metadata.Publisher, id, actor: socket.assigns.current_user) - - Ash.destroy!(publisher, actor: socket.assigns.current_user) - - {:noreply, stream_delete(socket, :publishers, publisher)} - end - defp sort_options do [ {"Sort by name", "name"}, diff --git a/lib/decentralised_book_index_web/live/publisher_live/show.ex b/lib/decentralised_book_index_web/live/publisher_live/show.ex index 7d77275..6ff08d4 100644 --- a/lib/decentralised_book_index_web/live/publisher_live/show.ex +++ b/lib/decentralised_book_index_web/live/publisher_live/show.ex @@ -9,7 +9,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Show do <:subtitle>This is a publisher record from your database. <:actions> - <.link patch={~p"/publishers/#{@publisher}/show/edit"} phx-click={JS.push_focus()}> + <.link patch={~p"/publishers/#{@publisher}/edit"} phx-click={JS.push_focus()}> <.edit_button> Edit @@ -24,23 +24,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Show do <.back navigate={~p"/publishers"}>Back to publishers - - <.modal - :if={@live_action == :edit} - id="publisher-modal" - show - on_cancel={JS.patch(~p"/publishers/#{@publisher}")} - > - <.live_component - module={DecentralisedBookIndexWeb.PublisherLive.FormComponent} - id={@publisher.id} - title={@page_title} - action={@live_action} - current_user={@current_user} - publisher={@publisher} - patch={~p"/publishers/#{@publisher}"} - /> - """ end diff --git a/lib/decentralised_book_index_web/router.ex b/lib/decentralised_book_index_web/router.ex index cc426fd..1c2175d 100644 --- a/lib/decentralised_book_index_web/router.ex +++ b/lib/decentralised_book_index_web/router.ex @@ -43,20 +43,18 @@ defmodule DecentralisedBookIndexWeb.Router do live "/authors/:id/edit", AuthorLive.Edit, :edit live "/publishers", PublisherLive.Index, :index - live "/publishers/new", PublisherLive.Index, :new - live "/publishers/:id/edit", PublisherLive.Index, :edit + live "/publishers/new", PublisherLive.Edit, :new + live "/publishers/:id/edit", PublisherLive.Edit, :edit live "/publishers/:id", PublisherLive.Show, :show - live "/publishers/:id/show/edit", PublisherLive.Show, :edit end ash_authentication_live_session :admin_authenticated_routes, on_mount: {DecentralisedBookIndexWeb.LiveUserAuth, :admin_required} do live "/servers", DbiServerLive.Index, :index - live "/servers/new", DbiServerLive.Index, :new - live "/servers/:id/edit", DbiServerLive.Index, :edit + live "/servers/new", DbiServerLive.Edit, :new + live "/servers/:id/edit", DbiServerLive.Edit, :edit live "/servers/:id", DbiServerLive.Show, :show - live "/servers/:id/show/edit", DbiServerLive.Show, :edit live "/users", UserLive.Index, :index live "/users/:id", UserLive.Show, :show