Skip to content

Commit

Permalink
Merge pull request #57913 from bernhardoj/fix/57457-expense-categorie…
Browse files Browse the repository at this point in the history
…s-opened-several-times

Fix multiple navigation to expense field page
  • Loading branch information
thienlnam authored Mar 7, 2025
2 parents 19ed526 + fdb45e7 commit 770d947
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 63 deletions.
78 changes: 15 additions & 63 deletions src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ReceiptEmptyState from '@components/ReceiptEmptyState';
import Switch from '@components/Switch';
import Text from '@components/Text';
import ViolationMessages from '@components/ViolationMessages';
import useActiveRoute from '@hooks/useActiveRoute';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
Expand Down Expand Up @@ -106,6 +107,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
const session = useSession();
const {isOffline} = useNetwork();
const {translate, toLocaleDigit} = useLocalize();
const {getReportRHPActiveRoute} = useActiveRoute();
const parentReportID = report?.parentReportID;
const policyID = report?.policyID;
const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID ?? CONST.DEFAULT_NUMBER_ID}`);
Expand Down Expand Up @@ -370,7 +372,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_DISTANCE.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, Navigation.getReportRHPActiveRoute()),
ROUTES.MONEY_REQUEST_STEP_DISTANCE.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
/>
Expand All @@ -387,13 +389,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_DISTANCE_RATE.getRoute(
CONST.IOU.ACTION.EDIT,
iouType,
transaction.transactionID,
report.reportID,
Navigation.getReportRHPActiveRoute(),
),
ROUTES.MONEY_REQUEST_STEP_DISTANCE_RATE.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
brickRoadIndicator={getErrorForField('customUnitRateID') ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
Expand Down Expand Up @@ -471,14 +467,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_TAG.getRoute(
CONST.IOU.ACTION.EDIT,
iouType,
orderWeight,
transaction.transactionID,
report.reportID,
Navigation.getReportRHPActiveRoute(),
),
ROUTES.MONEY_REQUEST_STEP_TAG.getRoute(CONST.IOU.ACTION.EDIT, iouType, orderWeight, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
brickRoadIndicator={tagError ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
Expand Down Expand Up @@ -564,7 +553,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_SCAN.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, Navigation.getReportRHPActiveRoute()),
ROUTES.MONEY_REQUEST_STEP_SCAN.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
/>
Expand All @@ -586,14 +575,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_AMOUNT.getRoute(
CONST.IOU.ACTION.EDIT,
iouType,
transaction.transactionID,
report.reportID,
'',
Navigation.getReportRHPActiveRoute(),
),
ROUTES.MONEY_REQUEST_STEP_AMOUNT.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, '', getReportRHPActiveRoute()),
);
}}
brickRoadIndicator={getErrorForField('amount') ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
Expand All @@ -613,13 +595,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_DESCRIPTION.getRoute(
CONST.IOU.ACTION.EDIT,
iouType,
transaction.transactionID,
report.reportID,
Navigation.getReportRHPActiveRoute(),
),
ROUTES.MONEY_REQUEST_STEP_DESCRIPTION.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
wrapperStyle={[styles.pv2, styles.taskDescriptionMenuItem]}
Expand All @@ -643,13 +619,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_MERCHANT.getRoute(
CONST.IOU.ACTION.EDIT,
iouType,
transaction.transactionID,
report.reportID,
Navigation.getReportRHPActiveRoute(),
),
ROUTES.MONEY_REQUEST_STEP_MERCHANT.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
wrapperStyle={[styles.taskDescriptionMenuItem]}
Expand All @@ -671,7 +641,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_DATE.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, Navigation.getReportRHPActiveRoute()),
ROUTES.MONEY_REQUEST_STEP_DATE.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
brickRoadIndicator={getErrorForField('date') ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
Expand All @@ -691,13 +661,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CATEGORY.getRoute(
CONST.IOU.ACTION.EDIT,
iouType,
transaction.transactionID,
report.reportID,
Navigation.getReportRHPActiveRoute(),
),
ROUTES.MONEY_REQUEST_STEP_CATEGORY.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
brickRoadIndicator={getErrorForField('category') ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
Expand Down Expand Up @@ -729,13 +693,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_TAX_RATE.getRoute(
CONST.IOU.ACTION.EDIT,
iouType,
transaction.transactionID,
report.reportID,
Navigation.getReportRHPActiveRoute(),
),
ROUTES.MONEY_REQUEST_STEP_TAX_RATE.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
brickRoadIndicator={getErrorForField('tax') ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
Expand All @@ -756,13 +714,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
return;
}
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_TAX_AMOUNT.getRoute(
CONST.IOU.ACTION.EDIT,
iouType,
transaction.transactionID,
report.reportID,
Navigation.getReportRHPActiveRoute(),
),
ROUTES.MONEY_REQUEST_STEP_TAX_AMOUNT.getRoute(CONST.IOU.ACTION.EDIT, iouType, transaction.transactionID, report.reportID, getReportRHPActiveRoute()),
);
}}
/>
Expand All @@ -778,9 +730,9 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
}
const reservations = transaction?.receipt?.reservationList?.length ?? 0;
if (reservations > 1) {
Navigation.navigate(ROUTES.TRAVEL_TRIP_SUMMARY.getRoute(report.reportID, transaction.transactionID, Navigation.getReportRHPActiveRoute()));
Navigation.navigate(ROUTES.TRAVEL_TRIP_SUMMARY.getRoute(report.reportID, transaction.transactionID, getReportRHPActiveRoute()));
}
Navigation.navigate(ROUTES.TRAVEL_TRIP_DETAILS.getRoute(report.reportID, transaction.transactionID, 0, Navigation.getReportRHPActiveRoute()));
Navigation.navigate(ROUTES.TRAVEL_TRIP_DETAILS.getRoute(report.reportID, transaction.transactionID, 0, getReportRHPActiveRoute()));
}}
/>
)}
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/useActiveRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {useCallback, useRef} from 'react';
import Navigation from '@libs/Navigation/Navigation';

function useActiveRoute() {
const currentReportRHPActiveRoute = useRef('');

const getReportRHPActiveRoute = useCallback(() => {
if (!currentReportRHPActiveRoute.current) {
currentReportRHPActiveRoute.current = Navigation.getReportRHPActiveRoute();
}

return currentReportRHPActiveRoute.current;
}, []);

return {getReportRHPActiveRoute};
}

export default useActiveRoute;
25 changes: 25 additions & 0 deletions tests/unit/useActiveRouteTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {renderHook} from '@testing-library/react-native';
import useActiveRoute from '@hooks/useActiveRoute';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';

describe('useActiveRoute', () => {
it('should return the same active route', () => {
// Given an active route
const navigation = jest.spyOn(Navigation, 'getReportRHPActiveRoute');
const {result} = renderHook(() => useActiveRoute());
const expectedActiveRoute = ROUTES.SEARCH_REPORT.getRoute({reportID: '1'});
navigation.mockReturnValueOnce(expectedActiveRoute);

const actualActiveRoute = result.current.getReportRHPActiveRoute();
expect(actualActiveRoute).toBe(expectedActiveRoute);

// When getting the active route multiple times
navigation.mockReturnValueOnce(ROUTES.MONEY_REQUEST_STEP_DESCRIPTION.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.CREATE, '1', '1'));
const actualActiveRoute2 = result.current.getReportRHPActiveRoute();

// Then it should return the first active route value
expect(actualActiveRoute2).toBe(expectedActiveRoute);
});
});

0 comments on commit 770d947

Please sign in to comment.