Implement filters for staking pool tables (#2558)

staking
Paul Tsupikoff 5 years ago committed by Victor Baranov
parent d020fc9baa
commit 31b90102de
  1. 33
      apps/block_scout_web/assets/js/pages/stakes.js
  2. 11
      apps/block_scout_web/lib/block_scout_web/controllers/stakes_controller.ex
  3. 4
      apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_title.html.eex
  4. 30
      apps/explorer/lib/explorer/chain.ex

@ -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)

@ -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)

@ -3,13 +3,13 @@
<div class="card-title-controls">
<%= if @show_banned_checkbox do %>
<div class="check card-title-control">
<input type="checkbox" />
<input type="checkbox" pool-filter-banned />
<div class="check-icon"></div>
<div class="check-text">Show banned only</div>
</div>
<% end %>
<div class="check card-title-control">
<input type="checkbox" />
<input type="checkbox" pool-filter-my />
<div class="check-icon"></div>
<div class="check-text">Show only those I staked into</div>
</div>

@ -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

Loading…
Cancel
Save