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: back button returns to expense report after submitting track expense #53065

Merged
merged 16 commits into from
Feb 21, 2025
Merged
39 changes: 36 additions & 3 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import setNavigationActionToMicrotaskQueue from './helpers/setNavigationActionTo
import switchPolicyID from './helpers/switchPolicyID';
import {linkingConfig} from './linkingConfig';
import navigationRef from './navigationRef';
import type {NavigationPartialRoute, NavigationStateRoute, RootNavigatorParamList, State} from './types';
import type {NavigationPartialRoute, NavigationRoute, NavigationStateRoute, RootNavigatorParamList, State} from './types';

let allReports: OnyxCollection<Report>;
Onyx.connect({
Expand Down Expand Up @@ -481,12 +481,12 @@ function navigateToReportWithPolicyCheck({report, reportID, reportActionID, refe
const shouldOpenAllWorkspace = isEmptyObject(targetReport) ? true : !doesReportBelongToWorkspace(targetReport, policyMemberAccountIDs, policyID);

if ((shouldOpenAllWorkspace && !policyID) || !shouldOpenAllWorkspace) {
linkTo(ref.current, ROUTES.REPORT_WITH_ID.getRoute(targetReport?.reportID ?? '-1', reportActionID, referrer));
linkTo(ref.current, ROUTES.REPORT_WITH_ID.getRoute(targetReport?.reportID, reportActionID, referrer));
return;
}

const params: Record<string, string> = {
reportID: targetReport?.reportID ?? '-1',
reportID: targetReport?.reportID ?? String(CONST.DEFAULT_NUMBER_ID),
};

if (reportActionID) {
Expand All @@ -506,6 +506,24 @@ function navigateToReportWithPolicyCheck({report, reportID, reportActionID, refe
);
}

function getReportRouteByID(reportID?: string, routes: NavigationRoute[] = navigationRef.getRootState().routes): NavigationRoute | null {
if (!reportID || !routes?.length) {
return null;
}
for (const route of routes) {
if (route.name === SCREENS.REPORT && !!route.params && 'reportID' in route.params && route.params.reportID === reportID) {
return route;
}
if (route.state?.routes) {
const partialRoute = getReportRouteByID(reportID, route.state.routes);
if (partialRoute) {
return partialRoute;
}
}
}
return null;
}

/**
* Closes the modal navigator (RHP, LHP, onboarding).
*/
Expand Down Expand Up @@ -555,6 +573,19 @@ function removeScreenFromNavigationState(screen: string) {
});
}

function removeScreenByKey(key: string) {
isNavigationReady().then(() => {
navigationRef.current?.dispatch((state) => {
const routes = state.routes?.filter((item) => item.key !== key);
return CommonActions.reset({
...state,
routes,
index: routes.length < state.routes.length ? state.index - 1 : state.index,
});
});
});
}

export default {
setShouldPopAllStateOnUP,
navigate,
Expand All @@ -580,6 +611,8 @@ export default {
navigateToReportWithPolicyCheck,
popToTop,
removeScreenFromNavigationState,
removeScreenByKey,
getReportRouteByID,
switchPolicyID,
replaceWithSplitNavigator,
};
Expand Down
2 changes: 2 additions & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type NavigationStateRoute = NavigationState['routes'][number];
type NavigationPartialRoute<TRouteName extends string = string> = PartialRoute<Route<TRouteName>>;
type StateOrRoute = NavigationState | NavigationStateRoute | NavigationPartialRoute;
type State<TParamList extends ParamListBase = ParamListBase> = NavigationState<TParamList> | PartialState<NavigationState<TParamList>>;
type NavigationRoute = NavigationStateRoute | NavigationPartialRoute;

type SplitNavigatorSidebarScreen = keyof typeof SIDEBAR_TO_SPLIT;

Expand Down Expand Up @@ -1912,6 +1913,7 @@ export type {
NavigationRef,
NavigationRoot,
NavigationStateRoute,
NavigationRoute,
NewChatNavigatorParamList,
NewTaskNavigatorParamList,
OnboardingFlowName,
Expand Down
6 changes: 6 additions & 0 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4582,6 +4582,12 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {

InteractionManager.runAfterInteractions(() => removeDraftTransaction(CONST.IOU.OPTIMISTIC_TRANSACTION_ID));
Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : activeReportID);

const trackReport = Navigation.getReportRouteByID(linkedTrackedExpenseReportAction?.childReportID);
if (trackReport?.key) {
Navigation.removeScreenByKey(trackReport.key);
}

if (activeReportID) {
notifyNewAction(activeReportID, payeeAccountID);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,19 @@ jest.mock('@src/libs/Navigation/Navigation', () => ({
goBack: jest.fn(),
getTopmostReportId: jest.fn(() => topMostReportID),
setNavigationActionToMicrotaskQueue: jest.fn(),
removeScreenByKey: jest.fn(),
isNavigationReady: jest.fn(() => Promise.resolve()),
getReportRouteByID: jest.fn(),
}));

jest.mock('@src/libs/Navigation/navigationRef', () => ({
getRootState: () => ({
routes: [],
}),
}));

jest.mock('@react-navigation/native');

jest.mock('@src/libs/actions/Report', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const originalModule = jest.requireActual('@src/libs/actions/Report');
Expand Down
Loading