Skip to content

Commit

Permalink
Merge pull request #243 from CityOfZion/CU-86a5jjanf-1
Browse files Browse the repository at this point in the history
CU-86a5jjanf - NEON3 - BUG - Send screen isn't enabling the send button
  • Loading branch information
hotequil authored Nov 15, 2024
2 parents cb5b0ed + 416dabf commit d7c644c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/renderer/src/components/TransactionFeeActionStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export const TransactionFeeActionStep = ({ fee, isCalculatingFee, service }: TPr
) : (
<div className="flex items-center gap-4.5">
<span className="text-gray-100">
{fee} {service?.feeToken.symbol}
{fee ?? '0.00'} {service?.feeToken.symbol}
</span>

<span className="text-gray-300">{NumberHelper.currency(fiatFee, currency.label)}</span>
</div>
)}
Expand Down
63 changes: 35 additions & 28 deletions src/renderer/src/routes/pages/Send/SendPageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,27 @@ export const SendPageContent = ({ account, recipientAddress }: TProps) => {

for (const recipient of recipients) {
if (!recipient.token || !recipient.amount || !recipient.address) {
if (!recipient.amount) {
clearErrors('selectedAccount')
}

setError('recipients', '')
return
}

const amountNumber = NumberHelper.number(recipient.amount)
const tokenHash = UtilsHelper.normalizeHash(recipient.token!.token.hash)
const tokenBalance = balance.data?.tokensBalances.find(
tokenBalance => UtilsHelper.normalizeHash(tokenBalance.token.hash) === tokenHash
)

if (!tokenBalance || amountNumber > tokenBalance.amountNumber) {
setError('selectedAccount', t('errors.insufficientFunds'))
return
}
}

clearErrors('recipients')
clearErrors(['recipients', 'selectedAccount'])
}

const handleSelectAccount = (account?: IAccountState) => {
Expand Down Expand Up @@ -213,8 +228,14 @@ export const SendPageContent = ({ account, recipientAddress }: TProps) => {
useEffect(() => {
if (balance.isLoading) return

const abortController = new AbortController()

const handleCalculateFee = async () => {
try {
// It works as a debounce
await UtilsHelper.sleep(1000)
if (abortController.signal.aborted) return

const fields = await getSendFields()
if (!fields || !isCalculableFee(fields.service)) {
setData({ fee: undefined })
Expand All @@ -229,7 +250,7 @@ export const SendPageContent = ({ account, recipientAddress }: TProps) => {
})

setData({
fee: `${fee} ${fields.service.feeToken.symbol}`,
fee,
})

let totalFeeAmount = NumberHelper.number(fee)
Expand All @@ -255,40 +276,21 @@ export const SendPageContent = ({ account, recipientAddress }: TProps) => {
} catch (error) {
console.error(error)
ToastHelper.error({ message: t('errors.feeError') })
setError('fee', t('errors.feeError'))
setData({ fee: undefined })
throw error
} finally {
setData({ isCalculatingFee: false })
}
}

handleCalculateFee()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [actionData.recipients, balance.data])

useEffect(() => {
const validateAmounts = () => {
for (const recipient of actionData.recipients) {
if (!recipient.amount || !recipient.token) {
continue
}

const amountNumber = NumberHelper.number(recipient.amount)
const tokenHash = UtilsHelper.normalizeHash(recipient.token!.token.hash)
const tokenBalance = balance.data?.tokensBalances.find(
tokenBalance => UtilsHelper.normalizeHash(tokenBalance.token.hash) === tokenHash
)

if (!tokenBalance || amountNumber > tokenBalance.amountNumber) {
setError('recipients', t('errors.insufficientFunds'))
return
}
}

clearErrors('recipients')
return () => {
abortController.abort()
}

validateAmounts()
}, [actionData.recipients, balance.data?.tokensBalances, clearErrors, setError, t])
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [actionData.recipients, balance.data])

useEffect(() => {
handleSelectAccount(account)
Expand Down Expand Up @@ -359,7 +361,12 @@ export const SendPageContent = ({ account, recipientAddress }: TProps) => {
/>
)}

{actionState.errors.fee && <AlertErrorBanner className="w-full mt-2" message={actionState.errors.fee} />}
{(actionState.errors.fee || actionState.errors.selectedAccount) && (
<AlertErrorBanner
className="w-full mt-2"
message={actionState.errors.fee || actionState.errors.selectedAccount || ''}
/>
)}

<Button
className="max-w-[16rem] w-full mt-4"
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/routes/pages/Send/SendRecipient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export const SendRecipient = ({
leftIcon={<VscCircleFilled className="text-gray-300 w-2 h-2" />}
>
<GreyAmountInput
value={recipient.amount}
value={recipient.amount ?? ''}
onChange={handleChangeAmount}
disabled={!selectedAccount || !recipient.token}
/>
Expand Down

0 comments on commit d7c644c

Please sign in to comment.