|
|
|
@ -1,45 +1,73 @@
|
|
|
|
|
defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
|
|
|
|
|
use DecentralisedBookIndexWeb, :live_view
|
|
|
|
|
|
|
|
|
|
alias DecentralisedBookIndex.Metadata
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def render(assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<.header>
|
|
|
|
|
Listing servers
|
|
|
|
|
<:actions>
|
|
|
|
|
<.link patch={~p"/servers/new"}>
|
|
|
|
|
<.button>New server</.button>
|
|
|
|
|
</.link>
|
|
|
|
|
</:actions>
|
|
|
|
|
</.header>
|
|
|
|
|
|
|
|
|
|
<div class="pt-2">
|
|
|
|
|
<.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>
|
|
|
|
|
<.search_resources
|
|
|
|
|
search_query={@search_query}
|
|
|
|
|
select_options={@select_options}
|
|
|
|
|
selected_option={@sort_by}
|
|
|
|
|
current_user={@current_user}
|
|
|
|
|
resource_type="server"
|
|
|
|
|
resource_new_url={~p"/servers/new"}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<%= if Enum.empty?(@page.results) do %>
|
|
|
|
|
<div class="flex justify-center ">
|
|
|
|
|
<div>
|
|
|
|
|
<p class="text-xl font-semibold py-5 dark:text-white">
|
|
|
|
|
No Servers
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<% else %>
|
|
|
|
|
<div class="pt-2 flex flex-col gap-2">
|
|
|
|
|
<.table
|
|
|
|
|
id="dbi_servers"
|
|
|
|
|
rows={@page.results}
|
|
|
|
|
row_click={fn dbi_server -> JS.navigate(~p"/servers/#{dbi_server}") end}
|
|
|
|
|
>
|
|
|
|
|
<:col :let={dbi_server} label="Name">{dbi_server.name}</:col>
|
|
|
|
|
|
|
|
|
|
<:col :let={dbi_server} label="Url">{dbi_server.url}</:col>
|
|
|
|
|
|
|
|
|
|
<:action :let={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={dbi_server}>
|
|
|
|
|
<.link
|
|
|
|
|
phx-click={JS.push("delete", value: %{id: dbi_server.id}) |> hide("##{dbi_server.id}")}
|
|
|
|
|
data-confirm="Are you sure?"
|
|
|
|
|
>
|
|
|
|
|
Delete
|
|
|
|
|
</.link>
|
|
|
|
|
</:action>
|
|
|
|
|
</.table>
|
|
|
|
|
|
|
|
|
|
<.pagination
|
|
|
|
|
endpoint={~p"/servers"}
|
|
|
|
|
page={@page}
|
|
|
|
|
page_params={@page_params}
|
|
|
|
|
params={@params}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
|
<div class="pt-2">
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<.modal
|
|
|
|
@ -74,7 +102,29 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def handle_params(params, _url, socket) do
|
|
|
|
|
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
|
|
|
|
search_query = Map.get(params, "query", "")
|
|
|
|
|
sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by()
|
|
|
|
|
page_params = AshPhoenix.LiveView.page_from_params(params, 10)
|
|
|
|
|
|
|
|
|
|
page = Metadata.search_dbi_server!(
|
|
|
|
|
search_query,
|
|
|
|
|
query: [sort_input: sort_by],
|
|
|
|
|
page: page_params ++ [count: true],
|
|
|
|
|
actor: socket.assigns.current_user
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
socket =
|
|
|
|
|
socket
|
|
|
|
|
|> assign(:sort_by, sort_by)
|
|
|
|
|
|> assign(:search_query, search_query)
|
|
|
|
|
|> assign(:select_options, sort_options())
|
|
|
|
|
|> assign(:page_params, page_params)
|
|
|
|
|
|> assign(:page, page)
|
|
|
|
|
|> assign(:params, params)
|
|
|
|
|
|> apply_action(socket.assigns.live_action, params)
|
|
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
|
# {:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp apply_action(socket, :edit, %{"id" => id}) do
|
|
|
|
@ -115,4 +165,40 @@ defmodule DecentralisedBookIndexWeb.DbiServerLive.Index do
|
|
|
|
|
|
|
|
|
|
{:noreply, stream_delete(socket, :dbi_servers, dbi_server)}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp sort_options do
|
|
|
|
|
[
|
|
|
|
|
{"Sort by name", "name"},
|
|
|
|
|
{"Sort by recently updated", "-updated_at"},
|
|
|
|
|
{"Sort by recently added", "-inserted_at"},
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validate_sort_by(key) do
|
|
|
|
|
valid_keys = Enum.map(sort_options(), &elem(&1, 1))
|
|
|
|
|
|
|
|
|
|
if key in valid_keys do
|
|
|
|
|
key
|
|
|
|
|
else
|
|
|
|
|
List.first(valid_keys)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def handle_event("change-sort", %{"sort_by" => sort_by}, socket) do
|
|
|
|
|
params =
|
|
|
|
|
socket.assigns.params
|
|
|
|
|
|> Map.put("sort_by", sort_by)
|
|
|
|
|
|
|
|
|
|
{:noreply, push_patch(socket, to: ~p"/servers?#{params}")}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def handle_event("search", %{"query" => query}, socket) do
|
|
|
|
|
params =
|
|
|
|
|
socket.assigns.params
|
|
|
|
|
|> Map.put("query", query)
|
|
|
|
|
|
|
|
|
|
{:noreply, push_patch(socket, to: ~p"/servers?#{params}")}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|