diff --git a/apps/explorer_web/lib/explorer_web/controllers/transaction_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/transaction_controller.ex index 0558fe543b..4ea6e8636e 100644 --- a/apps/explorer_web/lib/explorer_web/controllers/transaction_controller.ex +++ b/apps/explorer_web/lib/explorer_web/controllers/transaction_controller.ex @@ -3,7 +3,8 @@ defmodule ExplorerWeb.TransactionController do alias Explorer.{Chain, PagingOptions} - @default_paging_options %PagingOptions{page_size: 50} + @page_size 50 + @default_paging_options %PagingOptions{page_size: @page_size + 1} def index(conn, %{"block_number" => block_number_string, "index" => index_string}) do with {block_number, ""} <- Integer.parse(block_number_string), @@ -37,21 +38,24 @@ defmodule ExplorerWeb.TransactionController do options ) - transactions = Chain.recent_collated_transactions(full_options) + transactions_plus_one = Chain.recent_collated_transactions(full_options) + + {next_page, transactions} = List.pop_at(transactions_plus_one, @page_size) + transaction_estimated_count = Chain.transaction_estimated_count() render( conn, "index.html", - earliest: earliest(transactions), + next_page_params: next_page_params(next_page, transactions), transaction_estimated_count: transaction_estimated_count, transactions: transactions ) end - defp earliest([]), do: nil + defp next_page_params(nil, _transactions), do: nil - defp earliest(transactions) do + defp next_page_params(_, transactions) do last = List.last(transactions) %{block_number: last.block_number, index: last.index} end diff --git a/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex b/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex index 824480c2c6..507a36ff89 100644 --- a/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex @@ -85,7 +85,7 @@ - <%= if @earliest do %> + <%= if @next_page_params do %> <%= link( gettext("Older"), class: "button button--secondary button--sm u-float-right mt-3", @@ -93,7 +93,7 @@ @conn, :index, @conn.assigns.locale, - %{"block_number" => @earliest.block_number, "index" => @earliest.index} + @next_page_params ) ) %> <% end %> diff --git a/apps/explorer_web/test/explorer_web/controllers/transaction_controller_test.exs b/apps/explorer_web/test/explorer_web/controllers/transaction_controller_test.exs index eec2512a95..3a873d190b 100644 --- a/apps/explorer_web/test/explorer_web/controllers/transaction_controller_test.exs +++ b/apps/explorer_web/test/explorer_web/controllers/transaction_controller_test.exs @@ -65,6 +65,30 @@ defmodule ExplorerWeb.TransactionControllerTest do assert second_page_hashes == actual_hashes end + test "next_page_params exist if not on last page", %{conn: conn} do + address = insert(:address) + + 60 + |> insert_list(:transaction, from_address_hash: address.hash) + |> with_block() + + conn = get(conn, "/en/transactions") + + assert conn.assigns.next_page_params + end + + test "next_page_params are empty if on last page", %{conn: conn} do + address = insert(:address) + + :transaction + |> insert(from_address_hash: address.hash) + |> with_block() + + conn = get(conn, "/en/transactions") + + refute conn.assigns.next_page_params + end + test "guards against bad block_number input", %{conn: conn} do conn = get(conn, "/en/transactions", %{"block_number" => "foo", "index" => "2"}) assert html_response(conn, 422)