Replace PendingTransactionForm with proper PendingTransactionView functions

pull/128/head
Luke Imhoff 7 years ago
parent a34e1a9fff
commit 5c69c523da
  1. 32
      apps/explorer_web/lib/explorer_web/controllers/pending_transaction_controller.ex
  2. 42
      apps/explorer_web/lib/explorer_web/forms/pending_transaction_form.ex
  3. 20
      apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex
  4. 23
      apps/explorer_web/lib/explorer_web/views/pending_transaction_view.ex
  5. 34
      apps/explorer_web/test/explorer_web/controllers/pending_transaction_controller_test.exs
  6. 37
      apps/explorer_web/test/explorer_web/forms/pending_transaction_form_test.exs

@ -3,32 +3,34 @@ defmodule ExplorerWeb.PendingTransactionController do
alias Explorer.Chain
alias Explorer.Chain.Transaction
alias ExplorerWeb.PendingTransactionForm
def index(conn, %{"last_seen" => last_seen_id} = _) do
total = Chain.transaction_count(pending: true)
entries =
last_seen_id
|> Chain.transactions_recently_before_id(
necessity_by_association: %{
from_address: :optional,
to_address: :optional
},
transactions =
Chain.transactions_recently_before_id(
last_seen_id,
necessity_by_association: %{from_address: :optional, to_address: :optional},
pending: true
)
|> Enum.map(&PendingTransactionForm.build/1)
last = List.last(entries) || Transaction.null()
last_seen_transaction_id =
case transactions do
[] ->
nil
_ ->
transactions
|> Stream.map(fn %Transaction{id: id} -> id end)
|> Enum.max()
end
render(
conn,
"index.html",
transactions: %{
entries: entries,
total_entries: total,
last_seen: last.id
}
last_seen_transaction_id: last_seen_transaction_id,
transaction_count: total,
transactions: transactions
)
end

@ -1,42 +0,0 @@
defmodule ExplorerWeb.PendingTransactionForm do
@moduledoc "Format a pending Transaction for display."
import ExplorerWeb.Gettext
alias Explorer.Chain.{Address, Transaction}
# Functions
def build(transaction) do
Map.merge(transaction, %{
first_seen: first_seen(transaction),
formatted_status: gettext("Pending"),
from_address_hash: from_address_hash(transaction),
last_seen: last_seen(transaction),
status: :pending,
to_address_hash: to_address_hash(transaction)
})
end
def first_seen(transaction) do
transaction.inserted_at |> Timex.from_now()
end
def from_address_hash(%Transaction{from_address: from_address}) do
case from_address do
%Address{hash: hash} -> hash
_ -> nil
end
end
def last_seen(transaction) do
transaction.updated_at |> Timex.from_now()
end
def to_address_hash(%Transaction{to_address: to_address}) do
case to_address do
%Address{hash: hash} -> hash
_ -> nil
end
end
end

@ -1,7 +1,7 @@
<section class="container__section">
<section class="container__subsection">
<div class="transactions__headline">
<h1 class="transactions__headline-title"><%= gettext("Showing %{count} Pending Transactions", count: @transactions.total_entries) %></h1>
<h1 class="transactions__headline-title"><%= gettext("Showing %{count} Pending Transactions", count: @transaction_count) %></h1>
</div>
<div class="transactions">
<div class="transactions__tabs">
@ -21,23 +21,25 @@
</tr>
</thead>
<tbody>
<%= for transaction <- @transactions.entries do %>
<%= for transaction <- @transactions do %>
<tr class="transactions__row">
<td class="transactions__column transactions__column--status"><div class="transactions__dot transactions__dot--<%= transaction.status %>"></div></td>
<td class="transactions__column transactions__column--status"><div class="transactions__dot transactions__dot--pending"></div></td>
<td class="transactions__column transactions__column--hash">
<%= link(transaction.hash, to: transaction_path(@conn, :show, @conn.assigns.locale, transaction.hash), class: "transactions__link transactions__link--truncated transactions__link--long-hash") %>
</td>
<td class="transactions__column transactions__column--last-seen"><%= transaction.last_seen %></td>
<td class="transactions__column transactions__column--last-seen"><%= last_seen(transaction) %></td>
<td class="transactions__column transactions__column--optional transactions__column--from-address">
<%= if transaction.to_address_hash do %>
<%= link(transaction.to_address_hash, to: address_path(@conn, :show, @conn.assigns.locale, transaction.to_address_hash), class: "transactions__link transactions__link--truncated transactions__link--hash") %>
<% to_address_hash = to_address_hash(transaction) %>
<%= if to_address_hash do %>
<%= link(to_address_hash, to: address_path(@conn, :show, @conn.assigns.locale, to_address_hash), class: "transactions__link transactions__link--truncated transactions__link--hash") %>
<% else %>
<%= gettext "Pending" %>
<% end %>
</td>
<td class="transactions__column transactions__column--optional transactions__column--to-address">
<%= if transaction.from_address_hash do %>
<%= link(transaction.from_address_hash, to: address_path(@conn, :show, @conn.assigns.locale, transaction.from_address_hash), class: "transactions__link transactions__link--truncated transactions__link--hash") %>
<% from_address_hash = from_address_hash(transaction) %>
<%= if from_address_hash do %>
<%= link(from_address_hash, to: address_path(@conn, :show, @conn.assigns.locale, from_address_hash), class: "transactions__link transactions__link--truncated transactions__link--hash") %>
<% else %>
<%= gettext "Pending" %>
<% end %>
@ -47,7 +49,7 @@
<% end %>
</tbody>
</table>
<%= link(gettext("Next Page"), to: pending_transaction_path(@conn, :index, @conn.assigns.locale, %{"last_seen" => @transactions.last_seen}), class: "transactions__link transactions__link--next-page") %>
<%= link(gettext("Next Page"), to: pending_transaction_path(@conn, :index, @conn.assigns.locale, %{"last_seen" => @last_seen_transaction_id}), class: "transactions__link transactions__link--next-page") %>
</div>
</div>
</section>

@ -1,4 +1,27 @@
defmodule ExplorerWeb.PendingTransactionView do
use ExplorerWeb, :view
alias Explorer.Chain.{Address, Transaction}
@dialyzer :no_match
# Functions
def from_address_hash(%Transaction{from_address: from_address}) do
case from_address do
%Address{hash: hash} -> hash
_ -> nil
end
end
def last_seen(%Transaction{updated_at: updated_at}) do
Timex.from_now(updated_at)
end
def to_address_hash(%Transaction{to_address: to_address}) do
case to_address do
%Address{hash: hash} -> hash
_ -> nil
end
end
end

@ -12,8 +12,13 @@ defmodule ExplorerWeb.PendingTransactionControllerTest do
insert(:block_transaction, transaction: transaction, block: block)
insert(:to_address, transaction: transaction, address: address)
insert(:from_address, transaction: transaction, address: address)
conn = get(conn, pending_transaction_path(ExplorerWeb.Endpoint, :index, :en))
assert conn.assigns.transactions.entries == []
assert html = html_response(conn, 200)
assert html =~ ~r/Showing 0 Pending Transactions/
refute html =~ ~r/transactions__row/
end
test "does not count transactions that have a receipt", %{conn: conn} do
@ -24,8 +29,13 @@ defmodule ExplorerWeb.PendingTransactionControllerTest do
insert(:block_transaction, transaction: transaction, block: block)
insert(:to_address, transaction: transaction, address: address)
insert(:from_address, transaction: transaction, address: address)
conn = get(conn, pending_transaction_path(ExplorerWeb.Endpoint, :index, :en))
assert conn.assigns.transactions.total_entries === 0
assert html = html_response(conn, 200)
assert html =~ ~r/Showing 0 Pending Transactions/
refute html =~ ~r/transactions__row/
end
test "returns pending transactions", %{conn: conn} do
@ -33,8 +43,10 @@ defmodule ExplorerWeb.PendingTransactionControllerTest do
transaction = insert(:transaction)
insert(:to_address, transaction: transaction, address: address)
insert(:from_address, transaction: transaction, address: address)
conn = get(conn, pending_transaction_path(ExplorerWeb.Endpoint, :index, :en))
assert List.first(conn.assigns.transactions.entries).id == transaction.id
assert html_response(conn, 200) =~ transaction.hash
end
test "returns a count of pending transactions", %{conn: conn} do
@ -42,8 +54,10 @@ defmodule ExplorerWeb.PendingTransactionControllerTest do
transaction = insert(:transaction)
insert(:to_address, transaction: transaction, address: address)
insert(:from_address, transaction: transaction, address: address)
conn = get(conn, pending_transaction_path(ExplorerWeb.Endpoint, :index, :en))
assert length(conn.assigns.transactions.entries) === 1
assert html_response(conn, 200) =~ ~r/Showing 1 Pending Transactions/
end
test "paginates transactions using the last seen transaction", %{conn: conn} do
@ -59,19 +73,23 @@ defmodule ExplorerWeb.PendingTransactionControllerTest do
last_seen: transaction.id
)
assert conn.assigns.transactions.entries == []
refute html_response(conn, 200) =~ ~r/transactions__row/
end
test "sends back an estimate of the number of transactions", %{conn: conn} do
insert(:transaction)
conn = get(conn, pending_transaction_path(ExplorerWeb.Endpoint, :index, :en))
refute conn.assigns.transactions.total_entries == nil
assert html_response(conn, 200) =~ ~r/Showing 1 Pending Transactions/
end
test "works when there are no transactions", %{conn: conn} do
conn = get(conn, pending_transaction_path(ExplorerWeb.Endpoint, :index, :en))
assert conn.assigns.transactions.total_entries == 0
assert conn.assigns.transactions.entries == []
assert html = html_response(conn, 200)
assert html =~ ~r/Showing 0 Pending Transactions/
refute html =~ ~r/transactions__row/
end
end
end

@ -1,37 +0,0 @@
defmodule ExplorerWeb.PendingTransactionFormTest do
use Explorer.DataCase
alias ExplorerWeb.PendingTransactionForm
describe "build/1" do
test "returns a successful transaction when there is a successful receipt" do
time = DateTime.utc_now()
to_address = insert(:address, hash: "0xcafe")
from_address = insert(:address, hash: "0xbee5")
transaction =
insert(
:transaction,
inserted_at: time,
updated_at: time,
to_address_id: to_address.id,
from_address_id: from_address.id
)
form =
PendingTransactionForm.build(transaction |> Repo.preload([:to_address, :from_address]))
assert(
form ==
Map.merge(transaction |> Repo.preload([:to_address, :from_address]), %{
to_address_hash: "0xcafe",
from_address_hash: "0xbee5",
first_seen: time |> Timex.from_now(),
last_seen: time |> Timex.from_now(),
status: :pending,
formatted_status: "Pending"
})
)
end
end
end
Loading…
Cancel
Save