Skip to content

Commit

Permalink
fix: validate merchant field on submit
Browse files Browse the repository at this point in the history
  • Loading branch information
koko57 committed Dec 28, 2023
1 parent add1bde commit 9860e01
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/components/MoneyTemporaryForRefactorRequestConfirmationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ function MoneyTemporaryForRefactorRequestConfirmationList({
const [didConfirm, setDidConfirm] = useState(false);
const [didConfirmSplit, setDidConfirmSplit] = useState(false);

const [merchantError, setMerchantError] = useState(false);

const shouldDisplayFieldError = useMemo(() => {
if (!isEditingSplitBill) {
return false;
Expand All @@ -294,7 +296,19 @@ function MoneyTemporaryForRefactorRequestConfirmationList({
}, [isEditingSplitBill, hasSmartScanFailed, transaction, didConfirmSplit]);

const isMerchantEmpty = !iouMerchant || iouMerchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT;
const shouldDisplayMerchantError = isPolicyExpenseChat && !isScanRequest && isMerchantEmpty;
const isMerchantRequired = isPolicyExpenseChat && !isScanRequest;

useEffect(() => {
if ((!isMerchantRequired && isMerchantEmpty) || !merchantError) {
return;
}
if (!isMerchantEmpty && merchantError) {
setMerchantError(false);
if (formError === 'iou.error.invalidMerchant') {
setFormError('');
}
}
}, [formError, isMerchantEmpty, merchantError, isMerchantRequired]);

useEffect(() => {
if (shouldDisplayFieldError && hasSmartScanFailed) {
Expand All @@ -305,13 +319,13 @@ function MoneyTemporaryForRefactorRequestConfirmationList({
setFormError('iou.error.genericSmartscanFailureMessage');
return;
}
if (shouldDisplayMerchantError) {
if (merchantError) {
setFormError('iou.error.invalidMerchant');
return;
}
// reset the form error whenever the screen gains or loses focus
setFormError('');
}, [isFocused, transaction, shouldDisplayFieldError, hasSmartScanFailed, didConfirmSplit, shouldDisplayMerchantError]);
}, [isFocused, transaction, shouldDisplayFieldError, hasSmartScanFailed, didConfirmSplit, isMerchantRequired, merchantError]);

useEffect(() => {
if (!shouldCalculateDistanceAmount) {
Expand Down Expand Up @@ -481,7 +495,8 @@ function MoneyTemporaryForRefactorRequestConfirmationList({
if (_.isEmpty(selectedParticipants)) {
return;
}
if (shouldDisplayMerchantError) {
if ((isMerchantRequired && isMerchantEmpty) || (shouldDisplayFieldError && TransactionUtils.isMerchantMissing(transaction))) {
setMerchantError(true);
return;
}

Expand Down Expand Up @@ -514,15 +529,17 @@ function MoneyTemporaryForRefactorRequestConfirmationList({
},
[
selectedParticipants,
shouldDisplayMerchantError,
isMerchantRequired,
isMerchantEmpty,
shouldDisplayFieldError,
transaction,
iouType,
onSendMoney,
iouCurrencyCode,
isDistanceRequest,
isDistanceRequestWithoutRoute,
iouAmount,
isEditingSplitBill,
transaction,
onConfirm,
],
);
Expand Down Expand Up @@ -655,7 +672,7 @@ function MoneyTemporaryForRefactorRequestConfirmationList({
interactive={!isReadOnly}
numberOfLinesTitle={2}
/>
{shouldDisplayMerchantError && (
{isMerchantRequired && (
<MenuItemWithTopDescription
shouldShowRightIcon={!isReadOnly}
title={isMerchantEmpty ? '' : iouMerchant}
Expand All @@ -671,10 +688,8 @@ function MoneyTemporaryForRefactorRequestConfirmationList({
}}
disabled={didConfirm}
interactive={!isReadOnly}
brickRoadIndicator={
shouldDisplayMerchantError || (shouldDisplayFieldError && TransactionUtils.isMerchantMissing(transaction)) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''
}
error={shouldDisplayMerchantError || (shouldDisplayFieldError && TransactionUtils.isMerchantMissing(transaction)) ? translate('common.error.fieldRequired') : ''}
brickRoadIndicator={merchantError ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''}
error={merchantError ? translate('common.error.fieldRequired') : ''}
/>
)}
{!shouldShowAllFields && (
Expand Down Expand Up @@ -728,7 +743,7 @@ function MoneyTemporaryForRefactorRequestConfirmationList({
interactive={!isReadOnly}
/>
)}
{!shouldDisplayMerchantError && shouldShowMerchant && (
{!isMerchantRequired && shouldShowMerchant && (
<MenuItemWithTopDescription
shouldShowRightIcon={!isReadOnly}
title={isMerchantEmpty ? '' : iouMerchant}
Expand All @@ -744,10 +759,8 @@ function MoneyTemporaryForRefactorRequestConfirmationList({
}}
disabled={didConfirm}
interactive={!isReadOnly}
brickRoadIndicator={
shouldDisplayMerchantError || (shouldDisplayFieldError && TransactionUtils.isMerchantMissing(transaction)) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''
}
error={shouldDisplayMerchantError || (shouldDisplayFieldError && TransactionUtils.isMerchantMissing(transaction)) ? translate('common.error.fieldRequired') : ''}
brickRoadIndicator={merchantError ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''}
error={merchantError ? translate('common.error.fieldRequired') : ''}
/>
)}
{shouldShowCategories && (
Expand Down

0 comments on commit 9860e01

Please sign in to comment.