Skip to content

Commit d32af8f

Browse files
authored
feat(explorer): add first and last buttons to pagination (yetanotherco#737)
1 parent 7736c12 commit d32af8f

3 files changed

Lines changed: 39 additions & 15 deletions

File tree

explorer/lib/explorer/models/batches.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ defmodule Batches do
8787
Explorer.Repo.all(query)
8888
end
8989

90+
def get_last_page(page_size) do
91+
total_batches = Explorer.Repo.aggregate(Batches, :count, :merkle_root)
92+
last_page = div(total_batches, page_size)
93+
if rem(total_batches, page_size) > 0, do: last_page + 1, else: last_page
94+
end
95+
9096
def get_amount_of_verified_batches() do
9197
query = from(b in Batches,
9298
where: b.is_verified == true,

explorer/lib/explorer_web/live/pages/batches/index.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ defmodule ExplorerWeb.Batches.Index do
1313

1414
PubSub.subscribe(Explorer.PubSub, "update_views")
1515

16-
{:ok, assign(socket, current_page: current_page, batches: batches, page_title: "Batches")}
16+
{:ok,
17+
assign(socket,
18+
current_page: current_page,
19+
batches: batches,
20+
last_page: Batches.get_last_page(@page_size),
21+
page_title: "Batches"
22+
)}
1723
end
1824

1925
@impl true
2026
def handle_info(_, socket) do
21-
IO.puts("Received update for batches from PubSub")
22-
2327
current_page = socket.assigns.current_page
2428

2529
batches = Batches.get_paginated_batches(%{page: current_page, page_size: @page_size})
2630

27-
{:noreply, assign(socket, batches: batches)}
31+
{:noreply, assign(socket, batches: batches, last_page: Batches.get_last_page(@page_size))}
2832
end
2933

3034
@impl true

explorer/lib/explorer_web/live/pages/batches/index.html.heex

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="flex flex-col space-y-3 text-foreground px-1 sm:max-w-lg md:max-w-3xl lg:max-w-5xl mx-auto capitalize">
22
<.card_preheding>Latest Batches</.card_preheding>
33
<%= if @batches != :empty and @batches != [] do %>
4-
<.card_background class="overflow-x-auto">
4+
<.card_background class="overflow-x-auto min-h-[38.45rem]">
55
<table class={[
66
"table-auto border-collapse w-full"
77
]}>
@@ -49,7 +49,14 @@
4949
<p class="text-lg text-muted-foreground">No batches found.</p>
5050
</.card_background>
5151
<% end %>
52-
<div class="flex gap-x-2 items-center justify-center md:justify-start w-full">
52+
<div class="flex gap-x-2 items-center justify-center w-full">
53+
<%= if @current_page >= 2 do %>
54+
<.link navigate={~p"/batches?page=#{1}"}>
55+
<.button class="text-muted-foreground group">
56+
First
57+
</.button>
58+
</.link>
59+
<% end %>
5360
<%= if @current_page > 1 do %>
5461
<.link navigate={~p"/batches?page=#{@current_page - 1}"}>
5562
<.button
@@ -77,14 +84,21 @@
7784
min="1"
7885
/>
7986
</form>
80-
<.link navigate={~p"/batches?page=#{@current_page + 1}"}>
81-
<.button
82-
icon="arrow-right-solid"
83-
icon_class="group-hover:translate-x-1 transition-all duration-150"
84-
class="text-muted-foreground size-10 group"
85-
>
86-
<span class="sr-only">Next Page</span>
87-
</.button>
88-
</.link>
87+
<%= if @current_page != @last_page do %>
88+
<.link navigate={~p"/batches?page=#{@current_page + 1}"}>
89+
<.button
90+
icon="arrow-right-solid"
91+
icon_class="group-hover:translate-x-1 transition-all duration-150"
92+
class="text-muted-foreground size-10 group"
93+
>
94+
<span class="sr-only">Next Page</span>
95+
</.button>
96+
</.link>
97+
<.link navigate={~p"/batches?page=#{@last_page}"}>
98+
<.button class="text-muted-foreground group">
99+
Last
100+
</.button>
101+
</.link>
102+
<% end %>
89103
</div>
90104
</div>

0 commit comments

Comments
 (0)