Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: amount input add startTransition #73

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/screens/transfer/AmountPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useState, useTransition } from 'react';
import { useNavigate } from 'react-router-dom';
import { Button, CircularProgress, Input } from '@nextui-org/react';

Expand All @@ -17,6 +17,7 @@ export default function AmountPage() {
const { estimateFee, getExistentialDeposit } = useExtrinsicProvider();
const { BackButton } = useTelegram();
const { hideMainButton, reset, addMainButton, mainButton } = useMainButton();
const [isPending, startTransition] = useTransition();

const { selectedAsset, setSelectedAsset } = useGlobalContext();

Expand Down Expand Up @@ -86,22 +87,23 @@ export default function AmountPage() {
setIsAmountValid(Boolean(maxAmountToSend) && validateGift);
};

const handleChange = async (value: string) => {
const handleChange = (value: string) => {
const formattedValue = value.trim().replace(/,/g, '.');
setTransferAll(false);
const validateGift = selectedAsset?.isGift ? !!deposit && +formattedValue >= deposit : true;
startTransition(async () => {
const { max, fee, formattedDeposit } = await getTransferDetails(
selectedAsset as TrasferAsset,
formattedValue,
estimateFee,
getExistentialDeposit,
);

const { max, fee, formattedDeposit } = await getTransferDetails(
selectedAsset as TrasferAsset,
formattedValue,
estimateFee,
getExistentialDeposit,
);

setDeposit(formattedDeposit);
setMaxAmountToSend(max);
setSelectedAsset((prev) => ({ ...prev!, fee }));
setIsAmountValid(!!Number(formattedValue) && +formattedValue <= +max && validateGift);
setDeposit(formattedDeposit);
setMaxAmountToSend(max);
setSelectedAsset((prev) => ({ ...prev!, fee }));
setIsAmountValid(!!Number(formattedValue) && +formattedValue <= +max && validateGift);
});
setAmount(formattedValue);
};

Expand All @@ -124,7 +126,7 @@ export default function AmountPage() {
)}
<Button variant="light" size="md" className="p-2" onClick={handleMaxSend}>
<HeadlineText className="text-text-link">
Max: {maxAmountToSend || <CircularProgress size="sm" className="inline-block h-[22px]" />}{' '}
Max: {maxAmountToSend || <CircularProgress size="sm" className="inline-block h-[22px]" />}
{selectedAsset?.asset?.symbol}
</HeadlineText>
</Button>
Expand All @@ -135,7 +137,7 @@ export default function AmountPage() {
<Input
fullWidth={false}
variant="underlined"
className="mt-[-10px] font-manrope"
className="mt-[-10px] font-manrope h-full"
classNames={{ input: ['text-right !text-large-title max-w-[160px]'] }}
value={amount}
isInvalid={!isAmountValid}
Expand All @@ -146,7 +148,11 @@ export default function AmountPage() {
/>
</div>
<div className="grid grid-cols-[auto,1fr]">
<TokenPrice priceId={selectedAsset?.asset?.priceId} balance={amount || '0'} showBalance={isAmountValid} />
<TokenPrice
priceId={selectedAsset?.asset?.priceId}
balance={amount || '0'}
showBalance={isAmountValid && !isPending}
/>
{!isAmountValid && (
<>
<BodyText align="right" className="text-text-danger">
Expand Down