Skip to content

Commit 2718401

Browse files
authored
Merge pull request #1978 from naymspace/feature/preserve-filters-changed-on-confirmed-item-actions
Preserve filters_changed flag in confirmed item action return URL
2 parents 5f971b2 + b9d950b commit 2718401

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

demo/test/demo_web/live/post/filter_live_test.exs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,57 @@ defmodule DemoWeb.Live.Post.FilterLiveTest do
356356
end
357357
end
358358

359+
describe "filters_changed preservation across confirmed item actions" do
360+
test "delete item action preserves cleared default filter", %{conn: conn} do
361+
# Insert without tags to avoid the posts_tags FK preventing the delete.
362+
insert(:post, title: "Published Post", published: true, tags: [])
363+
draft_to_keep = insert(:post, title: "Draft to Keep", published: false, tags: [])
364+
draft_to_delete = insert(:post, title: "Draft to Delete", published: false, tags: [])
365+
366+
session =
367+
conn
368+
|> visit(~p"/admin/posts")
369+
|> assert_has("table tbody tr", count: 1)
370+
|> assert_has("td", text: "Published Post")
371+
|> refute_has("td", text: "Draft to Keep")
372+
|> unwrap(fn view ->
373+
view
374+
|> element("button[phx-click='clear-filter'][phx-value-field='published'][aria-label]")
375+
|> render_click()
376+
end)
377+
|> assert_has("table tbody tr", count: 3)
378+
|> assert_has("td", text: "Draft to Keep")
379+
|> unwrap(fn view ->
380+
view
381+
|> element("button[aria-label='Delete'][phx-value-item-id='#{draft_to_delete.id}']")
382+
|> render_click()
383+
end)
384+
|> unwrap(fn view ->
385+
view
386+
|> form("#resource-form")
387+
|> render_submit()
388+
end)
389+
390+
# After the confirmed delete the form component runs `push_navigate(return_to)`,
391+
# which remounts the index. Without the fix the URL would lose `filters_changed`,
392+
# so `maybe_redirect_to_default_filters/1` would re-apply `filters[published]=published`
393+
# and the remaining draft post would disappear from the table.
394+
session
395+
|> assert_path(~p"/admin/posts",
396+
query_params: %{
397+
"filters_changed" => "true",
398+
"order_by" => "id",
399+
"order_direction" => "asc",
400+
"per_page" => "15",
401+
"page" => "1"
402+
}
403+
)
404+
|> assert_has("td", text: "Published Post")
405+
|> assert_has("td", text: draft_to_keep.title)
406+
|> refute_has("td", text: draft_to_delete.title)
407+
end
408+
end
409+
359410
describe "filter badges" do
360411
test "shows badge for active category filter", %{conn: conn} do
361412
category = insert(:category, name: "Technology")

lib/backpex/live_resource/index.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,13 @@ defmodule Backpex.LiveResource.Index do
468468
end
469469

470470
defp apply_index_return_to(socket) do
471-
%{live_resource: live_resource, params: params, query_options: query_options} = socket.assigns
471+
%{live_resource: live_resource, params: params, query_options: query_options, filters_changed: filters_changed} =
472+
socket.assigns
472473

473-
socket
474-
|> assign(:return_to, Router.get_path(socket, live_resource, params, :index, query_options))
474+
return_to_options =
475+
if filters_changed, do: Map.put(query_options, :filters_changed, "true"), else: query_options
476+
477+
assign(socket, :return_to, Router.get_path(socket, live_resource, params, :index, return_to_options))
475478
end
476479

477480
# TODO: move to common module

0 commit comments

Comments
 (0)