Skip to content

Commit

Permalink
merge squash from dejan-fix-remain-addr-msg-tabchange
Browse files Browse the repository at this point in the history
Signed-off-by: Dejan <[email protected]>
  • Loading branch information
funny2code committed Feb 27, 2024
1 parent 297bcec commit 5c45461
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/pages/iou/request/step/IOURequestStepDistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ function IOURequestStepDistance({
const {translate} = useLocalize();

const [optimisticWaypoints, setOptimisticWaypoints] = useState(null);
const [hasError, setHasError] = useState(false);
const waypoints = useMemo(() => optimisticWaypoints || lodashGet(transaction, 'comment.waypoints', {waypoint0: {}, waypoint1: {}}), [optimisticWaypoints, transaction]);
const waypointsList = _.keys(waypoints);
const previousWaypoints = usePrevious(waypoints);
Expand All @@ -73,6 +72,10 @@ function IOURequestStepDistance({
const haveValidatedWaypointsChanged = !_.isEqual(previousValidatedWaypoints, validatedWaypoints);
const isRouteAbsentWithoutErrors = !hasRoute && !hasRouteError;
const shouldFetchRoute = (isRouteAbsentWithoutErrors || haveValidatedWaypointsChanged) && !isLoadingRoute && _.size(validatedWaypoints) > 1;
const [shouldShowAtLeastTwoDifferentWaypointsError, setShouldShowAtLeastTwoDifferentWaypointsError] = useState(false);
const nonEmptyWaypointsCount = useMemo(() => _.filter(_.keys(waypoints), (key) => !_.isEmpty(waypoints[key])).length, [waypoints]);
const duplicateWaypointsError = useMemo(() => nonEmptyWaypointsCount >= 2 && _.size(validatedWaypoints) !== nonEmptyWaypointsCount, [nonEmptyWaypointsCount, validatedWaypoints]);
const atLeastTwoDifferentWaypointsError = useMemo(() => _.size(validatedWaypoints) < 2, [validatedWaypoints]);

useEffect(() => {
MapboxToken.init();
Expand All @@ -83,7 +86,6 @@ function IOURequestStepDistance({
if (isOffline || !shouldFetchRoute) {
return;
}

Transaction.getRouteForDraft(transactionID, validatedWaypoints);
}, [shouldFetchRoute, transactionID, validatedWaypoints, isOffline]);

Expand All @@ -94,6 +96,14 @@ function IOURequestStepDistance({
scrollViewRef.current.scrollToEnd({animated: true});
}, [numberOfPreviousWaypoints, numberOfWaypoints]);


useEffect(() => {
if (nonEmptyWaypointsCount >= 2 && (duplicateWaypointsError || atLeastTwoDifferentWaypointsError || hasRouteError || isLoadingRoute || isLoading)) {
return;
}
setShouldShowAtLeastTwoDifferentWaypointsError(false);
}, [atLeastTwoDifferentWaypointsError, duplicateWaypointsError, hasRouteError, isLoading, isLoadingRoute, nonEmptyWaypointsCount, transaction]);

const navigateBack = () => {
Navigation.goBack(backTo);
};
Expand Down Expand Up @@ -133,12 +143,10 @@ function IOURequestStepDistance({
if (hasRouteError) {
return ErrorUtils.getLatestErrorField(transaction, 'route');
}

if (_.keys(waypoints).length > 2 && _.size(validatedWaypoints) !== _.keys(waypoints).length) {
if (duplicateWaypointsError) {
return {0: translate('iou.error.duplicateWaypointsErrorMessage')};
}

if (_.size(validatedWaypoints) < 2) {
if (atLeastTwoDifferentWaypointsError) {
return {0: 'iou.error.atLeastTwoDifferentWaypoints'};
}
};
Expand All @@ -165,12 +173,12 @@ function IOURequestStepDistance({

const submitWaypoints = useCallback(() => {
// If there is any error or loading state, don't let user go to next page.
if (_.size(validatedWaypoints) < 2 || (_.keys(waypoints).length > 2 && _.size(validatedWaypoints) !== _.keys(waypoints).length) || hasRouteError || isLoadingRoute || isLoading) {
setHasError(true);
if (duplicateWaypointsError || atLeastTwoDifferentWaypointsError || hasRouteError || isLoadingRoute || isLoading) {
setShouldShowAtLeastTwoDifferentWaypointsError(true);
return;
}
navigateToNextStep();
}, [setHasError, waypoints, hasRouteError, isLoadingRoute, isLoading, validatedWaypoints, navigateToNextStep]);
}, [atLeastTwoDifferentWaypointsError, duplicateWaypointsError, hasRouteError, isLoadingRoute, isLoading, navigateToNextStep]);

return (
<StepScreenWrapper
Expand Down Expand Up @@ -211,7 +219,7 @@ function IOURequestStepDistance({
</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 && _.size(validatedWaypoints) < 2) || (_.keys(waypoints).length > 2 && _.size(validatedWaypoints) !== _.keys(waypoints).length) || hasRouteError) && (
{((shouldShowAtLeastTwoDifferentWaypointsError && atLeastTwoDifferentWaypointsError) || duplicateWaypointsError || hasRouteError) && (
<DotIndicatorMessage
style={[styles.mh4, styles.mv3]}
messages={getError()}
Expand Down

0 comments on commit 5c45461

Please sign in to comment.