Skip to content

Commit

Permalink
Merge pull request Expensify#35910 from tienifr/fix/34999
Browse files Browse the repository at this point in the history
fix: Duplicate waypoint error message is not dimissed after closing distance editor
  • Loading branch information
tgolen authored Feb 21, 2024
2 parents a6c1847 + ba736c8 commit 2932fd6
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/components/DistanceRequest/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Report, Transaction} from '@src/types/onyx';
import type {WaypointCollection} from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import DistanceRequestFooter from './DistanceRequestFooter';
import DistanceRequestRenderItem from './DistanceRequestRenderItem';

Expand Down Expand Up @@ -176,7 +177,7 @@ function DistanceRequest({transactionID = '', report, transaction, route, isEdit
);
};

const getError = () => {
const getError = useCallback(() => {
// Get route error if available else show the invalid number of waypoints error.
if (hasRouteError) {
return ErrorUtils.getLatestErrorField((transaction ?? {}) as Transaction, 'route');
Expand All @@ -186,8 +187,12 @@ function DistanceRequest({transactionID = '', report, transaction, route, isEdit
// eslint-disable-next-line @typescript-eslint/naming-convention
return {0: 'iou.error.atLeastTwoDifferentWaypoints'};
}
return {};
};

if (Object.keys(validatedWaypoints).length < Object.keys(waypoints).length) {
// eslint-disable-next-line @typescript-eslint/naming-convention
return {0: translate('iou.error.duplicateWaypointsErrorMessage')};
}
}, [translate, transaction, hasRouteError, validatedWaypoints, waypoints]);

const updateWaypoints = useCallback(
({data}: DraggableListData<string>) => {
Expand All @@ -211,7 +216,7 @@ function DistanceRequest({transactionID = '', report, transaction, route, isEdit

const submitWaypoints = useCallback(() => {
// If there is any error or loading state, don't let user go to next page.
if (Object.keys(validatedWaypoints).length < 2 || hasRouteError || isLoadingRoute || (isLoading && !isOffline)) {
if (!isEmptyObject(getError()) || isLoadingRoute || (isLoading && !isOffline)) {
setHasError(true);
return;
}
Expand All @@ -221,7 +226,7 @@ function DistanceRequest({transactionID = '', report, transaction, route, isEdit
}

onSubmit(waypoints);
}, [onSubmit, setHasError, hasRouteError, isLoadingRoute, isLoading, validatedWaypoints, waypoints, isEditingNewRequest, isEditingRequest, isOffline]);
}, [onSubmit, setHasError, getError, isLoadingRoute, isLoading, waypoints, isEditingNewRequest, isEditingRequest, isOffline]);

const content = (
<>
Expand Down Expand Up @@ -254,10 +259,10 @@ function DistanceRequest({transactionID = '', report, transaction, route, isEdit
</View>
<View style={[styles.w100, styles.pt2]}>
{/* Show error message if there is route error or there are less than 2 routes and user has tried submitting, */}
{((hasError && Object.keys(validatedWaypoints).length < 2) || hasRouteError) && (
{((hasError && !isEmptyObject(getError())) || hasRouteError) && (
<DotIndicatorMessage
style={[styles.mh4, styles.mv3]}
messages={getError()}
messages={getError() ?? {}}
type="error"
/>
)}
Expand Down

0 comments on commit 2932fd6

Please sign in to comment.