diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/transaction_token_transfer_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/transaction_token_transfer_controller.ex index baa36f1a2f..a750a75076 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/transaction_token_transfer_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/transaction_token_transfer_controller.ex @@ -3,11 +3,12 @@ defmodule BlockScoutWeb.TransactionTokenTransferController do import BlockScoutWeb.Chain, only: [paging_options: 1, next_page_params: 3, split_list_by_page: 1] - alias BlockScoutWeb.TransactionView + alias BlockScoutWeb.{TransactionTokenTransferView, TransactionView} alias Explorer.{Chain, Market} alias Explorer.ExchangeRates.Token + alias Phoenix.View - def index(conn, %{"transaction_id" => hash_string} = params) do + def index(conn, %{"transaction_id" => hash_string, "type" => "JSON"} = params) do with {:ok, hash} <- Chain.string_to_transaction_hash(hash_string), {:ok, transaction} <- Chain.hash_to_transaction( @@ -37,13 +38,68 @@ defmodule BlockScoutWeb.TransactionTokenTransferController do {token_transfers, next_page} = split_list_by_page(token_transfers_plus_one) + next_page_url = + case next_page_params(next_page, token_transfers, params) do + nil -> + nil + + next_page_params -> + transaction_token_transfer_path(conn, :index, transaction, Map.delete(next_page_params, "type")) + end + + items = + token_transfers + |> Enum.map(fn transfer -> + View.render_to_string( + TransactionTokenTransferView, + "_token_transfer.html", + token_transfer: transfer, + conn: conn + ) + end) + + json( + conn, + %{ + items: items, + next_page_path: next_page_url + } + ) + else + :error -> + conn + |> put_status(422) + |> put_view(TransactionView) + |> render("invalid.html", transaction_hash: hash_string) + + {:error, :not_found} -> + conn + |> put_status(404) + |> put_view(TransactionView) + |> render("not_found.html", transaction_hash: hash_string) + end + end + + def index(conn, %{"transaction_id" => hash_string}) do + with {:ok, hash} <- Chain.string_to_transaction_hash(hash_string), + {:ok, transaction} <- + Chain.hash_to_transaction( + hash, + necessity_by_association: %{ + :block => :optional, + [created_contract_address: :names] => :optional, + [from_address: :names] => :optional, + [to_address: :names] => :optional, + [to_address: :smart_contract] => :optional, + :token_transfers => :optional + } + ) do render( conn, "index.html", exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), block_height: Chain.block_height(), - next_page_params: next_page_params(next_page, token_transfers, params), - token_transfers: token_transfers, + current_path: current_path(conn), show_token_transfers: true, transaction: transaction ) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction_token_transfer/index.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction_token_transfer/index.html.eex index 53ef700b14..d40dedd414 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction_token_transfer/index.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction_token_transfer/index.html.eex @@ -3,34 +3,24 @@