add addresses pagination to view

pull/1812/head
Ayrat Badykov 6 years ago
parent 1e4b5c2517
commit 75cf4c9f27
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 15
      apps/block_scout_web/lib/block_scout_web/chain.ex
  2. 30
      apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex
  3. 9
      apps/block_scout_web/lib/block_scout_web/templates/address/index.html.eex

@ -25,7 +25,8 @@ defmodule BlockScoutWeb.Chain do
InternalTransaction, InternalTransaction,
Log, Log,
TokenTransfer, TokenTransfer,
Transaction Transaction,
Wei
} }
alias Explorer.PagingOptions alias Explorer.PagingOptions
@ -82,7 +83,13 @@ defmodule BlockScoutWeb.Chain do
end end
def paging_options(%{"hash" => hash, "fetched_coin_balance" => fetched_coin_balance}) do def paging_options(%{"hash" => hash, "fetched_coin_balance" => fetched_coin_balance}) do
[paging_options: %{@default_paging_options | key: {fetched_coin_balance, hash}}] with {coin_balance, ""} <- Integer.parse(fetched_coin_balance),
{:ok, address_hash} <- string_to_address_hash(hash) do
[paging_options: %{@default_paging_options | key: {%Wei{value: Decimal.new(coin_balance)}, address_hash}}]
else
_ ->
[paging_options: @default_paging_options]
end
end end
def paging_options(%{ def paging_options(%{
@ -175,6 +182,10 @@ defmodule BlockScoutWeb.Chain do
end end
end end
defp paging_params({%Address{hash: hash, fetched_coin_balance: fetched_coin_balance}, _}) do
%{"hash" => hash, "fetched_coin_balance" => Decimal.to_string(fetched_coin_balance.value)}
end
defp paging_params({%Reward{block: %{number: number}}, _}) do defp paging_params({%Reward{block: %{number: number}}, _}) do
%{"block_number" => number, "index" => 0} %{"block_number" => number, "index" => 0}
end end

@ -1,16 +1,40 @@
defmodule BlockScoutWeb.AddressController do defmodule BlockScoutWeb.AddressController do
use BlockScoutWeb, :controller use BlockScoutWeb, :controller
import BlockScoutWeb.Chain, only: [paging_options: 1, next_page_params: 3, split_list_by_page: 1]
alias Explorer.{Chain, Market} alias Explorer.{Chain, Market}
alias Explorer.Chain.Address alias Explorer.Chain.Address
alias Explorer.ExchangeRates.Token alias Explorer.ExchangeRates.Token
def index(conn, _params) do def index(conn, params) do
addresses =
params
|> paging_options()
|> Chain.list_top_addresses()
{addresses_page, next_page} = split_list_by_page(addresses)
next_page_path =
case next_page_params(next_page, addresses_page, params) do
nil ->
nil
next_page_params ->
address_path(
conn,
:index,
next_page_params
)
end
render(conn, "index.html", render(conn, "index.html",
address_tx_count_pairs: Chain.list_top_addresses(), address_tx_count_pairs: addresses_page,
page_address_count: Enum.count(addresses_page),
address_count: Chain.count_addresses_with_balance_from_cache(), address_count: Chain.count_addresses_with_balance_from_cache(),
exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(),
total_supply: Chain.total_supply() total_supply: Chain.total_supply(),
next_page_path: next_page_path
) )
end end

@ -3,7 +3,9 @@
<div class="card-body"> <div class="card-body">
<h1><%= gettext "Addresses" %></h1> <h1><%= gettext "Addresses" %></h1>
<p> <p>
<%= gettext "Showing 250 addresses of" %> <%= gettext "Showing " %>
<%= Cldr.Number.to_string!(@page_address_count, format: "#,###") %>
<%= gettext " addresses of" %>
<%= Cldr.Number.to_string!(@address_count, format: "#,###") %> <%= Cldr.Number.to_string!(@address_count, format: "#,###") %>
<%= gettext "total addresses with a balance" %> <%= gettext "total addresses with a balance" %>
</p> </p>
@ -15,6 +17,11 @@
total_supply: @total_supply, tx_count: tx_count, total_supply: @total_supply, tx_count: tx_count,
validation_count: validation_count(address) %> validation_count: validation_count(address) %>
<% end %> <% end %>
<%= if @next_page_path do %>
<a href="<%= "#{@next_page_path}" %>" class="button button-secondary button-small float-right mt-4">
<%= gettext("Next") %>
</a>
<% end %>
</span> </span>
</div> </div>
</div> </div>

Loading…
Cancel
Save