Skip to content
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
20 changes: 9 additions & 11 deletions src/pages/iou/steps/MoneyRequestAmountForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const getNewSelection = (oldSelection, prevLength, newLength) => {
return {start: cursorPosition, end: cursorPosition};
};

const isAmountValid = (amount) => !amount.length || parseFloat(amount) < 0.01;
const isAmountInvalid = (amount) => !amount.length || parseFloat(amount) < 0.01;

const AMOUNT_VIEW_ID = 'amountView';
const NUM_PAD_CONTAINER_VIEW_ID = 'numPadContainerView';
Expand All @@ -74,8 +74,6 @@ function MoneyRequestAmountForm({amount, currency, isEditing, forwardedRef, onCu
const selectedAmountAsString = amount ? CurrencyUtils.convertToFrontendAmount(amount).toString() : '';

const [currentAmount, setCurrentAmount] = useState(selectedAmountAsString);
const [isInvalidAmount, setIsInvalidAmount] = useState(isAmountValid(selectedAmountAsString));
const [firstPress, setFirstPress] = useState(false);
const [formError, setFormError] = useState('');
const [shouldUpdateSelection, setShouldUpdateSelection] = useState(true);

Expand Down Expand Up @@ -135,17 +133,17 @@ function MoneyRequestAmountForm({amount, currency, isEditing, forwardedRef, onCu
setSelection((prevSelection) => ({...prevSelection}));
return;
}
const checkInvalidAmount = isAmountValid(newAmountWithoutSpaces);
setIsInvalidAmount(checkInvalidAmount);
setFormError(checkInvalidAmount ? 'iou.error.invalidAmount' : '');
if (!_.isEmpty(formError)) {
setFormError('');
}
setCurrentAmount((prevAmount) => {
const strippedAmount = MoneyRequestUtils.stripCommaFromAmount(newAmountWithoutSpaces);
const isForwardDelete = prevAmount.length > strippedAmount.length && forwardDeletePressedRef.current;
setSelection((prevSelection) => getNewSelection(prevSelection, isForwardDelete ? strippedAmount.length : prevAmount.length, strippedAmount.length));
return strippedAmount;
});
},
[decimals],
[decimals, formError],
);

// Modifies the amount to match the decimals for changed currency.
Expand Down Expand Up @@ -204,13 +202,13 @@ function MoneyRequestAmountForm({amount, currency, isEditing, forwardedRef, onCu
* Submit amount and navigate to a proper page
*/
const submitAndNavigateToNextPage = useCallback(() => {
if (isInvalidAmount) {
setFirstPress(true);
if (isAmountInvalid(currentAmount)) {
setFormError('iou.error.invalidAmount');
return;
}

onSubmitButtonPress(currentAmount);
}, [onSubmitButtonPress, currentAmount, isInvalidAmount]);
}, [onSubmitButtonPress, currentAmount]);

/**
* Input handler to check for a forward-delete key (or keyboard shortcut) press.
Expand Down Expand Up @@ -263,7 +261,7 @@ function MoneyRequestAmountForm({amount, currency, isEditing, forwardedRef, onCu
onKeyPress={textInputKeyPress}
/>
</View>
{!_.isEmpty(formError) && firstPress && (
{!_.isEmpty(formError) && (
<FormHelpMessage
style={[styles.ph5]}
isError
Expand Down