Skip to content

Commit 4af43a0

Browse files
feat: numeric pagination with ellipsis logic
1 parent ae27f26 commit 4af43a0

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

lib/yearbook_web/live/backoffice/mock_live/index.ex

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ defmodule YearbookWeb.Approvals do
7272
)
7373
end
7474

75+
defp page_range(current, total) do
76+
if total <= 7 do
77+
Enum.to_list(1..total)
78+
else
79+
([1, total] ++ Enum.to_list((current - 2)..(current + 2)))
80+
|> Enum.filter(&(&1 >= 1 and &1 <= total))
81+
|> Enum.uniq()
82+
|> Enum.sort()
83+
|> add_dots([])
84+
end
85+
end
86+
87+
defp add_dots([a, b | tail], acc) when b > a + 1 do
88+
add_dots([b | tail], acc ++ [a, :ellipsis])
89+
end
90+
91+
defp add_dots([head | tail], acc) do
92+
add_dots(tail, acc ++ [head])
93+
end
94+
95+
defp add_dots([], acc), do: acc
96+
7597
def render(assigns) do
7698
~H"""
7799
<div class="flex-1 p-7 bg-white rounded-2xl">
@@ -133,7 +155,7 @@ defmodule YearbookWeb.Approvals do
133155
Página <strong>{@current_page_num}</strong> de {@total_pages}
134156
</span>
135157
136-
<div class="flex gap-2">
158+
<div class="flex items-center gap-2">
137159
<.link
138160
:if={@current_page_num > 1}
139161
patch={~p"/backoffice/approvals?page=#{@current_page_num - 1}"}
@@ -142,6 +164,25 @@ defmodule YearbookWeb.Approvals do
142164
<.icon name="hero-chevron-left" class="size-5" />
143165
</.link>
144166
167+
<%= for item <- page_range(@current_page_num, @total_pages) do %>
168+
<%= if item == :ellipsis do %>
169+
<span class="px-2 text-gray-400">...</span>
170+
<% else %>
171+
<.link
172+
patch={~p"/backoffice/approvals?page=#{item}"}
173+
class={[
174+
"px-3 py-1 rounded text-sm font-medium",
175+
if(item == @current_page_num,
176+
do: "bg-gray-800 text-white",
177+
else: "text-gray-600 hover:bg-gray-100"
178+
)
179+
]}
180+
>
181+
{item}
182+
</.link>
183+
<% end %>
184+
<% end %>
185+
145186
<.link
146187
:if={@current_page_num < @total_pages}
147188
patch={~p"/backoffice/approvals?page=#{@current_page_num + 1}"}

priv/repo/seeds/entries.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule Yearbook.Repo.Seeds.Entries do
1313
end
1414

1515
def seed_all_entries do
16-
for {name, i} <- Enum.with_index(Enum.take(@names, 17)) do
16+
for {name, i} <- Enum.with_index(Enum.take(@names, 87)) do
1717
attrs = %{
1818
name: name,
1919
text: Enum.random(@quotes),

0 commit comments

Comments
 (0)