Skip to content

Commit d88b5a1

Browse files
authored
CRM: fix preserving index on pagination (#6130)
* Fix preserving the sort order, compute index only once * Test * Revert "Fix preserving the sort order, compute index only once" This reverts commit 0d5d5e8. * Reapply "Fix preserving the sort order, compute index only once" This reverts commit 3a22aea.
1 parent 4bb13bf commit d88b5a1

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

  • extra/lib/plausible_web/live/customer_support/team/components
  • test/plausible_web/live/customer_support

extra/lib/plausible_web/live/customer_support/team/components/sites.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ defmodule PlausibleWeb.CustomerSupport.Team.Components.Sites do
1717
team = Repo.preload(team, :owners)
1818
owner = List.first(team.owners)
1919

20-
index_state =
21-
Index.build(owner, team: team, sort_by: :traffic, sort_direction: :desc)
20+
socket =
21+
assign_new(socket, :index_state, fn ->
22+
Index.build(owner, team: team, sort_by: :traffic, sort_direction: :desc)
23+
end)
2224

23-
page = Index.paginate(index_state, tab_params["page"], @page_size)
25+
page = Index.paginate(socket.assigns.index_state, tab_params["page"], @page_size)
2426

2527
sites = fetch_sites(page.entries)
2628

@@ -35,7 +37,6 @@ defmodule PlausibleWeb.CustomerSupport.Team.Components.Sites do
3537
team: team,
3638
sites: sites,
3739
hourly_stats: hourly_stats,
38-
index_state: index_state,
3940
page_number: page.page_number,
4041
total_pages: page.total_pages,
4142
total_entries: page.total_entries,

test/plausible_web/live/customer_support/teams_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,31 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
212212
assert text(html) =~ "Page 1 of 2"
213213
end
214214

215+
test "persists sort order across paginated entries", %{conn: conn, user: user} do
216+
team = team_of(user)
217+
218+
for i <- 1..24 do
219+
new_site(owner: user, domain: "site-#{String.pad_leading("#{i}", 2, "0")}.example.com")
220+
end
221+
222+
{:ok, lv, _html} = live(conn, open_team(team.id, tab: "sites"))
223+
224+
# sort alphabetically ascending
225+
lv |> element(~s|th[phx-value-by="alnum"]|) |> render_click()
226+
227+
html = render(lv)
228+
page1_domains = extract_domains(html)
229+
assert page1_domains == Enum.sort(page1_domains)
230+
231+
lv |> render_patch(open_team(team.id, tab: "sites", page: 2))
232+
html = render(lv)
233+
page2_domains = extract_domains(html)
234+
235+
# should still be alphabetically ascending on page 2
236+
assert page2_domains == Enum.sort(page2_domains)
237+
assert Enum.max(page1_domains) < Enum.min(page2_domains)
238+
end
239+
215240
test "shows no pagination when sites fit on one page", %{conn: conn, user: user} do
216241
team = team_of(user)
217242
new_site(owner: user, domain: "only-site.example.com")

0 commit comments

Comments
 (0)