From ad333aac7f421d450068c0c5e7943898086a7bb0 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 22 May 2019 12:53:25 +0300 Subject: [PATCH] load data async on addresses page --- .../controllers/address_controller.ex | 68 +++++++++---------- .../templates/address/index.html.eex | 21 +++--- .../lib/block_scout_web/views/address_view.ex | 2 - 3 files changed, 44 insertions(+), 47 deletions(-) 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 3d43ed7054..88ff531f70 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 @@ -3,11 +3,13 @@ defmodule BlockScoutWeb.AddressController do import BlockScoutWeb.Chain, only: [paging_options: 1, next_page_params: 3, split_list_by_page: 1] + alias BlockScoutWeb.AddressView alias Explorer.{Chain, Market} alias Explorer.Chain.Address alias Explorer.ExchangeRates.Token + alias Phoenix.View - def index(conn, params) do + def index(conn, %{"type" => "JSON"} = params) do addresses = params |> paging_options() @@ -15,41 +17,49 @@ defmodule BlockScoutWeb.AddressController do {addresses_page, next_page} = split_list_by_page(addresses) - cur_page_number = - cond do - !params["prev_page_number"] -> 1 - params["next_page"] -> String.to_integer(params["prev_page_number"]) + 1 - params["prev_page"] -> String.to_integer(params["prev_page_number"]) - 1 - end - next_page_path = case next_page_params(next_page, addresses_page, params) do nil -> nil next_page_params -> - next_params = - next_page_params - |> Map.put("prev_page_path", cur_page_path(conn, params)) - |> Map.put("next_page", true) - |> Map.put("prev_page_number", cur_page_number) - address_path( conn, :index, - next_params + next_page_params ) end + exchange_rate = Market.get_exchange_rate(Explorer.coin()) || Token.null() + total_supply = Chain.total_supply() + + json( + conn, + %{ + items: + addresses_page + |> Enum.with_index(1) + |> Enum.map(fn {{address, tx_count}, index} -> + View.render_to_string( + AddressView, + "_tile.html", + address: address, + index: index, + exchange_rate: exchange_rate, + total_supply: total_supply, + tx_count: tx_count, + validation_count: validation_count(address) + ) + end), + next_page_path: next_page_path + } + ) + end + + def index(conn, _params) do render(conn, "index.html", - 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(), - next_page_path: next_page_path, - prev_page_path: params["prev_page_path"], - cur_page_number: cur_page_number + current_path: current_path(conn), + address_count: Chain.count_addresses_with_balance_from_cache() ) end @@ -64,16 +74,4 @@ defmodule BlockScoutWeb.AddressController do def validation_count(%Address{} = address) do Chain.address_to_validation_count(address) end - - defp cur_page_path(conn, %{"hash" => _hash, "fetched_coin_balance" => _balance} = params) do - new_params = Map.put(params, "next_page", false) - - address_path( - conn, - :index, - new_params - ) - end - - defp cur_page_path(conn, _), do: address_path(conn, :index) 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 91697de0c9..dd57df4273 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 @@ -1,20 +1,21 @@
-
+

<%= gettext "Addresses" %>

- <%= render BlockScoutWeb.CommonComponentsView, "_pagination_container.html", position: "top", prev_page_path: @prev_page_path, next_page_path: @next_page_path, cur_page_number: @cur_page_number, show_pagination_limit: true %> + <%= render BlockScoutWeb.CommonComponentsView, "_pagination_container.html", position: "top", cur_page_number: "1", show_pagination_limit: true, data_next_page_button: true %> -
- <%= for {{address, tx_count}, index} <- Enum.with_index(@address_tx_count_pairs, 1) do %> - <%= render "_tile.html", - address: address, index: index, exchange_rate: @exchange_rate, - total_supply: @total_supply, tx_count: tx_count, - validation_count: validation_count(address) %> - <% end %> +
+ + - <%= render BlockScoutWeb.CommonComponentsView, "_pagination_container.html", position: "bottom", prev_page_path: @prev_page_path, next_page_path: @next_page_path, cur_page_number: @cur_page_number, show_pagination_limit: true %> + <%= render BlockScoutWeb.CommonComponentsView, "_pagination_container.html", position: "bottom", cur_page_number: "1", show_pagination_limit: true, data_next_page_button: true %>
diff --git a/apps/block_scout_web/lib/block_scout_web/views/address_view.ex b/apps/block_scout_web/lib/block_scout_web/views/address_view.ex index 7a6005d526..3102401144 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/address_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/address_view.ex @@ -3,8 +3,6 @@ defmodule BlockScoutWeb.AddressView do require Logger - import BlockScoutWeb.AddressController, only: [validation_count: 1] - alias BlockScoutWeb.LayoutView alias Explorer.Chain alias Explorer.Chain.{Address, Hash, InternalTransaction, SmartContract, Token, TokenTransfer, Transaction, Wei}