Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions demo/lib/demo_web/filters/date_range.ex

This file was deleted.

13 changes: 0 additions & 13 deletions demo/lib/demo_web/filters/date_time_range.ex

This file was deleted.

2 changes: 1 addition & 1 deletion demo/lib/demo_web/filters/post_category_select.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule DemoWeb.Filters.PostCategorySelect do
@moduledoc """
Implementation of the `Backpex.Filters.MultiSelect` behaviour.
A reusable select filter module, see `Backpex.Filters.Select`.
"""

use Backpex.Filters.Select
Expand Down
13 changes: 0 additions & 13 deletions demo/lib/demo_web/filters/post_like_range.ex

This file was deleted.

28 changes: 0 additions & 28 deletions demo/lib/demo_web/filters/post_published.ex

This file was deleted.

30 changes: 0 additions & 30 deletions demo/lib/demo_web/filters/post_user_multi_select.ex

This file was deleted.

13 changes: 0 additions & 13 deletions demo/lib/demo_web/filters/product_quantity_range.ex

This file was deleted.

35 changes: 26 additions & 9 deletions demo/lib/demo_web/live/post_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ defmodule DemoWeb.PostLive do
persist: [:order, :filters, :columns, :metrics],
context_assigns: []

import Backpex.Expr, only: [expr: 1]
import Ecto.Query, warn: false

alias Backpex.LiveResource
alias Backpex.Metrics.Value
alias Backpex.Router
alias Demo.Post
alias Demo.Repo
alias DemoWeb.CategoryLive
alias DemoWeb.Filters.DateTimeRange
alias DemoWeb.Filters.PostCategorySelect
alias DemoWeb.Filters.PostLikeRange
alias DemoWeb.Filters.PostPublished
alias DemoWeb.Filters.PostUserMultiSelect
alias DemoWeb.Layouts
alias DemoWeb.TagLive
alias DemoWeb.UserLive
Expand All @@ -43,12 +42,15 @@ defmodule DemoWeb.PostLive do
label: "Category"
},
user_id: %{
module: PostUserMultiSelect,
label: "Users"
module: Backpex.Filters.MultiSelect,
label: "Users",
prompt: "Select users ...",
options: &user_options/1
},
likes: %{
module: PostLikeRange,
module: Backpex.Filters.Range,
label: "Likes",
type: :number,
presets: [
%{
label: "Over 100",
Expand All @@ -61,8 +63,9 @@ defmodule DemoWeb.PostLive do
]
},
inserted_at: %{
module: DateTimeRange,
module: Backpex.Filters.Range,
label: "Created at",
type: :datetime,
presets: [
%{
label: "Last 7 Days",
Expand Down Expand Up @@ -94,8 +97,12 @@ defmodule DemoWeb.PostLive do
]
},
published: %{
module: PostPublished,
module: Backpex.Filters.Boolean,
label: "Published?",
options: [
%{label: "Published", key: "published", predicate: expr(published == true)},
%{label: "Not published", key: "not_published", predicate: expr(published == false)}
],
default: ["published"],
presets: [
%{
Expand All @@ -115,6 +122,16 @@ defmodule DemoWeb.PostLive do
]
end

defp user_options(_assigns) do
query =
from p in Post,
join: u in assoc(p, :user),
distinct: p.user_id,
select: {fragment("? || ' ' || ?", u.first_name, u.last_name), u.id}

Repo.all(query)
end

@impl LiveResource
def fields do
[
Expand Down
6 changes: 3 additions & 3 deletions demo/lib/demo_web/live/product_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule DemoWeb.ProductLive do
alias Backpex.Metrics.Value
alias Demo.Supplier
alias DemoWeb.Endpoint
alias DemoWeb.Filters.ProductQuantityRange
alias DemoWeb.Layouts
alias DemoWeb.ShortLinkLive
alias Phoenix.VerifiedRoutes
Expand All @@ -32,8 +31,9 @@ defmodule DemoWeb.ProductLive do
def filters do
[
quantity: %{
module: ProductQuantityRange,
label: "QTY"
module: Backpex.Filters.Range,
label: "QTY",
type: :number
}
]
end
Expand Down
22 changes: 22 additions & 0 deletions demo/lib/demo_web/live/user_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ defmodule DemoWeb.UserLive do
init_order: &__MODULE__.init_order/1,
persist: [:order, :filters, :columns, :metrics]

import Backpex.Expr, only: [expr: 1]
import Ecto.Query, warn: false

alias Backpex.Filters.Select
alias Backpex.Metrics.Value

@impl Backpex.LiveResource
Expand Down Expand Up @@ -58,6 +60,26 @@ defmodule DemoWeb.UserLive do
|> Enum.concat(user_soft_delete: %{module: DemoWeb.ItemActions.UserSoftDelete})
end

@impl Backpex.LiveResource
def filters do
[
role: %{
module: Select,
label: "Role",
options: [{"User", "user"}, {"Admin", "admin"}]
},
age: %{
module: Select,
label: "Age",
options: [{"Under 18", "minor"}, {"18 and older", "adult"}],
condition: fn
{field, _config}, "minor", _assigns -> expr(ref(^field) < 18)
{field, _config}, "adult", _assigns -> expr(ref(^field) >= 18)
end
}
]
end

@impl Backpex.LiveResource
def panels do
[names: "Names"]
Expand Down
59 changes: 59 additions & 0 deletions demo/test/demo_web/live/user/filter_live_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
defmodule DemoWeb.Live.User.FilterLiveTest do
use DemoWeb.ConnCase, async: false

import Demo.EctoFactory
import Phoenix.LiveViewTest

describe "select filter configured in the LiveResource" do
test "renders the options of its configuration", %{conn: conn} do
conn
|> visit(~p"/admin/users")
|> assert_has("select[name='filters[role]'] option", text: "Select an option...")
|> assert_has("select[name='filters[role]'] option[value='admin']", text: "Admin")
|> assert_has("select[name='filters[age]'] option[value='minor']", text: "Under 18")
end

test "filters users by an enum value", %{conn: conn} do
insert(:user, first_name: "Paula", last_name: "Plain", role: :user)
insert(:user, first_name: "Ada", last_name: "Admin", role: :admin)

conn
|> visit(~p"/admin/users")
|> assert_has("table tbody tr", count: 2)
|> unwrap(fn view ->
view
|> form("form[phx-change='change-filter']", filters: %{role: "admin"})
|> render_change()
end)
|> assert_has("table tbody tr", count: 1)
|> assert_has("td", text: "Ada Admin")
|> refute_has("td", text: "Paula Plain")
end

test "rejects a value that is not an option", %{conn: conn} do
insert(:user, role: :user)

conn
|> visit(~p"/admin/users?#{%{"filters" => %{"role" => "owner"}}}")
|> assert_has("table tbody tr", count: 1)
|> assert_has("#filter-form", text: "is not a valid option")
end
end

describe "condition option" do
test "keeps the items that match the condition of the selected option", %{conn: conn} do
insert(:user, first_name: "Yuri", last_name: "Young", age: 16)
insert(:user, first_name: "Ada", last_name: "Adult", age: 30)

conn
|> visit(~p"/admin/users?#{%{"filters" => %{"age" => "minor"}}}")
|> assert_has("table tbody tr", count: 1)
|> assert_has("td", text: "Yuri Young")
|> refute_has("td", text: "Ada Adult")
|> visit(~p"/admin/users?#{%{"filters" => %{"age" => "adult"}}}")
|> assert_has("table tbody tr", count: 1)
|> assert_has("td", text: "Ada Adult")
|> refute_has("td", text: "Yuri Young")
end
end
end
Loading
Loading