Merge pull request #1279 from poanetwork/wsa-async-load-home-page-transactions
Async load home page transactionspull/1281/head
commit
d81c5dd765
@ -0,0 +1,35 @@ |
|||||||
|
defmodule BlockScoutWeb.RecentTransactionsController do |
||||||
|
use BlockScoutWeb, :controller |
||||||
|
|
||||||
|
alias Explorer.{Chain, PagingOptions} |
||||||
|
alias Explorer.Chain.Hash |
||||||
|
alias Phoenix.View |
||||||
|
|
||||||
|
def index(conn, _params) do |
||||||
|
with true <- ajax?(conn) do |
||||||
|
recent_transactions = |
||||||
|
Chain.recent_collated_transactions( |
||||||
|
necessity_by_association: %{ |
||||||
|
:block => :required, |
||||||
|
[created_contract_address: :names] => :optional, |
||||||
|
[from_address: :names] => :required, |
||||||
|
[to_address: :names] => :optional |
||||||
|
}, |
||||||
|
paging_options: %PagingOptions{page_size: 5} |
||||||
|
) |
||||||
|
|
||||||
|
transactions = |
||||||
|
Enum.map(recent_transactions, fn transaction -> |
||||||
|
%{ |
||||||
|
transaction_hash: Hash.to_string(transaction.hash), |
||||||
|
transaction_html: |
||||||
|
View.render_to_string(BlockScoutWeb.TransactionView, "_tile.html", transaction: transaction) |
||||||
|
} |
||||||
|
end) |
||||||
|
|
||||||
|
json(conn, %{transactions: transactions}) |
||||||
|
else |
||||||
|
_ -> unprocessable_entity(conn) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,54 @@ |
|||||||
|
defmodule BlockScoutWeb.RecentTransactionsControllerTest do |
||||||
|
use BlockScoutWeb.ConnCase |
||||||
|
|
||||||
|
import BlockScoutWeb.Router.Helpers, only: [recent_transactions_path: 2] |
||||||
|
|
||||||
|
alias Explorer.Chain.Hash |
||||||
|
|
||||||
|
describe "GET index/2" do |
||||||
|
test "returns a transaction", %{conn: conn} do |
||||||
|
transaction = |
||||||
|
:transaction |
||||||
|
|> insert() |
||||||
|
|> with_block() |
||||||
|
|
||||||
|
conn = |
||||||
|
conn |
||||||
|
|> put_req_header("x-requested-with", "xmlhttprequest") |
||||||
|
|> get(recent_transactions_path(conn, :index)) |
||||||
|
|
||||||
|
assert response = json_response(conn, 200)["transactions"] |
||||||
|
|
||||||
|
response_hashes = Enum.map(response, & &1["transaction_hash"]) |
||||||
|
|
||||||
|
assert Enum.member?(response_hashes, Hash.to_string(transaction.hash)) |
||||||
|
end |
||||||
|
|
||||||
|
test "only returns transactions with an associated block", %{conn: conn} do |
||||||
|
associated = |
||||||
|
:transaction |
||||||
|
|> insert() |
||||||
|
|> with_block() |
||||||
|
|
||||||
|
unassociated = insert(:transaction) |
||||||
|
|
||||||
|
conn = |
||||||
|
conn |
||||||
|
|> put_req_header("x-requested-with", "xmlhttprequest") |
||||||
|
|> get(recent_transactions_path(conn, :index)) |
||||||
|
|
||||||
|
assert response = json_response(conn, 200)["transactions"] |
||||||
|
|
||||||
|
response_hashes = Enum.map(response, & &1["transaction_hash"]) |
||||||
|
|
||||||
|
assert Enum.member?(response_hashes, Hash.to_string(associated.hash)) |
||||||
|
refute Enum.member?(response_hashes, Hash.to_string(unassociated.hash)) |
||||||
|
end |
||||||
|
|
||||||
|
test "only responds to ajax requests", %{conn: conn} do |
||||||
|
conn = get(conn, recent_transactions_path(conn, :index)) |
||||||
|
|
||||||
|
assert conn.status == 422 |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue