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: prevent requesting for amounts larger than the allowed maximum #28266

Merged
merged 3 commits into from
Oct 2, 2023
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
40 changes: 35 additions & 5 deletions src/components/MoneyRequestConfirmationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {format} from 'date-fns';
import _ from 'underscore';
import {View} from 'react-native';
import lodashGet from 'lodash/get';
import {useIsFocused} from '@react-navigation/native';
import Text from './Text';
import styles from '../styles/styles';
import * as ReportUtils from '../libs/ReportUtils';
Expand Down Expand Up @@ -36,9 +37,11 @@ import tagPropTypes from './tagPropTypes';
import ConfirmedRoute from './ConfirmedRoute';
import transactionPropTypes from './transactionPropTypes';
import DistanceRequestUtils from '../libs/DistanceRequestUtils';
import FormHelpMessage from './FormHelpMessage';
import * as IOU from '../libs/actions/IOU';
import * as TransactionUtils from '../libs/TransactionUtils';
import * as PolicyUtils from '../libs/PolicyUtils';
import * as MoneyRequestUtils from '../libs/MoneyRequestUtils';

const propTypes = {
/** Callback to inform parent modal of success */
Expand Down Expand Up @@ -216,6 +219,13 @@ function MoneyRequestConfirmationList(props) {
props.isDistanceRequest ? currency : props.iouCurrencyCode,
);

const isFocused = useIsFocused();
const [formError, setFormError] = useState('');
useEffect(() => {
// reset the form error whenever the screen gains or loses focus
setFormError('');
}, [isFocused]);

useEffect(() => {
if (!shouldCalculateDistanceAmount) {
return;
Expand Down Expand Up @@ -370,8 +380,6 @@ function MoneyRequestConfirmationList(props) {
*/
const confirm = useCallback(
(paymentMethod) => {
setDidConfirm(true);

if (_.isEmpty(selectedParticipants)) {
return;
}
Expand All @@ -381,13 +389,22 @@ function MoneyRequestConfirmationList(props) {
return;
}

setDidConfirm(true);

Log.info(`[IOU] Sending money via: ${paymentMethod}`);
onSendMoney(paymentMethod);
} else {
// validate the amount for distance requests
if (props.isDistanceRequest && !isDistanceRequestWithoutRoute && !MoneyRequestUtils.validateAmount(String(props.iouAmount))) {
setFormError('common.error.invalidAmount');
return;
}

setDidConfirm(true);
onConfirm(selectedParticipants);
}
},
[selectedParticipants, onSendMoney, onConfirm, props.iouType],
[selectedParticipants, onSendMoney, onConfirm, props.iouType, props.isDistanceRequest, isDistanceRequestWithoutRoute, props.iouAmount],
);

const footerContent = useMemo(() => {
Expand All @@ -398,7 +415,7 @@ function MoneyRequestConfirmationList(props) {
const shouldShowSettlementButton = props.iouType === CONST.IOU.MONEY_REQUEST_TYPE.SEND;
const shouldDisableButton = selectedParticipants.length === 0;

return shouldShowSettlementButton ? (
const button = shouldShowSettlementButton ? (
<SettlementButton
isDisabled={shouldDisableButton}
onPress={confirm}
Expand All @@ -422,7 +439,20 @@ function MoneyRequestConfirmationList(props) {
buttonSize={CONST.DROPDOWN_BUTTON_SIZE.LARGE}
/>
);
}, [confirm, props.bankAccountRoute, props.iouCurrencyCode, props.iouType, props.isReadOnly, props.policyID, selectedParticipants, splitOrRequestOptions]);

return (
<>
{!_.isEmpty(formError) && (
<FormHelpMessage
style={[styles.ph1, styles.mb2]}
isError
message={translate(formError)}
/>
)}
{button}
</>
);
}, [confirm, props.bankAccountRoute, props.iouCurrencyCode, props.iouType, props.isReadOnly, props.policyID, selectedParticipants, splitOrRequestOptions, translate, formError]);

return (
<OptionsSelector
Expand Down
Loading