From 5676012a3708f3a01132dbfa9195e944b703d288 Mon Sep 17 00:00:00 2001 From: Vadim Date: Wed, 18 Dec 2019 17:14:14 +0300 Subject: [PATCH] Prepare Recalculate button for Claim Reward --- .../assets/js/pages/stakes/claim_reward.js | 82 ++++++++++++++++++- .../channels/stakes_channel.ex | 1 + .../stakes/_stakes_btn_claim_reward.html.eex | 3 + .../stakes/_stakes_btn_recalculate.html.eex | 34 ++++++++ ...stakes_modal_claim_reward_content.html.eex | 5 +- 5 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_recalculate.html.eex diff --git a/apps/block_scout_web/assets/js/pages/stakes/claim_reward.js b/apps/block_scout_web/assets/js/pages/stakes/claim_reward.js index 106ed2b374..18b0dbc759 100644 --- a/apps/block_scout_web/assets/js/pages/stakes/claim_reward.js +++ b/apps/block_scout_web/assets/js/pages/stakes/claim_reward.js @@ -54,13 +54,17 @@ function onPoolsFound($modal, $modalBody) { const $poolsDropdown = $('[pool-select]', $modalBody) const $epochChoiceRadio = $('input[name="epoch_choice"]', $modalBody) const $specifiedEpochsText = $('.specified-epochs', $modalBody) + let allowedEpochs = [] $poolsDropdown.on('change', () => { const data = $('option:selected', this).data() const $poolInfo = $('.selected-pool-info', $modalBody) + const epochs = data.epochs ? data.epochs : '' + + allowedEpochs = expandEpochsToArray(epochs) $poolsDropdown.blur() - $('textarea', $poolInfo).val(data.epochs ? data.epochs : '') + $('textarea', $poolInfo).val(epochs) $('#token-reward-sum', $poolInfo).html(data.tokenRewardSum ? data.tokenRewardSum : '0') $('#native-reward-sum', $poolInfo).html(data.nativeRewardSum ? data.nativeRewardSum : '0') $('#tx-gas-limit', $poolInfo).html(data.gasLimit ? '~' + data.gasLimit : '0') @@ -71,6 +75,80 @@ function onPoolsFound($modal, $modalBody) { }) $epochChoiceRadio.on('change', () => { - $specifiedEpochsText.toggleClass('hidden') + if ($('#epoch-choice-all', $modalBody).is(':checked')) { + $specifiedEpochsText.addClass('hidden') + showRecalcButton(false, $modalBody) + } else { + $specifiedEpochsText.removeClass('hidden') + $specifiedEpochsText.trigger('input') + } + }) + + $specifiedEpochsText.on('input', () => { + const filtered = filterSpecifiedEpochs($specifiedEpochsText.val()) + const pointedEpochs = expandEpochsToArray(filtered) + const needsRecalc = pointedEpochs.length > 0 && !isArrayIncludedToArray(allowedEpochs, pointedEpochs) + showRecalcButton(needsRecalc, $modalBody) + $specifiedEpochsText.val(filtered) }) } + +function showRecalcButton(show, $modalBody) { + const $itemsToStrikeOut = $('#token-reward-sum, #native-reward-sum, #tx-gas-limit', $modalBody) + const $recalculateButton = $('button.recalculate', $modalBody) + const $submitButton = $('button.submit', $modalBody) + if (show) { + $itemsToStrikeOut.css('text-decoration', 'line-through') + $recalculateButton.removeClass('hidden') + $submitButton.addClass('hidden') + } else { + $itemsToStrikeOut.css('text-decoration', '') + $recalculateButton.addClass('hidden') + $submitButton.removeClass('hidden') + } +} + +function expandEpochsToArray(epochs) { + let filtered = epochs.replace(/[-|,]$/g, '').trim() + if (filtered == '') return [] + let ranges = filtered.split(',') + ranges = ranges.map((v) => { + if (v.indexOf('-') > -1) { + v = v.split('-') + v[0] = parseInt(v[0]) + v[1] = parseInt(v[1]) + v.sort((a, b) => a - b) + const min = v[0] + const max = v[1] + let expanded = [] + for (let i = min; i <= max; i++) { + expanded.push(i) + } + return expanded + } else { + return parseInt(v) + } + }) + ranges = ranges.reduce((acc, val) => acc.concat(val), []) // similar to ranges.flat() + ranges.sort((a, b) => a - b) + ranges = [...new Set(ranges)] // make unique + ranges = ranges.filter(epoch => epoch != 0) + return ranges +} + +function filterSpecifiedEpochs(epochs) { + let filtered = epochs + filtered = filtered.replace(/[^0-9,-]+/g, '') + filtered = filtered.replace(/-{2,}/g, '-') + filtered = filtered.replace(/,{2,}/g, ',') + filtered = filtered.replace(/,-/g, ',') + filtered = filtered.replace(/-,/g, '-') + filtered = filtered.replace(/(-[0-9]+)-/g, '$1,') + filtered = filtered.replace(/^[,|-|0]/g, '') + return filtered +} + +function isArrayIncludedToArray(source, target) { + const filtered = target.filter(item => source.indexOf(item) != -1) + return filtered.length == source.length +} diff --git a/apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex b/apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex index daae4283c9..b37d144c2c 100644 --- a/apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex +++ b/apps/block_scout_web/lib/block_scout_web/channels/stakes_channel.ex @@ -399,6 +399,7 @@ defmodule BlockScoutWeb.StakesChannel do {data, pool_staking_address} end) + |> Enum.filter(fn {data, _} -> data.epochs != "" end) pools_gas_estimates = Enum.map(pools, fn {_data, pool_staking_address} -> result = ContractReader.claim_reward_estimate_gas( diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_claim_reward.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_claim_reward.html.eex index 96a04a09ba..a36547d971 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_claim_reward.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_claim_reward.html.eex @@ -1,3 +1,6 @@ \ No newline at end of file diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_recalculate.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_recalculate.html.eex new file mode 100644 index 0000000000..9d8813f7d7 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_btn_recalculate.html.eex @@ -0,0 +1,34 @@ + \ No newline at end of file diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim_reward_content.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim_reward_content.html.eex index 81ca12b130..1643b5da1c 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim_reward_content.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_claim_reward_content.html.eex @@ -41,15 +41,14 @@ <%= gettext("You will receive:") %>
<%= @token.symbol %>
<%= @coin.symbol %>
-

<%= gettext("Tx Gas Limit:") %>

-

- <%= render BlockScoutWeb.StakesView, "_stakes_btn_withdraw.html", text: gettext("Claim Reward"), extra_class: "full-width" %> + <%= render BlockScoutWeb.StakesView, "_stakes_btn_recalculate.html", text: gettext("Recalculate"), extra_class: "full-width recalculate hidden" %> + <%= render BlockScoutWeb.StakesView, "_stakes_btn_withdraw.html", text: gettext("Claim Reward"), extra_class: "full-width submit" %> <% else %>