From 7f3f983ceb658b84ac8b63f33bbdd3d1429fff25 Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 26 May 2020 17:03:59 +0300 Subject: [PATCH] Fix 'Stake' and 'Become a Candidate' popups --- .../assets/js/pages/stakes/become_candidate.js | 12 ++++++++++-- .../lib/block_scout_web/channels/stakes_channel.ex | 13 ++++++++----- .../stakes/_stakes_modal_become_candidate.html.eex | 12 +++++++++--- .../templates/stakes/_stakes_modal_stake.html.eex | 6 +++--- .../stakes/_stakes_modal_withdraw.html.eex | 5 +++-- 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/apps/block_scout_web/assets/js/pages/stakes/become_candidate.js b/apps/block_scout_web/assets/js/pages/stakes/become_candidate.js index b10fac526e..5afc1f23fb 100644 --- a/apps/block_scout_web/assets/js/pages/stakes/become_candidate.js +++ b/apps/block_scout_web/assets/js/pages/stakes/become_candidate.js @@ -16,9 +16,10 @@ export function openBecomeCandidateModal (store) { .push('render_become_candidate') .receive('ok', msg => { const $modal = $(msg.html) + const $form = $modal.find('form') setupValidation( - $modal.find('form'), + $form, { 'candidate-stake': value => isCandidateStakeValid(value, store, msg), 'mining-address': value => isMiningAddressValid(value, store) @@ -26,7 +27,14 @@ export function openBecomeCandidateModal (store) { $modal.find('form button') ) - $modal.find('form').submit(() => { + $modal.find('[data-available-amount]').click(e => { + const amount = $(e.currentTarget).data('available-amount') + $('[candidate-stake]', $form).val(amount).trigger('input') + $('.tooltip').tooltip('hide') + return false + }) + + $form.submit(() => { becomeCandidate($modal, store, msg) return false }) 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 62ef1a1daa..8be45d5d55 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 @@ -107,7 +107,7 @@ defmodule BlockScoutWeb.StakesChannel do end def handle_in("render_become_candidate", _, socket) do - min_candidate_stake = ContractState.get(:min_candidate_stake) + min_candidate_stake = Decimal.new(ContractState.get(:min_candidate_stake)) token = ContractState.get(:token) balance = Chain.fetch_last_token_balance(socket.assigns.account, token.contract_address_hash) @@ -140,19 +140,22 @@ defmodule BlockScoutWeb.StakesChannel do ContractState.get(:min_delegator_stake) end + min_stake = Decimal.new(min_stake) + delegator_staked = Decimal.new((delegator && delegator.stake_amount) || 0) + html = View.render_to_string(StakesView, "_stakes_modal_stake.html", - min_stake: min_stake, balance: balance, - token: token, + delegator_staked: delegator_staked, + min_stake: min_stake, pool: pool, - delegator: delegator + token: token ) result = %{ html: html, balance: balance, - delegator_staked: (delegator && delegator.stake_amount) || 0, + delegator_staked: delegator_staked, min_stake: min_stake, self_staked_amount: pool.self_staked_amount, total_staked_amount: pool.total_staked_amount diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_become_candidate.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_become_candidate.html.eex index d919e20a44..b843eb13e1 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_become_candidate.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_become_candidate.html.eex @@ -16,9 +16,15 @@

<%= gettext("Your Balance:") %> - - <%= format_token_amount(@balance, @token) %> - + <%= if @balance >= @min_candidate_stake do %> + + <%= format_token_amount(@balance, @token) %> + + <% else %> + + <%= format_token_amount(@balance, @token) %> + + <% end %>

<%= render BlockScoutWeb.CommonComponentsView, "_btn_add_full.html", text: gettext("Become a Candidate"), extra_class: "full-width" %>
diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_stake.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_stake.html.eex index ed188727ac..dd5baec303 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_stake.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_stake.html.eex @@ -10,12 +10,12 @@
<%= render BlockScoutWeb.CommonComponentsView, "_input_group.html", id: "delegator-stake", classes: "form-group", input_classes: "form-control n-b-r", attributes: "delegator-stake", type: "text", placeholder: gettext("Amount"), prepend: @token.symbol %> - <% has_stake = @delegator && Decimal.positive?(@delegator.stake_amount) %> + <% has_stake = Decimal.positive?(@delegator_staked) %> <%= if has_stake do %>

<%= gettext("Your Current Stake:") %> - <%= format_token_amount(@delegator.stake_amount, @token) %> + <%= format_token_amount(@delegator_staked, @token) %>

<% else %> @@ -26,7 +26,7 @@

<% end %>

<%= gettext("Your Balance:") %> - <%= if Decimal.positive?(@balance) do %> + <%= if Decimal.add(@delegator_staked, @balance) >= @min_stake do %> <%= format_token_amount(@balance, @token) %> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_withdraw.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_withdraw.html.eex index c0cd5368ff..1d0914c35d 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_withdraw.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/stakes/_stakes_modal_withdraw.html.eex @@ -36,11 +36,12 @@ %> <% end %> <%= if Decimal.positive?(@delegator.max_withdraw_allowed) do %> -

<%= gettext("Available Now:") %> +

<%= gettext("Available Now:") %> <%= format_token_amount(@delegator.max_withdraw_allowed, @token) %> -

+ <%= render BlockScoutWeb.CommonComponentsView, "_i_tooltip.html", text: gettext("You can withdraw this amount right now.") %> +
<%= render BlockScoutWeb.StakesView, "_stakes_btn_withdraw.html",