defmodule ActionRequestsDemoWeb.ActionRequestsLive do
use ActionRequestsDemoWeb, :live_view
alias ActionRequestsDemo.ActionRequests
def mount(_params, _session, socket) do
{:ok, assign(socket, current_user_id: 1)}
end
def handle_params(params, _uri, socket) do
case ActionRequests.list_action_requests(params, socket.assigns.current_user_id) do
{:ok, {action_requests, meta}} ->
{:noreply,
socket
|> assign(:action_requests, action_requests)
|> assign(:meta, meta)
|> assign(:params, params)}
{:error, _meta} ->
{:noreply,
socket
|> assign(:action_requests, [])
|> assign(:meta, %Flop.Meta{})
|> assign(:params, params)}
end
end
def handle_event("filter", params, socket) do
# Navigate with new params, which will trigger handle_params
{:noreply, push_patch(socket, to: ~p"/?#{params}")}
end
def handle_event("patch", params, socket) do
# Handle pagination events from paginator links
{:noreply, push_patch(socket, to: ~p"/?#{params}")}
end
def render(assigns) do
~H"""
<%= if request.delivery_scheduled_at do %>
{Calendar.strftime(request.delivery_scheduled_at, "%Y-%m-%d %H:%M")}
<% else %>
-
<% end %>
<% end %>
Page {@meta.current_page} of {@meta.total_pages} (showing {length(@action_requests)} of {@meta.total_count} records)
"""
end
defp page_window(current, total, radius) do
# Returns a list of page numbers centered around current page, excluding first/last
start_page = max(current - radius, 2)
end_page = min(current + radius, total - 1)
if end_page >= start_page do
Enum.to_list(start_page..end_page)
else
[]
end
end
defp status_badge(assigns) do
~H"""
{@status}
"""
end
defp sort_link(assigns) do
current_order =
if assigns.meta.flop.order_by == [assigns.field],
do: List.first(assigns.meta.flop.order_directions),
else: nil
next_direction = if current_order == :asc, do: :desc, else: :asc
assigns = assign(assigns, :next_direction, next_direction)
assigns = assign(assigns, :current_order, current_order)
~H"""
<.link
patch={
~p"/?#{Map.merge(@params, %{"order_by" => @field, "order_directions" => @next_direction})}"
}
class="group inline-flex"
>
{render_slot(@inner_block)}
<%= if @current_order == :asc do %>
↑
<% end %>
<%= if @current_order == :desc do %>
↓
<% end %>
"""
end
end