load data async on addresses page

pull/1999/head
Ayrat Badykov 6 years ago
parent 60ceb4bb84
commit ad333aac7f
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 68
      apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex
  2. 21
      apps/block_scout_web/lib/block_scout_web/templates/address/index.html.eex
  3. 2
      apps/block_scout_web/lib/block_scout_web/views/address_view.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

@ -1,20 +1,21 @@
<section class="container">
<div class="card">
<div class="card-body">
<div class="card-body" data-async-listing="<%= @current_path %>">
<h1 class="card-title"><%= gettext "Addresses" %></h1>
<%= 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 %>
<div data-selector="top-addresses-list">
<%= 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 %>
<div data-selector="top-addresses-list"></div>
<div data-loading-message class="tile tile-muted text-center mt-3" style="display: none;">
<span class="loading-spinner-small mr-2">
<span class="loading-spinner-block-1"></span>
<span class="loading-spinner-block-2"></span>
</span>
<%= gettext("Loading") %>...
</div>
<%= 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 %>
</div>
</div>
</section>

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

Loading…
Cancel
Save