From 75cf4c9f271fcc424eb0684387f4e8269cd00334 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 23 Apr 2019 13:27:59 +0300 Subject: [PATCH] add addresses pagination to view --- .../lib/block_scout_web/chain.ex | 15 ++++++++-- .../controllers/address_controller.ex | 30 +++++++++++++++++-- .../templates/address/index.html.eex | 9 +++++- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/chain.ex b/apps/block_scout_web/lib/block_scout_web/chain.ex index b2fd0a188d..5903db1144 100644 --- a/apps/block_scout_web/lib/block_scout_web/chain.ex +++ b/apps/block_scout_web/lib/block_scout_web/chain.ex @@ -25,7 +25,8 @@ defmodule BlockScoutWeb.Chain do InternalTransaction, Log, TokenTransfer, - Transaction + Transaction, + Wei } alias Explorer.PagingOptions @@ -82,7 +83,13 @@ defmodule BlockScoutWeb.Chain do end 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 def paging_options(%{ @@ -175,6 +182,10 @@ defmodule BlockScoutWeb.Chain do 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 %{"block_number" => number, "index" => 0} end diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex index a0c443aee6..730b09eb8f 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex @@ -1,16 +1,40 @@ defmodule BlockScoutWeb.AddressController do 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.Address 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", - 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(), 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 diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address/index.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address/index.html.eex index e0a10a8e65..cf4e0efb95 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address/index.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address/index.html.eex @@ -3,7 +3,9 @@

<%= gettext "Addresses" %>

- <%= 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: "#,###") %> <%= gettext "total addresses with a balance" %>

@@ -15,6 +17,11 @@ total_supply: @total_supply, tx_count: tx_count, validation_count: validation_count(address) %> <% end %> + <%= if @next_page_path do %> + " class="button button-secondary button-small float-right mt-4"> + <%= gettext("Next") %> + + <% end %>