Skip to content

Commit

Permalink
refactor(finance-quick-transfer): improve amount handling logic
Browse files Browse the repository at this point in the history
- Introduce `MAX_POOL` constant to limit the pool size to 1 billion.
- Refactor logic to handle amount constraints consistently:
  - Add helper function `handleAmountConstraints` to centralize validation.
  - Update `handleChangeInput` and `handleBlur` to use the new helper.
- Adjust `InputAmount` component to use `MAX_POOL` instead of `MAX_AMOUNT`.
  • Loading branch information
cswni committed Jan 9, 2025
1 parent 84a1a7e commit 58fec1e
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/sections/finance/components/finance-quick-transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import FinanceSearchProfileModal from '@src/sections/finance/components/finance-

const STEP = 50;
const MIN_AMOUNT = 0;
// A thousand millions allowed in the pool
const MAX_POOL: number = 1000000000;

interface Props extends CardProps {
title?: string;
Expand Down Expand Up @@ -202,29 +204,34 @@ export default function FinanceQuickTransfer({
}
}, [MAX_AMOUNT]);

// Handle changes in the input for the amount
const handleChangeInput = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
setAmount(Number(event.target.value));
if(Number(event.target.value) < MAX_AMOUNT) {
setCanContinue(true);
}else{
setCanContinue(false);
// Helper function to handle amount constraints
const handleAmountConstraints = (value: number, MAX_AMOUNT: number) => {
if (value > MAX_POOL) {
value = MAX_POOL; // Truncate to a thousand millions
}
}, []);

// Validate the amount on blur
const handleBlur = useCallback(() => {
if(amount < MAX_AMOUNT) {
setCanContinue(true);
if (value < 0) {
value = 0; // Set amount to 0 if lower than 0
}
setAmount(value);
setCanContinue(value <= MAX_AMOUNT);

if (amount < 0) {
setAmount(0);
} else if (amount > MAX_AMOUNT) {
// If amount is greater than balance, allow input but setCanContinue to false
if (value > MAX_AMOUNT) {
setCanContinue(false);
}
};

const handleChangeInput = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
const value = Number(event.target.value);
handleAmountConstraints(value, MAX_AMOUNT);
}, [MAX_AMOUNT]);


const handleBlur = useCallback(() => {
handleAmountConstraints(amount, MAX_AMOUNT);
}, [amount, MAX_AMOUNT]);


// Called after finishing a transfer
const handleTransferFinish = () => {
setAmount(0);
Expand Down Expand Up @@ -371,7 +378,7 @@ export default function FinanceQuickTransfer({
const renderInput = (
<Stack spacing={3}>
<InputAmount
max={MAX_AMOUNT}
max={MAX_POOL}
amount={amount}
onBlur={handleBlur}
onChange={handleChangeInput}
Expand Down

0 comments on commit 58fec1e

Please sign in to comment.