Change to last seen paging and hide paging button on last page for

transaction_logs

Co-authored-by: Timothy Mecklem <timothy@mecklem.com>
pull/306/head
Stamates 7 years ago
parent 7dca4c343b
commit af8a0b9ab0
  1. 40
      apps/explorer_web/lib/explorer_web/controllers/transaction_log_controller.ex
  2. 13
      apps/explorer_web/lib/explorer_web/templates/transaction_log/index.html.eex
  3. 52
      apps/explorer_web/test/explorer_web/controllers/transaction_log_controller_test.exs

@ -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

@ -68,5 +68,18 @@
<% end %>
</div>
</div>
<%= 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 %>
</section>
</section>

@ -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

Loading…
Cancel
Save