From af8a0b9ab0c003d27f70afce2f11aca044515bc9 Mon Sep 17 00:00:00 2001 From: Stamates Date: Thu, 14 Jun 2018 10:03:08 -0400 Subject: [PATCH] Change to last seen paging and hide paging button on last page for transaction_logs Co-authored-by: Timothy Mecklem --- .../controllers/transaction_log_controller.ex | 40 +++++++++++--- .../templates/transaction_log/index.html.eex | 13 +++++ .../transaction_log_controller_test.exs | 52 ++++++++++++++++++- 3 files changed, 97 insertions(+), 8 deletions(-) diff --git a/apps/explorer_web/lib/explorer_web/controllers/transaction_log_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/transaction_log_controller.ex index 925c12252d..6aa8c748df 100644 --- a/apps/explorer_web/lib/explorer_web/controllers/transaction_log_controller.ex +++ b/apps/explorer_web/lib/explorer_web/controllers/transaction_log_controller.ex @@ -1,9 +1,12 @@ defmodule ExplorerWeb.TransactionLogController do use ExplorerWeb, :controller - alias Explorer.{Chain, Market} + alias Explorer.{Chain, Market, PagingOptions} alias Explorer.ExchangeRates.Token + @page_size 50 + @default_paging_options %PagingOptions{page_size: @page_size + 1} + def index(conn, %{"transaction_id" => transaction_hash_string} = params) do with {:ok, transaction_hash} <- Chain.string_to_transaction_hash(transaction_hash_string), {:ok, transaction} <- @@ -15,18 +18,26 @@ defmodule ExplorerWeb.TransactionLogController do to_address: :required } ) do - logs = - Chain.transaction_to_logs( - transaction, - necessity_by_association: %{address: :optional}, - pagination: params + full_options = + Keyword.merge( + [ + necessity_by_association: %{ + address: :optional + } + ], + paging_options(params) ) + logs_plus_one = Chain.transaction_to_logs(transaction, full_options) + + {logs, next_page} = Enum.split(logs_plus_one, @page_size) + render( conn, "index.html", logs: logs, max_block_number: max_block_number(), + next_page_params: next_page_params(next_page, logs), transaction: transaction, exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null() ) @@ -45,4 +56,21 @@ defmodule ExplorerWeb.TransactionLogController do {:error, :not_found} -> 0 end end + + defp next_page_params([], _logs), do: nil + + defp next_page_params(_, logs) do + last = List.last(logs) + %{index: last.index} + end + + defp paging_options(params) do + with %{"index" => index_string} <- params, + {index, ""} <- Integer.parse(index_string) do + [paging_options: %{@default_paging_options | key: {index}}] + else + _ -> + [paging_options: @default_paging_options] + end + end end diff --git a/apps/explorer_web/lib/explorer_web/templates/transaction_log/index.html.eex b/apps/explorer_web/lib/explorer_web/templates/transaction_log/index.html.eex index 86bfea85fc..5ee750ef99 100644 --- a/apps/explorer_web/lib/explorer_web/templates/transaction_log/index.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/transaction_log/index.html.eex @@ -68,5 +68,18 @@ <% end %> + <%= if @next_page_params do %> + <%= link( + gettext("Newer"), + class: "button button--secondary button--sm u-float-right mt-3", + to: transaction_log_path( + @conn, + :index, + @conn.assigns.locale, + @transaction, + @next_page_params + ) + ) %> + <% end %> diff --git a/apps/explorer_web/test/explorer_web/controllers/transaction_log_controller_test.exs b/apps/explorer_web/test/explorer_web/controllers/transaction_log_controller_test.exs index 634c3e91b8..b4ad1ae098 100644 --- a/apps/explorer_web/test/explorer_web/controllers/transaction_log_controller_test.exs +++ b/apps/explorer_web/test/explorer_web/controllers/transaction_log_controller_test.exs @@ -33,7 +33,7 @@ defmodule ExplorerWeb.TransactionLogControllerTest do conn = get(conn, transaction_log_path(conn, :index, :en, transaction)) - first_log = List.first(conn.assigns.logs.entries) + first_log = List.first(conn.assigns.logs) assert first_log.transaction_hash == transaction.hash end @@ -44,7 +44,55 @@ defmodule ExplorerWeb.TransactionLogControllerTest do conn = get(conn, path) - assert Enum.count(conn.assigns.logs.entries) == 0 + assert Enum.count(conn.assigns.logs) == 0 + end + + test "returns next page of results based on last seen transaction log", %{conn: conn} do + transaction = + :transaction + |> insert() + |> with_block() + + second_page_indexes = + 1..50 + |> Enum.map(fn index -> insert(:log, transaction_hash: transaction.hash, index: index) end) + |> Enum.map(& &1.index) + + log = insert(:log, transaction_hash: transaction.hash, index: 51) + + conn = + get(conn, transaction_log_path(conn, :index, :en, transaction), %{ + "index" => Integer.to_string(log.index) + }) + + actual_indexes = Enum.map(conn.assigns.logs, & &1.index) + + assert second_page_indexes == actual_indexes + end + + test "next_page_params exist if not on last page", %{conn: conn} do + transaction = + :transaction + |> insert() + |> with_block() + + 1..60 + |> Enum.map(fn index -> insert(:log, transaction_hash: transaction.hash, index: index) end) + + conn = get(conn, transaction_log_path(conn, :index, :en, transaction)) + + assert conn.assigns.next_page_params + end + + test "next_page_params are empty if on last page", %{conn: conn} do + transaction = + :transaction + |> insert() + |> with_block() + + conn = get(conn, transaction_log_path(conn, :index, :en, transaction)) + + refute conn.assigns.next_page_params end end