Add the separated Edit LiveViews for Publisher and DBIServer.
This commit is contained in:
@@ -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
|
||||
@@ -26,6 +26,9 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.FormComponent do
|
||||
<.save_button phx-disable-with="Saving...">
|
||||
Save
|
||||
</.save_button>
|
||||
<.cancel_button phx-click="cancel" phx-target={@myself}>
|
||||
Cancel
|
||||
</.cancel_button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
@@ -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
|
||||
|
||||
@@ -67,26 +67,6 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
|
||||
/>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="pt-2">
|
||||
</div>
|
||||
|
||||
<.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"}
|
||||
/>
|
||||
</.modal>
|
||||
"""
|
||||
end
|
||||
|
||||
|
||||
@@ -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
|
||||
</.edit_button>
|
||||
@@ -25,23 +25,6 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Show do
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/servers"}>Back to servers</.back>
|
||||
|
||||
<.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}"}
|
||||
/>
|
||||
</.modal>
|
||||
"""
|
||||
end
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -27,6 +27,9 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.FormComponent do
|
||||
<.save_button phx-disable-with="Saving...">
|
||||
Save
|
||||
</.save_button>
|
||||
<.cancel_button phx-click="cancel" phx-target={@myself}>
|
||||
Cancel
|
||||
</.cancel_button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
@@ -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
|
||||
|
||||
@@ -55,23 +55,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do
|
||||
/>
|
||||
</div>
|
||||
<% 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"}
|
||||
/>
|
||||
</.modal>
|
||||
"""
|
||||
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"},
|
||||
|
||||
@@ -9,7 +9,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Show do
|
||||
<:subtitle>This is a publisher record from your database.</:subtitle>
|
||||
|
||||
<: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
|
||||
</.edit_button>
|
||||
@@ -24,23 +24,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Show do
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/publishers"}>Back to publishers</.back>
|
||||
|
||||
<.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}"}
|
||||
/>
|
||||
</.modal>
|
||||
"""
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user