Add LiveViews for DBI Servers.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
e1169c1d82
commit
b49b7c954c
@ -0,0 +1,83 @@
|
|||||||
|
defmodule DecentralisedBookIndexWeb.DbiServerLive.FormComponent do
|
||||||
|
use DecentralisedBookIndexWeb, :live_component
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def render(assigns) do
|
||||||
|
~H"""
|
||||||
|
<div>
|
||||||
|
<.header>
|
||||||
|
{@title}
|
||||||
|
<:subtitle>Use this form to manage servers records in your database.</:subtitle>
|
||||||
|
</.header>
|
||||||
|
|
||||||
|
<.simple_form
|
||||||
|
for={@form}
|
||||||
|
id="dbi_server-form"
|
||||||
|
phx-target={@myself}
|
||||||
|
phx-change="validate"
|
||||||
|
phx-submit="save"
|
||||||
|
>
|
||||||
|
<.input field={@form[:name]} type="text" label="Name" /><.input
|
||||||
|
field={@form[:url]}
|
||||||
|
type="text"
|
||||||
|
label="Url"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<:actions>
|
||||||
|
<.button phx-disable-with="Saving...">Save server</.button>
|
||||||
|
</:actions>
|
||||||
|
</.simple_form>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def update(assigns, socket) do
|
||||||
|
{:ok,
|
||||||
|
socket
|
||||||
|
|> assign(assigns)
|
||||||
|
|> assign_form()}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_event("validate", %{"dbi_server" => dbi_server_params}, socket) do
|
||||||
|
{:noreply,
|
||||||
|
assign(socket, form: AshPhoenix.Form.validate(socket.assigns.form, dbi_server_params))}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("save", %{"dbi_server" => dbi_server_params}, socket) do
|
||||||
|
case AshPhoenix.Form.submit(socket.assigns.form, params: dbi_server_params) do
|
||||||
|
{:ok, dbi_server} ->
|
||||||
|
notify_parent({:saved, dbi_server})
|
||||||
|
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> put_flash(:info, "Server #{socket.assigns.form.source.type}d successfully")
|
||||||
|
|> push_patch(to: socket.assigns.patch)
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
|
||||||
|
{:error, form} ->
|
||||||
|
{:noreply, assign(socket, form: form)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
||||||
|
|
||||||
|
defp assign_form(%{assigns: %{dbi_server: dbi_server}} = socket) do
|
||||||
|
form =
|
||||||
|
if dbi_server do
|
||||||
|
AshPhoenix.Form.for_update(dbi_server, :update,
|
||||||
|
as: "dbi_server",
|
||||||
|
actor: socket.assigns.current_user
|
||||||
|
)
|
||||||
|
else
|
||||||
|
AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.DBIServer, :create,
|
||||||
|
as: "dbi_server",
|
||||||
|
actor: socket.assigns.current_user
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
assign(socket, form: to_form(form))
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,116 @@
|
|||||||
|
defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
|
||||||
|
use DecentralisedBookIndexWeb, :live_view
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def render(assigns) do
|
||||||
|
~H"""
|
||||||
|
<.header>
|
||||||
|
Listing servers
|
||||||
|
<:actions>
|
||||||
|
<.link patch={~p"/servers/new"}>
|
||||||
|
<.button>New server</.button>
|
||||||
|
</.link>
|
||||||
|
</:actions>
|
||||||
|
</.header>
|
||||||
|
|
||||||
|
<.table
|
||||||
|
id="dbi_servers"
|
||||||
|
rows={@streams.dbi_servers}
|
||||||
|
row_click={fn {_id, dbi_server} -> JS.navigate(~p"/servers/#{dbi_server}") end}
|
||||||
|
>
|
||||||
|
<:col :let={{_id, dbi_server}} label="Name">{dbi_server.name}</:col>
|
||||||
|
|
||||||
|
<:col :let={{_id, dbi_server}} label="Url">{dbi_server.url}</:col>
|
||||||
|
|
||||||
|
<:action :let={{_id, dbi_server}}>
|
||||||
|
<div class="sr-only">
|
||||||
|
<.link navigate={~p"/servers/#{dbi_server}"}>Show</.link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<.link patch={~p"/servers/#{dbi_server}/edit"}>Edit</.link>
|
||||||
|
</:action>
|
||||||
|
|
||||||
|
<:action :let={{id, dbi_server}}>
|
||||||
|
<.link
|
||||||
|
phx-click={JS.push("delete", value: %{id: dbi_server.id}) |> hide("##{id}")}
|
||||||
|
data-confirm="Are you sure?"
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</.link>
|
||||||
|
</:action>
|
||||||
|
</.table>
|
||||||
|
|
||||||
|
<.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
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def mount(_params, _session, socket) do
|
||||||
|
{:ok,
|
||||||
|
socket
|
||||||
|
|> stream(
|
||||||
|
:dbi_servers,
|
||||||
|
Ash.read!(DecentralisedBookIndex.Metadata.DBIServer, actor: socket.assigns[:current_user])
|
||||||
|
)
|
||||||
|
|> assign_new(:current_user, fn -> nil end)}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_params(params, _url, socket) do
|
||||||
|
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||||
|
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
|
||||||
|
|
||||||
|
defp apply_action(socket, :index, _params) do
|
||||||
|
socket
|
||||||
|
|> assign(:page_title, "Listing servers")
|
||||||
|
|> assign(:dbi_server, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_info(
|
||||||
|
{DecentralisedBookIndexWeb.DbiServerLive.FormComponent, {:saved, dbi_server}},
|
||||||
|
socket
|
||||||
|
) do
|
||||||
|
{:noreply, stream_insert(socket, :dbi_servers, dbi_server)}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_event("delete", %{"id" => id}, socket) do
|
||||||
|
dbi_server =
|
||||||
|
Ash.get!(DecentralisedBookIndex.Metadata.DBIServer, id, actor: socket.assigns.current_user)
|
||||||
|
|
||||||
|
Ash.destroy!(dbi_server, actor: socket.assigns.current_user)
|
||||||
|
|
||||||
|
{:noreply, stream_delete(socket, :dbi_servers, dbi_server)}
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,65 @@
|
|||||||
|
defmodule DecentralisedBookIndexWeb.DbiServerLive.Show do
|
||||||
|
use DecentralisedBookIndexWeb, :live_view
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def render(assigns) do
|
||||||
|
~H"""
|
||||||
|
<.header>
|
||||||
|
Dbi server {@dbi_server.id}
|
||||||
|
<:subtitle>This is a dbi_server record from your database.</:subtitle>
|
||||||
|
|
||||||
|
<:actions>
|
||||||
|
<.link patch={~p"/dbi_servers/#{@dbi_server}/show/edit"} phx-click={JS.push_focus()}>
|
||||||
|
<.button>Edit dbi_server</.button>
|
||||||
|
</.link>
|
||||||
|
</:actions>
|
||||||
|
</.header>
|
||||||
|
|
||||||
|
<.list>
|
||||||
|
<:item title="Id">{@dbi_server.id}</:item>
|
||||||
|
|
||||||
|
<:item title="Name">{@dbi_server.name}</:item>
|
||||||
|
|
||||||
|
<:item title="Url">{@dbi_server.url}</:item>
|
||||||
|
</.list>
|
||||||
|
|
||||||
|
<.back navigate={~p"/dbi_servers"}>Back to dbi_servers</.back>
|
||||||
|
|
||||||
|
<.modal
|
||||||
|
:if={@live_action == :edit}
|
||||||
|
id="dbi_server-modal"
|
||||||
|
show
|
||||||
|
on_cancel={JS.patch(~p"/dbi_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"/dbi_servers/#{@dbi_server}"}
|
||||||
|
/>
|
||||||
|
</.modal>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def mount(_params, _session, socket) do
|
||||||
|
{:ok, socket}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_params(%{"id" => id}, _, socket) do
|
||||||
|
{:noreply,
|
||||||
|
socket
|
||||||
|
|> assign(:page_title, page_title(socket.assigns.live_action))
|
||||||
|
|> assign(
|
||||||
|
:dbi_server,
|
||||||
|
Ash.get!(DecentralisedBookIndex.Metadata.DBIServer, id, actor: socket.assigns.current_user)
|
||||||
|
)}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp page_title(:show), do: "Show Dbi server"
|
||||||
|
defp page_title(:edit), do: "Edit Dbi server"
|
||||||
|
end
|
Loading…
Reference in new issue