Skip to content

Commit

Permalink
do not use active_filter; instead, update filter in fetch_rows
Browse files Browse the repository at this point in the history
  • Loading branch information
bokner committed Apr 28, 2023
1 parent 235a6d5 commit e056334
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
17 changes: 9 additions & 8 deletions lib/phoenix/live_dashboard/components/table_component.ex
Expand Up @@ -10,8 +10,7 @@ defmodule Phoenix.LiveDashboard.TableComponent do
sort_by: :atom,
sort_dir: :desc | :asc,
search: binary(),
hint: binary() | nil,
filter: binary() | nil
hint: binary() | nil
}

@impl true
Expand All @@ -31,7 +30,6 @@ defmodule Phoenix.LiveDashboard.TableComponent do
|> validate_required_one_sortable_column()
|> Map.put_new(:search, true)
|> Map.put_new(:limit, @limit)
|> Map.put_new(:filter, nil)
|> Map.put_new(:row_attrs, [])
|> Map.put_new(:hint, nil)
|> Map.put_new(:dom_id, nil)
Expand Down Expand Up @@ -93,7 +91,7 @@ defmodule Phoenix.LiveDashboard.TableComponent do
defp fetch_rows(row_fetcher, table_params, page_node, socket)
when is_function(row_fetcher, 2) do
{rows, total} = row_fetcher.(table_params, page_node)
{rows, total, assign(socket, :active_filter, nil)}
{rows, total, assign(socket, :filter_list, nil)}
end

defp fetch_rows({row_fetcher, initial_state}, table_params, page_node, socket)
Expand All @@ -105,11 +103,14 @@ defmodule Phoenix.LiveDashboard.TableComponent do
{nil, nil, rows, total, state}
end

## Update `filter` in table_params
table_params = Map.put(table_params, :filter, active_filter)

{rows, total,
socket
|> assign(:row_fetcher_state, state)
|> assign(:filter_list, available_filters)
|> assign(:active_filter, active_filter)}
|> assign(:table_params, table_params)
|> assign(:filter_list, available_filters)}
end

defp normalize_table_params(assigns) do
Expand Down Expand Up @@ -205,13 +206,13 @@ defmodule Phoenix.LiveDashboard.TableComponent do
</div>
</form>
<form :if={@active_filter} phx-change="select_filter" phx-target={@myself} class="form-inline">
<form :if={@filter_list} phx-change="select_filter" phx-target={@myself} class="form-inline">
<div class="form-row align-items-center">
<div class="col-auto">Filter</div>
<div class="col-auto">
<div class="input-group input-group-sm">
<select name="filter" class="custom-select" id="filter-select">
<%= options_for_select(@filter_list, @active_filter) %>
<%= options_for_select(@filter_list, @table_params.filter) %>
</select>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion lib/phoenix/live_dashboard/page_builder.ex
Expand Up @@ -292,7 +292,6 @@ defmodule Phoenix.LiveDashboard.PageBuilder do

attr :hint, :string, default: nil, doc: "A textual hint to show close to the title."
attr :dom_id, :string, default: nil, doc: "id attribute for the HTML the main tag."
attr :filter, :string, default: nil, doc: "To filter table rows"

@spec live_table(assigns :: Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
def live_table(assigns) do
Expand Down
2 changes: 1 addition & 1 deletion lib/phoenix/live_dashboard/system_info.ex
Expand Up @@ -234,7 +234,7 @@ defmodule Phoenix.LiveDashboard.SystemInfo do
{active_filter, available_filters, processes, count, next_state}
end

def get_process_filter_data(filter) do
defp get_process_filter_data(filter) do
case Application.get_env(:phoenix_live_dashboard, :process_filter) do
nil ->
{nil, nil, Process.list()}
Expand Down
8 changes: 3 additions & 5 deletions test/phoenix/live_dashboard/system_info_test.exs
Expand Up @@ -271,7 +271,7 @@ defmodule Phoenix.LiveDashboard.SystemInfoTestSync do
{active_filter, available_filters, processes, count, _} =
SystemInfo.fetch_processes(node(), nil, "", :memory, :asc, 5000)

assert ProcessFilter.default_filter() == active_filter
assert "Phoenix" == active_filter
assert ProcessFilter.list() == available_filters
assert Enum.count(processes) == count

Expand All @@ -282,12 +282,10 @@ defmodule Phoenix.LiveDashboard.SystemInfoTestSync do
assert count > 1
end

test "process list has filtered entries" do
{active_filter, _available_filters, processes, count, _} =
test "process list has only filtered entries" do
{_active_filter, _available_filters, processes, count, _} =
SystemInfo.fetch_processes(node(), nil, "", :memory, :asc, 5000)

assert "Phoenix" == active_filter

assert count ==
Enum.count(processes, fn [_pid, {:name_or_initial_call, name} | _] ->
String.contains?(name, "Phoenix")
Expand Down

0 comments on commit e056334

Please sign in to comment.