Skip to content

Commit 3134a4b

Browse files
fix: reactivity problem between withdrawal form and amount range component (#43)
* feat: update mountRangeInput component and reset form properly * fix: remove afterUpdate to prevent disabled input * fix: add a hack to force a re-render of the component --------- Co-authored-by: Begoña Alvarez <[email protected]>
1 parent 08f625f commit 3134a4b

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

src/components/inputs/amount-range.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<script lang="ts">
2+
import { onMount } from 'svelte';
3+
24
export let decimals: number = 0;
35
export let min: number = 0;
46
export let max: number = 0;
5-
export let value: number = 0;
7+
export let value: number;
68
export let disabled: boolean = false;
79
export let label: string = '';
810
export let valid: boolean = true;
@@ -15,6 +17,11 @@
1517
$: maxValueFormatted = max / 10 ** decimals;
1618
$: valid = value >= min && value <= max;
1719
20+
onMount(() => {
21+
value = Math.max(min, Math.min(max, value));
22+
valueFormatted = value / 10 ** decimals;
23+
});
24+
1825
function handleRangeChange(event): void {
1926
value = event.target.value;
2027
valueFormatted = value / 10 ** decimals;

src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const EVMAddressLength = 42;
22
export const NativeTokenIDLength = 38;
33

4+
export const L1_BASE_TOKEN_DECIMALS = 6;
45
export const L2_NATIVE_GAS_TOKEN_DECIMALS = 18;
56
export const wSMR_TOKEN_DECIMALS = 18;

src/routes/sections/withdraw-section.svelte

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import { AmountRangeInput, Button, Input, Select } from '$components';
44
import { getBech32AddressLengthFromChain, truncateText } from '$lib/common';
55
import { InputType } from '$lib/common/enums';
6-
import { L2_NATIVE_GAS_TOKEN_DECIMALS } from '$lib/constants';
6+
import {
7+
L2_NATIVE_GAS_TOKEN_DECIMALS,
8+
L1_BASE_TOKEN_DECIMALS,
9+
} from '$lib/constants';
710
import {
811
appConfiguration,
912
nodeClient,
@@ -20,7 +23,7 @@
2023
withdrawStateStore,
2124
} from '$lib/withdraw';
2225
23-
const formInput: WithdrawFormInput = {
26+
let formInput: WithdrawFormInput = {
2427
receiverAddress: '',
2528
baseTokensToSend: storageDeposit,
2629
nativeTokensToSend: {},
@@ -49,6 +52,10 @@
4952
$: $withdrawStateStore, updateFormInput();
5053
$: placeholderHrp = `${$appConfiguration?.bech32Hrp.toLowerCase()}...`;
5154
55+
$: storageDepositAdjustedDecimals =
56+
storageDeposit *
57+
10 ** (L2_NATIVE_GAS_TOKEN_DECIMALS - L1_BASE_TOKEN_DECIMALS);
58+
5259
function updateFormInput() {
5360
if (formInput.baseTokensToSend > $withdrawStateStore.availableBaseTokens) {
5461
formInput.baseTokensToSend = 0;
@@ -132,7 +139,7 @@
132139
}
133140
134141
if (result.status) {
135-
resetForm();
142+
resetView();
136143
showNotification({
137144
type: NotificationType.Success,
138145
message: `Withdraw request sent. BlockIndex: ${result.blockNumber}`,
@@ -221,11 +228,15 @@
221228
});
222229
};
223230
224-
function resetForm(): void {
231+
let resetReactiveVariable = 0; // This is a hack to force a re-render of the component
232+
function resetView(): void {
225233
formInput.receiverAddress = '';
226234
formInput.baseTokensToSend = storageDeposit;
227235
formInput.nativeTokensToSend = {};
228236
formInput.nftIDToSend = null;
237+
formInput = formInput;
238+
239+
resetReactiveVariable++;
229240
}
230241
</script>
231242

@@ -255,17 +266,19 @@
255266
<tokens-to-send-wrapper>
256267
<div class="mb-2">Tokens to send</div>
257268
<info-box class="flex flex-col space-y-4 max-h-96 overflow-auto">
258-
<AmountRangeInput
259-
label="{$appConfiguration?.ticker} Token:"
260-
bind:value={formInput.baseTokensToSend}
261-
disabled={!canSetAmountToWithdraw}
262-
min={storageDeposit}
263-
max={Math.max(
264-
$withdrawStateStore.availableBaseTokens - storageDeposit,
265-
0,
266-
)}
267-
decimals={L2_NATIVE_GAS_TOKEN_DECIMALS}
268-
/>
269+
{#key resetReactiveVariable}
270+
<AmountRangeInput
271+
label="{$appConfiguration?.ticker} Token:"
272+
bind:value={formInput.baseTokensToSend}
273+
disabled={!canSetAmountToWithdraw}
274+
min={storageDepositAdjustedDecimals}
275+
max={Math.max(
276+
$withdrawStateStore.availableBaseTokens - storageDeposit,
277+
0,
278+
)}
279+
decimals={L2_NATIVE_GAS_TOKEN_DECIMALS}
280+
/>
281+
{/key}
269282
{#each $withdrawStateStore.availableNativeTokens as nativeToken}
270283
<AmountRangeInput
271284
bind:value={formInput.nativeTokensToSend[nativeToken.id]}

0 commit comments

Comments
 (0)