/
ManageVotingPowerPopup.svelte
105 lines (94 loc) · 4.06 KB
/
ManageVotingPowerPopup.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<script lang="ts">
import { Button, Text, TextHint, AssetAmountInput } from 'shared/components'
import { HTMLButtonType, TextType } from 'shared/components/enums'
import { selectedAccount } from '@core/account/stores'
import { handleError } from '@core/error/handlers'
import { setVotingPower } from '@contexts/governance/actions'
import { localize } from '@core/i18n'
import { checkActiveProfileAuth } from '@core/profile/actions'
import { convertToRawAmount, visibleSelectedAccountAssets } from '@core/wallet'
import { closePopup, openPopup } from '@auxiliary/popup/actions'
import { popupState } from '@auxiliary/popup/stores'
import { hasToRevote, hasPendingGovernanceTransaction } from '@contexts/governance/stores'
import { onMount } from 'svelte'
import { modifyPopupState } from '@auxiliary/popup/helpers'
import { isSelectedAccountVoting } from '@contexts/governance/utils'
export let _onMount: (..._: any[]) => Promise<void> = async () => {}
export let newVotingPower: string = undefined
let assetAmountInput: AssetAmountInput
let amount: string
let rawAmount = newVotingPower ?? $selectedAccount?.votingPower
$: asset = $visibleSelectedAccountAssets?.baseCoin
$: votingPower = parseInt($selectedAccount?.votingPower, 10)
$: isTransferring = $hasPendingGovernanceTransaction?.[$selectedAccount.index] || $selectedAccount?.isTransferring
$: disabled = getDisabled(amount, $hasToRevote, isTransferring)
function getDisabled(amount: string, hasToRevote: boolean, isTransferring: boolean): boolean {
if (!amount) {
return hasToRevote || isTransferring
}
const convertedSliderAmount = convertToRawAmount(amount, asset?.metadata).toString()
return convertedSliderAmount === $selectedAccount?.votingPower || isTransferring
}
function onCancelClick(): void {
closePopup()
}
async function onSubmit(): Promise<void> {
try {
await assetAmountInput?.validate(true)
const isVoting = isSelectedAccountVoting()
if (amount === '0' && isVoting) {
openPopup({ type: 'votingPowerToZero' })
return
}
// After unlocking stronghold popup, the popup tracks newVotingPower to show it when reopened.
$popupState.props = { newVotingPower: rawAmount }
await checkActiveProfileAuth(
async () => {
await setVotingPower(rawAmount, isVoting)
},
{ stronghold: true, ledger: false }
)
} catch (err) {
handleError(err)
}
}
onMount(async () => {
disabled = true
try {
await _onMount()
if ($hasToRevote) {
modifyPopupState({ ...$popupState, preventClose: true, hideClose: true })
} else {
disabled = $selectedAccount?.isTransferring
}
} catch (err) {
disabled = false
handleError(err)
}
})
</script>
<form id="manage-voting-power" on:submit|preventDefault={onSubmit}>
<Text type={TextType.h4} classes="mb-3">{localize('popups.manageVotingPower.title')}</Text>
<Text type={TextType.p} classes="mb-5">{localize('popups.manageVotingPower.body')}</Text>
<div class="space-y-4 mb-6">
<AssetAmountInput
bind:this={assetAmountInput}
bind:rawAmount
bind:amount
{asset}
containsSlider
disableAssetSelection
{disabled}
{votingPower}
/>
<TextHint info text={localize('popups.manageVotingPower.hint')} />
</div>
<div class="flex flex-row flex-nowrap w-full space-x-4">
<Button outline disabled={isTransferring} classes="w-full" onClick={onCancelClick}>
{localize('actions.cancel')}
</Button>
<Button type={HTMLButtonType.Submit} {disabled} isBusy={isTransferring} classes="w-full">
{localize('actions.confirm')}
</Button>
</div>
</form>