2021-03-05 19:23:32 -05:00
|
|
|
defmodule Shift73kWeb.UserManagementLive.Index do
|
|
|
|
use Shift73kWeb, :live_view
|
2021-03-04 22:03:27 -05:00
|
|
|
|
|
|
|
import Ecto.Query
|
2021-03-05 19:23:32 -05:00
|
|
|
import Shift73kWeb.Pagination
|
|
|
|
import Shift73k.Util.Dt
|
2021-03-04 22:03:27 -05:00
|
|
|
|
2021-03-05 19:23:32 -05:00
|
|
|
alias Shift73k.Repo
|
|
|
|
alias Shift73k.Accounts
|
|
|
|
alias Shift73k.Accounts.User
|
|
|
|
alias Shift73kWeb.Roles
|
2021-03-04 22:03:27 -05:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def mount(_params, session, socket) do
|
|
|
|
socket
|
|
|
|
|> assign_defaults(session)
|
2021-03-05 15:32:01 -05:00
|
|
|
|> assign(:page, nil)
|
2021-03-04 22:03:27 -05:00
|
|
|
|> live_okreply()
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_params(params, _url, socket) do
|
|
|
|
current_user = socket.assigns.current_user
|
|
|
|
live_action = socket.assigns.live_action
|
|
|
|
user = user_from_params(params)
|
|
|
|
|
|
|
|
if Roles.can?(current_user, user, live_action) do
|
|
|
|
socket
|
|
|
|
|> assign(:query, query_map(params))
|
|
|
|
|> assign_modal_return_to()
|
2021-03-05 15:32:01 -05:00
|
|
|
|> assign(:delete_user, nil)
|
|
|
|
|> assign(:page, nil)
|
|
|
|
|> request_page_query()
|
2021-03-04 22:03:27 -05:00
|
|
|
|> apply_action(socket.assigns.live_action, params)
|
|
|
|
|> live_noreply()
|
|
|
|
else
|
|
|
|
socket
|
|
|
|
|> put_flash(:error, "Unauthorised")
|
|
|
|
|> redirect(to: "/")
|
|
|
|
|> live_noreply()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-05 15:32:01 -05:00
|
|
|
defp request_page_query(%{assigns: %{query: query}} = socket) do
|
|
|
|
send(self(), {:do_page_query, query})
|
|
|
|
socket
|
|
|
|
end
|
|
|
|
|
2021-03-04 22:03:27 -05:00
|
|
|
defp apply_action(socket, :index, _params) do
|
|
|
|
socket
|
|
|
|
|> assign(:page_title, "Listing Users")
|
|
|
|
|> assign(:user, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :new, _params) do
|
|
|
|
socket
|
|
|
|
|> assign(:page_title, "New User")
|
|
|
|
|> assign(:user, %User{})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :edit, %{"id" => id}) do
|
|
|
|
socket
|
|
|
|
|> assign(:page_title, "Edit User")
|
|
|
|
|> assign(:user, Accounts.get_user!(id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_modal_return_to(%{assigns: %{query: query}} = socket) do
|
|
|
|
to = Routes.user_management_index_path(socket, :index, Enum.into(query, []))
|
|
|
|
assign(socket, :modal_return_to, to)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp user_from_params(params)
|
|
|
|
|
|
|
|
defp user_from_params(%{"id" => id}),
|
|
|
|
do: Accounts.get_user!(id)
|
|
|
|
|
|
|
|
defp user_from_params(_params), do: %User{}
|
|
|
|
|
|
|
|
def query_map(params) do
|
|
|
|
%{
|
|
|
|
filter: params["filter"] || "",
|
|
|
|
sort_by: (params["sort_by"] in ~w(email inserted_at role) && params["sort_by"]) || "email",
|
|
|
|
sort_order: (params["sort_order"] == "desc" && "desc") || "asc",
|
|
|
|
page_number: String.to_integer(params["page_number"] || "1"),
|
|
|
|
page_size: String.to_integer(params["page_size"] || "10")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-03-05 15:32:01 -05:00
|
|
|
defp page_query(query) do
|
|
|
|
from(u in User)
|
|
|
|
|> or_where([u], ilike(u.email, ^"%#{query.filter}%"))
|
|
|
|
|> order_by([u], [
|
|
|
|
{^String.to_existing_atom(query.sort_order), ^String.to_existing_atom(query.sort_by)}
|
|
|
|
])
|
|
|
|
|> Repo.paginate(page: query.page_number, page_size: query.page_size)
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2021-03-05 15:32:01 -05:00
|
|
|
def handle_event("delete-modal", %{"id" => id}, socket) do
|
2021-03-09 20:55:59 -05:00
|
|
|
{:noreply, assign(socket, :delete_user, Accounts.get_user!(id))}
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("filter-change", params, socket) do
|
|
|
|
send(self(), {:query_update, Map.put(params, "page_number", "1")})
|
2021-03-05 15:32:01 -05:00
|
|
|
{:noreply, socket}
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("filter-clear", _params, socket) do
|
|
|
|
send(self(), {:query_update, %{"filter" => "", "page_number" => "1"}})
|
2021-03-05 15:32:01 -05:00
|
|
|
{:noreply, socket}
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event(
|
|
|
|
"sort-change",
|
|
|
|
%{"sort_by" => column} = params,
|
|
|
|
%{assigns: %{query: query}} = socket
|
|
|
|
) do
|
|
|
|
(column == query.sort_by &&
|
|
|
|
send(
|
|
|
|
self(),
|
|
|
|
{:query_update, %{"sort_order" => (query.sort_order == "asc" && "desc") || "asc"}}
|
|
|
|
)) ||
|
|
|
|
send(self(), {:query_update, Map.put(params, "sort_order", "asc")})
|
|
|
|
|
2021-03-05 15:32:01 -05:00
|
|
|
{:noreply, socket}
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("sort-by-change", %{"sort" => params}, socket) do
|
|
|
|
send(self(), {:query_update, params})
|
2021-03-05 15:32:01 -05:00
|
|
|
{:noreply, socket}
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("sort-order-change", _params, socket) do
|
|
|
|
new_sort_order = (socket.assigns.query.sort_order == "asc" && "desc") || "asc"
|
|
|
|
send(self(), {:query_update, %{"sort_order" => new_sort_order}})
|
2021-03-05 15:32:01 -05:00
|
|
|
{:noreply, socket}
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("page-change", params, socket) do
|
|
|
|
send(self(), {:query_update, params})
|
2021-03-05 15:32:01 -05:00
|
|
|
{:noreply, socket}
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("page-size-change", params, socket) do
|
|
|
|
send(self(), {:query_update, Map.put(params, "page_number", "1")})
|
2021-03-05 15:32:01 -05:00
|
|
|
{:noreply, socket}
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info({:query_update, params}, %{assigns: %{query: q}} = socket) do
|
|
|
|
{:noreply,
|
|
|
|
push_patch(socket,
|
|
|
|
to: Routes.user_management_index_path(socket, :index, get_new_params(params, q)),
|
|
|
|
replace: true
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
|
2021-03-05 15:32:01 -05:00
|
|
|
@impl true
|
|
|
|
def handle_info({:do_page_query, query}, socket) do
|
|
|
|
{:noreply, assign(socket, :page, page_query(query))}
|
|
|
|
end
|
|
|
|
|
2021-03-04 22:03:27 -05:00
|
|
|
@impl true
|
|
|
|
def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do
|
|
|
|
socket |> copy_flash() |> push_patch(to: to) |> live_noreply()
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info({:put_flash_message, {flash_type, msg}}, socket) do
|
|
|
|
socket |> put_flash(flash_type, msg) |> live_noreply()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_new_params(params, query) do
|
|
|
|
[
|
|
|
|
{:filter, Map.get(params, "filter") || query.filter},
|
|
|
|
{:sort_by, Map.get(params, "sort_by") || query.sort_by},
|
|
|
|
{:sort_order, Map.get(params, "sort_order") || query.sort_order},
|
|
|
|
{:page_number, Map.get(params, "page_number") || query.page_number},
|
|
|
|
{:page_size, Map.get(params, "page_size") || query.page_size}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-03-06 00:42:13 -05:00
|
|
|
def dt_out(ndt), do: format_ndt(ndt, "%Y %b %d, %I:%M %p")
|
2021-03-04 22:03:27 -05:00
|
|
|
end
|