From 31b90102dea1fc61b653b8c03df9dd0750dae30c Mon Sep 17 00:00:00 2001 From: Paul Tsupikoff Date: Mon, 19 Aug 2019 11:19:39 +0200 Subject: [PATCH] Implement filters for staking pool tables (#2558) --- .../block_scout_web/assets/js/pages/stakes.js | 33 +++++++++++++++++-- .../controllers/stakes_controller.ex | 11 ++++++- .../templates/stakes/_stakes_title.html.eex | 4 +-- apps/explorer/lib/explorer/chain.ex | 30 ++++++++++++++--- 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/apps/block_scout_web/assets/js/pages/stakes.js b/apps/block_scout_web/assets/js/pages/stakes.js index b40105db19..5ad37bac77 100644 --- a/apps/block_scout_web/assets/js/pages/stakes.js +++ b/apps/block_scout_web/assets/js/pages/stakes.js @@ -42,7 +42,17 @@ export function reducer (state = initialState, action) { case 'ACCOUNT_UPDATED': { return Object.assign({}, state, { account: action.account, - additionalParams: { account: action.account } + additionalParams: Object.assign({}, state.additionalParams, { + account: action.account + }) + }) + } + case 'FILTERS_UPDATED': { + return Object.assign({}, state, { + additionalParams: Object.assign({}, state.additionalParams, { + filterBanned: action.filterBanned, + filterMy: action.filterMy + }) }) } case 'RECEIVED_UPDATE': { @@ -67,7 +77,13 @@ export function reducer (state = initialState, action) { const elements = { '[data-page="stakes"]': { load ($el) { - return { refreshInterval: $el.data('refresh-interval') || null } + return { + refreshInterval: $el.data('refresh-interval') || null, + additionalParams: { + filterBanned: $el.find('[pool-filter-banned]').prop('checked'), + filterMy: $el.find('[pool-filter-my]').prop('checked') + } + } } } } @@ -123,9 +139,22 @@ if ($stakesPage.length) { .on('click', '.js-move-stake', event => openMoveStakeModal(event, store)) .on('click', '.js-withdraw-stake', event => openWithdrawStakeModal(event, store)) + $stakesPage + .on('change', '[pool-filter-banned]', () => updateFilters(store)) + .on('change', '[pool-filter-my]', () => updateFilters(store)) + initializeWeb3(store) } +function updateFilters (store) { + store.dispatch({ + type: 'FILTERS_UPDATED', + filterBanned: $stakesPage.find('[pool-filter-banned]').prop('checked'), + filterMy: $stakesPage.find('[pool-filter-my]').prop('checked') + }) + refreshPage(store) +} + function initializeWeb3 (store) { if (window.ethereum) { const web3 = new Web3(window.ethereum) diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex index da4bdcd442..4371f19a2f 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex @@ -48,7 +48,16 @@ defmodule BlockScoutWeb.StakesController do |> Map.get("position", "0") |> String.to_integer() - pools_plus_one = Chain.staking_pools(filter, options, params["account"]) + pools_plus_one = + Chain.staking_pools( + filter, + options, + unless params["account"] == "" do + params["account"] + end, + params["filterBanned"] == "true", + params["filterMy"] == "true" + ) {pools, next_page} = split_list_by_page(pools_plus_one) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_title.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_title.html.eex index f90b49b962..030486e63c 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_title.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_title.html.eex @@ -3,13 +3,13 @@
<%= if @show_banned_checkbox do %>
- +
Show banned only
<% end %>
- +
Show only those I staked into
diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index aad91ae872..e96e728540 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -4727,16 +4727,24 @@ defmodule Explorer.Chain do @spec staking_pools( filter :: :validator | :active | :inactive, paging_options :: PagingOptions.t() | :all, - delegator_address_hash :: Hash.t() | nil + delegator_address_hash :: Hash.t() | nil, + filter_banned :: boolean() | nil, + filter_my :: boolean() | nil ) :: [map()] - def staking_pools(filter, paging_options \\ @default_paging_options, delegator_address_hash \\ nil) do + def staking_pools( + filter, + paging_options \\ @default_paging_options, + delegator_address_hash \\ nil, + filter_banned \\ false, + filter_my \\ false + ) do base_query = StakingPool |> where(is_deleted: false) |> staking_pool_filter(filter) |> staking_pools_paging_query(paging_options) - query = + delegator_query = if delegator_address_hash do base_query |> join(:left, [p], pd in StakingPoolsDelegator, @@ -4748,7 +4756,21 @@ defmodule Explorer.Chain do |> select([p], %{pool: p, delegator: nil}) end - Repo.all(query) + banned_query = + if filter_banned do + where(delegator_query, is_banned: true) + else + delegator_query + end + + filtered_query = + if delegator_address_hash && filter_my do + where(banned_query, [..., pd], not is_nil(pd)) + else + banned_query + end + + Repo.all(filtered_query) end defp staking_pools_paging_query(base_query, :all), do: base_query