Add the search for Users Index LiveView.
This commit is contained in:
@@ -282,6 +282,7 @@ defmodule DecentralisedBookIndex.Accounts.User do
|
|||||||
attribute :role, DecentralisedBookIndex.Accounts.Role do
|
attribute :role, DecentralisedBookIndex.Accounts.Role do
|
||||||
allow_nil? false
|
allow_nil? false
|
||||||
default :user
|
default :user
|
||||||
|
public? true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents do
|
|||||||
import MyComponents.AuthorSearch, only: [author_search: 1]
|
import MyComponents.AuthorSearch, only: [author_search: 1]
|
||||||
|
|
||||||
import MyComponents.SearchResources, only: [search_resources: 1]
|
import MyComponents.SearchResources, only: [search_resources: 1]
|
||||||
|
import MyComponents.JustSearchResources, only: [just_search_resources: 1]
|
||||||
|
|
||||||
import MyComponents.Pagination, only: [pagination: 1]
|
import MyComponents.Pagination, only: [pagination: 1]
|
||||||
import MyComponents.Buttons, only: [
|
import MyComponents.Buttons, only: [
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
defmodule DecentralisedBookIndexWeb.Components.MyComponents.JustSearchResources do
|
||||||
|
use Phoenix.Component
|
||||||
|
use DecentralisedBookIndexWeb, :verified_routes
|
||||||
|
|
||||||
|
alias DecentralisedBookIndex.Accounts.Role
|
||||||
|
|
||||||
|
attr :search_query, :string, default: ""
|
||||||
|
attr :select_options, :list, required: true
|
||||||
|
attr :selected_option, :string, required: true
|
||||||
|
attr :current_user, :map, default: nil
|
||||||
|
|
||||||
|
attr :search_placeholder, :string, default: "Search by name"
|
||||||
|
|
||||||
|
def just_search_resources(assigns) do
|
||||||
|
~H"""
|
||||||
|
<div class="relative bg-white shadow-md dark:bg-gray-800 sm:rounded-lg">
|
||||||
|
<div class="flex flex-col items-center justify-between p-4 space-y-3 md:flex-row md:space-y-0 md:space-x-4">
|
||||||
|
<div class="w-full grow">
|
||||||
|
<form class="flex items-center" phx-submit="search">
|
||||||
|
<label for="simple-search" class="sr-only">Search</label>
|
||||||
|
<div class="relative w-full">
|
||||||
|
<div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
|
||||||
|
<svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor" viewbox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<input name="query" type="text" id="simple-search" class="block w-full p-2 pl-10 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" placeholder={@search_placeholder} value={@search_query}>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col items-stretch justify-end flex-shrink-0 w-full space-y-2 md:w-auto md:flex-row md:space-y-0 md:items-center md:space-x-3">
|
||||||
|
<div class="flex items-center w-full space-x-3 md:w-auto">
|
||||||
|
<form phx-change="change-sort">
|
||||||
|
<select name="sort_by" id="sort_by" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
|
||||||
|
<%= for {name, value} <- @select_options do %>
|
||||||
|
<%= if value == @selected_option do %>
|
||||||
|
<option value={value} selected>{name}</option>
|
||||||
|
<% else %>
|
||||||
|
<option value={value}>{name}</option>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
</select>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -11,35 +11,64 @@ defmodule DecentralisedBookIndexWeb.UserLive.Index do
|
|||||||
</.header>
|
</.header>
|
||||||
|
|
||||||
<div class="pt-2">
|
<div class="pt-2">
|
||||||
<.table
|
<.just_search_resources
|
||||||
id="users"
|
search_query={@search_query}
|
||||||
rows={@users}
|
select_options={@select_options}
|
||||||
row_click={fn {_id, user} -> JS.navigate(~p"/users/#{user}") end}
|
selected_option={@sort_by}
|
||||||
>
|
current_user={@current_user}
|
||||||
<:col :let={{_id, user}} label="Email">{user.email}</:col>
|
search_placeholder="Search by email"
|
||||||
|
/>
|
||||||
<:col :let={{_id, user}} label="Role">{user.role}</:col>
|
|
||||||
|
|
||||||
<:action :let={{_id, user}}>
|
|
||||||
<div class="sr-only">
|
|
||||||
<.link navigate={~p"/users/#{user}"}>Show</.link>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<.link patch={~p"/users/#{user}/edit"}>Edit Role</.link>
|
|
||||||
</:action>
|
|
||||||
</.table>
|
|
||||||
</div>
|
</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 Users
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% else %>
|
||||||
|
<div class="pt-2 flex flex-col gap-2">
|
||||||
|
<.table
|
||||||
|
id="users"
|
||||||
|
rows={@users}
|
||||||
|
row_click={fn user -> JS.navigate(~p"/users/#{user}") end}
|
||||||
|
>
|
||||||
|
<:col :let={user} label="Email">{user.email}</:col>
|
||||||
|
|
||||||
|
<:col :let={user} label="Role">{user.role}</:col>
|
||||||
|
|
||||||
|
<:action :let={user}>
|
||||||
|
<div class="sr-only">
|
||||||
|
<.link navigate={~p"/users/#{user}"}>Show</.link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<.link patch={~p"/users/#{user}/edit"}>Edit Role</.link>
|
||||||
|
</:action>
|
||||||
|
</.table>
|
||||||
|
|
||||||
|
<.pagination
|
||||||
|
endpoint={~p"/servers"}
|
||||||
|
page={@page}
|
||||||
|
page_params={@page_params}
|
||||||
|
params={@params}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(params, _session, socket) do
|
def mount(params, _session, socket) do
|
||||||
search_query = Map.get(params, "query", "")
|
search_query = Map.get(params, "query", "")
|
||||||
sort_by = Map.get(params, "sort_by", "name")
|
sort_by = Map.get(params, "sort_by", "name") |> validate_sort_by()
|
||||||
page_params = AshPhoenix.LiveView.page_from_params(params, 10)
|
page_params = AshPhoenix.LiveView.page_from_params(params, 10)
|
||||||
|
|
||||||
page = User.search!(
|
page = User.search!(
|
||||||
search_query,
|
search_query,
|
||||||
|
query: [sort_input: sort_by],
|
||||||
page: page_params ++ [count: true],
|
page: page_params ++ [count: true],
|
||||||
actor: socket.assigns.current_user
|
actor: socket.assigns.current_user
|
||||||
)
|
)
|
||||||
@@ -48,9 +77,10 @@ defmodule DecentralisedBookIndexWeb.UserLive.Index do
|
|||||||
socket
|
socket
|
||||||
|> assign(:sort_by, sort_by)
|
|> assign(:sort_by, sort_by)
|
||||||
|> assign(:search_query, search_query)
|
|> assign(:search_query, search_query)
|
||||||
|
|> assign(:select_options, sort_options())
|
||||||
|> assign(:page_params, page_params)
|
|> assign(:page_params, page_params)
|
||||||
|> assign(:page, page)
|
|> assign(:page, page)
|
||||||
|> assign(:users, user_row(page.results))
|
|> assign(:users, page.results)
|
||||||
|> assign(:params, params)
|
|> assign(:params, params)
|
||||||
|> apply_action(socket.assigns.live_action, params)
|
|> apply_action(socket.assigns.live_action, params)
|
||||||
|
|
||||||
@@ -67,7 +97,39 @@ defmodule DecentralisedBookIndexWeb.UserLive.Index do
|
|||||||
|> assign(:page_title, "Listing Users")
|
|> assign(:page_title, "Listing Users")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp user_row(users) do
|
defp sort_options do
|
||||||
Enum.map(users, fn user -> {user.id, user} end)
|
[
|
||||||
|
{"Sort by email", "email"},
|
||||||
|
{"Sort by role", "role"},
|
||||||
|
{"Sort by role desc", "-role"},
|
||||||
|
]
|
||||||
|
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"/users?#{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"/users?#{params}")}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user