Add the separated Edit LiveViews for Publisher and DBIServer.

dev
KKlochko 2 months ago
parent 3ee8b2ed0b
commit d8587ca9b6

@ -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_button phx-disable-with="Saving...">
Save Save
</.save_button> </.save_button>
<.cancel_button phx-click="cancel" phx-target={@myself}>
Cancel
</.cancel_button>
</:actions> </:actions>
</.simple_form> </.simple_form>
</div> </div>
@ -54,7 +57,7 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.FormComponent do
socket = socket =
socket socket
|> put_flash(:info, "Server #{socket.assigns.form.source.type}d successfully") |> 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} {:noreply, socket}
@ -63,6 +66,17 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.FormComponent do
end end
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 notify_parent(msg), do: send(self(), {__MODULE__, msg})
defp assign_form(%{assigns: %{dbi_server: dbi_server}} = socket) do 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)) assign(socket, form: to_form(form))
end end
defp patch_url(action, server_id) do
case action do
:edit -> ~p"/servers/#{server_id}"
:new -> ~p"/servers"
_ -> ~p"/servers"
end
end
end end

@ -67,26 +67,6 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
/> />
</div> </div>
<% end %> <% 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 end

@ -8,7 +8,7 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Show do
{@dbi_server.name} {@dbi_server.name}
<:actions> <: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_button>
Edit Edit
</.edit_button> </.edit_button>
@ -25,23 +25,6 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Show do
</.list> </.list>
<.back navigate={~p"/servers"}>Back to servers</.back> <.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 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_button phx-disable-with="Saving...">
Save Save
</.save_button> </.save_button>
<.cancel_button phx-click="cancel" phx-target={@myself}>
Cancel
</.cancel_button>
</:actions> </:actions>
</.simple_form> </.simple_form>
</div> </div>
@ -55,7 +58,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.FormComponent do
socket = socket =
socket socket
|> put_flash(:info, "Publisher #{socket.assigns.form.source.type}d successfully") |> 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} {:noreply, socket}
@ -64,6 +67,17 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.FormComponent do
end end
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 notify_parent(msg), do: send(self(), {__MODULE__, msg})
defp assign_form(%{assigns: %{publisher: publisher}} = socket) do defp assign_form(%{assigns: %{publisher: publisher}} = socket) do
@ -82,4 +96,12 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.FormComponent do
assign(socket, form: to_form(form)) assign(socket, form: to_form(form))
end end
defp patch_url(action, publisher_id) do
case action do
:edit -> ~p"/publishers/#{publisher_id}"
:new -> ~p"/publishers"
_ -> ~p"/publishers"
end
end
end end

@ -55,23 +55,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do
/> />
</div> </div>
<% end %> <% 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 end
@ -105,7 +88,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do
|> apply_action(socket.assigns.live_action, params) |> apply_action(socket.assigns.live_action, params)
{:noreply, socket} {:noreply, socket}
#apply_action(socket, socket.assigns.live_action, params)}
end end
defp apply_action(socket, :edit, %{"id" => id}) do defp apply_action(socket, :edit, %{"id" => id}) do
@ -137,16 +119,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Index do
{:noreply, stream_insert(socket, :publishers, publisher)} {:noreply, stream_insert(socket, :publishers, publisher)}
end 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 defp sort_options do
[ [
{"Sort by name", "name"}, {"Sort by name", "name"},

@ -9,7 +9,7 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Show do
<:subtitle>This is a publisher record from your database.</:subtitle> <:subtitle>This is a publisher record from your database.</:subtitle>
<:actions> <: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_button>
Edit Edit
</.edit_button> </.edit_button>
@ -24,23 +24,6 @@ defmodule DecentralisedBookIndexWeb.PublisherLive.Show do
</.list> </.list>
<.back navigate={~p"/publishers"}>Back to publishers</.back> <.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 end

@ -43,20 +43,18 @@ defmodule DecentralisedBookIndexWeb.Router do
live "/authors/:id/edit", AuthorLive.Edit, :edit live "/authors/:id/edit", AuthorLive.Edit, :edit
live "/publishers", PublisherLive.Index, :index live "/publishers", PublisherLive.Index, :index
live "/publishers/new", PublisherLive.Index, :new live "/publishers/new", PublisherLive.Edit, :new
live "/publishers/:id/edit", PublisherLive.Index, :edit live "/publishers/:id/edit", PublisherLive.Edit, :edit
live "/publishers/:id", PublisherLive.Show, :show live "/publishers/:id", PublisherLive.Show, :show
live "/publishers/:id/show/edit", PublisherLive.Show, :edit
end end
ash_authentication_live_session :admin_authenticated_routes, on_mount: {DecentralisedBookIndexWeb.LiveUserAuth, :admin_required} do ash_authentication_live_session :admin_authenticated_routes, on_mount: {DecentralisedBookIndexWeb.LiveUserAuth, :admin_required} do
live "/servers", DbiServerLive.Index, :index live "/servers", DbiServerLive.Index, :index
live "/servers/new", DbiServerLive.Index, :new live "/servers/new", DbiServerLive.Edit, :new
live "/servers/:id/edit", DbiServerLive.Index, :edit live "/servers/:id/edit", DbiServerLive.Edit, :edit
live "/servers/:id", DbiServerLive.Show, :show live "/servers/:id", DbiServerLive.Show, :show
live "/servers/:id/show/edit", DbiServerLive.Show, :edit
live "/users", UserLive.Index, :index live "/users", UserLive.Index, :index
live "/users/:id", UserLive.Show, :show live "/users/:id", UserLive.Show, :show

Loading…
Cancel
Save