Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Add missing pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiago Pittella authored and Santiago Pittella committed Oct 12, 2023
1 parent cc9bed9 commit eca1165
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 9 deletions.
22 changes: 21 additions & 1 deletion lib/starknet_explorer/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ defmodule StarknetExplorer.Transaction do
|> Repo.paginate(params)
end

def paginate_transactions_for_index(params, network) do
def paginate_transactions_for_index(params, network, filter \\ "ALL")

def paginate_transactions_for_index(params, network, "ALL") do
query =
from t in Transaction,
where: t.network == ^network,
Expand All @@ -209,6 +211,24 @@ defmodule StarknetExplorer.Transaction do
query |> Repo.paginate(Map.put(params, :options, %{total_entries: total_rows}))
end

def paginate_transactions_for_index(params, network, filter) do
query =
from t in Transaction,
where: t.network == ^network and t.type == ^filter,
join: b in Block,
on: t.block_number == b.number and t.network == b.network,
select: %{hash: t.hash, type: t.type, timestamp: b.timestamp, status: b.status},
order_by: [desc: b.timestamp]

total_rows =
case StarknetExplorer.Counts.get(network) do
nil -> get_total_count(network)
entities_count -> entities_count.transactions
end

query |> Repo.paginate(Map.put(params, :options, %{total_entries: total_rows}))
end

@spec rename_rpc_fields(map) :: map
def rename_rpc_fields(rpc_tx = %{"transaction_hash" => th}) do
rpc_tx
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer_web/live/pages/block_detail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
class="dropdown relative bg-[#232331] p-5 mb-2 rounded-md lg:hidden"
phx-hook="Dropdown"
>
<span class="networkSelected capitalize"><%= assigns.view %></span>
<span class="networkSelected capitalize"><%= assigns.tx_filter %></span>
<span class="absolute inset-y-0 right-5 transform translate-1/2 flex items-center">
<img class="transform rotate-90 w-5 h-5" src={~p"/images/dropdown.svg"} />
</span>
Expand Down
95 changes: 88 additions & 7 deletions lib/starknet_explorer_web/live/transaction_index_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule StarknetExplorerWeb.TransactionIndexLive do
<div class="max-w-7xl mx-auto">
<div class="table-header">
<h2>Transactions</h2>
<%= render_tx_filter(assigns) %>
<CoreComponents.pagination_links
id="transactions-top-pagination"
page={@page}
Expand Down Expand Up @@ -85,14 +86,88 @@ defmodule StarknetExplorerWeb.TransactionIndexLive do
"""
end

def render_tx_filter(assigns) do
~H"""
<div>
<div>
<div
id="dropdown"
class="dropdown relative bg-[#232331] p-5 mb-2 rounded-md lg:hidden"
phx-hook="Dropdown"
>
<span class="networkSelected capitalize"><%= assigns.tx_filter %></span>
<span class="absolute inset-y-0 right-5 transform translate-1/2 flex items-center">
<img class="transform rotate-90 w-5 h-5" src={~p"/images/dropdown.svg"} />
</span>
</div>
<div>
<div class="options hidden">
<div
class={"option #{if Map.get(assigns, :tx_filter) == "ALL", do: "lg:!border-b-se-blue text-white", else: "lg:border-b-transparent text-gray-400"}"}
phx-click="select-filter"
,
phx-value-filter="ALL"
>
All
</div>
<div
class={"option #{if Map.get(assigns, :tx_filter) == "INVOKE", do: "lg:!border-b-se-blue text-white", else: "lg:border-b-transparent text-gray-400"}"}
phx-click="select-filter"
,
phx-value-filter="INVOKE"
>
Invoke
</div>
<div
class={"option #{if Map.get(assigns, :tx_filter) == "DEPLOY_ACCOUNT", do: "lg:!border-b-se-blue text-white", else: "lg:border-b-transparent text-gray-400"}"}
phx-click="select-filter"
,
phx-value-filter="DEPLOY_ACCOUNT"
>
Deploy Account
</div>
<div
class={"option #{if Map.get(assigns, :tx_filter) == "DEPLOY", do: "lg:!border-b-se-blue text-white", else: "lg:border-b-transparent text-gray-400"}"}
phx-click="select-filter"
,
phx-value-filter="DEPLOY"
>
Deploy
</div>
<div
class={"option #{if Map.get(assigns, :tx_filter) == "DECLARE", do: "lg:!border-b-se-blue text-white", else: "lg:border-b-transparent text-gray-400"}"}
phx-click="select-filter"
,
phx-value-filter="DECLARE"
,
>
Declare
</div>
<div
class={"option #{if Map.get(assigns, :tx_filter) == "L1_HANDLER", do: "lg:!border-b-se-blue text-white", else: "lg:border-b-transparent text-gray-400"}"}
phx-click="select-filter"
,
phx-value-filter="L1_HANDLER"
,
>
L1 Handler
</div>
</div>
</div>
</div>
</div>
"""
end

@impl true
def mount(_params, _session, socket) do
page = Transaction.paginate_transactions_for_index(%{}, socket.assigns.network)

{:ok,
assign(socket,
page: page,
active_pagination_id: ""
active_pagination_id: "",
tx_filter: "ALL"
)}
end

Expand All @@ -104,15 +179,20 @@ defmodule StarknetExplorerWeb.TransactionIndexLive do
)}
end

@impl true
def handle_event("select-filter", %{"filter" => filter}, socket) do
pagination(socket, 1, filter)
end

@impl true
def handle_event("inc_txs", _value, socket) do
new_page_number = socket.assigns.page.page_number + 1
pagination(socket, new_page_number)
pagination(socket, new_page_number, Map.get(socket.assigns, :tx_filter, "ALL"))
end

def handle_event("dec_txs", _value, socket) do
new_page_number = socket.assigns.page.page_number - 1
pagination(socket, new_page_number)
pagination(socket, new_page_number, Map.get(socket.assigns, :tx_filter, "ALL"))
end

@impl true
Expand All @@ -122,21 +202,22 @@ defmodule StarknetExplorerWeb.TransactionIndexLive do
socket
) do
new_page_number = String.to_integer(page_number)
pagination(socket, new_page_number)
pagination(socket, new_page_number, Map.get(socket.assigns, :tx_filter, "ALL"))
end

def handle_event("toggle-page-edit", %{"target" => target}, socket) do
socket = assign(socket, active_pagination_id: target)
{:noreply, push_event(socket, "focus", %{id: target})}
end

def pagination(socket, new_page_number) do
def pagination(socket, new_page_number, filter \\ "ALL") do
page =
Transaction.paginate_transactions_for_index(
%{page: new_page_number},
socket.assigns.network
socket.assigns.network,
filter
)

{:noreply, assign(socket, page: page)}
{:noreply, assign(socket, page: page, tx_filter: filter)}
end
end

0 comments on commit eca1165

Please sign in to comment.