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: navigate to parent report on delete track expense #55015

Merged
merged 7 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import NAVIGATORS from '@src/NAVIGATORS';
import ONYXKEYS from '@src/ONYXKEYS';
import type {HybridAppRoute, Route} from '@src/ROUTES';
import ROUTES, {HYBRID_APP_ROUTES} from '@src/ROUTES';
import {PROTECTED_SCREENS} from '@src/SCREENS';
import SCREENS, {PROTECTED_SCREENS} from '@src/SCREENS';
import type {Screen} from '@src/SCREENS';
import type {Report} from '@src/types/onyx';
import originalCloseRHPFlow from './closeRHPFlow';
Expand Down Expand Up @@ -428,6 +428,13 @@ function getTopMostCentralPaneRouteFromRootState() {
return getTopmostCentralPaneRoute(navigationRef.getRootState() as State<RootStackParamList>);
}

function getReportRouteByID(reportID?: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sorry, one more dumb question. Why isn't the report route just /r/<reportID> ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to return the whole Route object here because in the future there might be use of it, instead of just return the route name/key.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok gotcha. Sounds good.

if (!reportID) {
return null;
}
return navigationRef.getRootState().routes.find((r) => r.name === SCREENS.REPORT && !!r.params && 'reportID' in r.params && r.params.reportID === reportID);
}

function removeScreenFromNavigationState(screen: Screen) {
isNavigationReady().then(() => {
navigationRef.dispatch((state) => {
Expand All @@ -442,6 +449,19 @@ function removeScreenFromNavigationState(screen: Screen) {
});
}

function removeScreenFromNavigationStateByKey(key: string) {
const state = navigationRef.getRootState();
const routes = state.routes.filter((item) => item.key !== key);

navigationRef.current?.dispatch(() => {
return CommonActions.reset({
...state,
routes,
index: routes.length < state.routes.length ? state.index - 1 : state.index,
});
});
}

export default {
setShouldPopAllStateOnUP,
navigate,
Expand All @@ -467,6 +487,8 @@ export default {
setNavigationActionToMicrotaskQueue,
getTopMostCentralPaneRouteFromRootState,
removeScreenFromNavigationState,
getReportRouteByID,
removeScreenFromNavigationStateByKey,
};

export {navigationRef};
11 changes: 9 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4473,7 +4473,7 @@ function goBackToDetailsPage(report: OnyxEntry<Report>, backTo?: string) {
}
}

function navigateBackOnDeleteTransaction(backRoute: Route | undefined, isFromRHP?: boolean) {
function navigateBackOnDeleteTransaction(backRoute: Route | undefined, isFromRHP?: boolean, reportIDToRemove?: string) {
if (!backRoute) {
return;
}
Expand All @@ -4483,7 +4483,14 @@ function navigateBackOnDeleteTransaction(backRoute: Route | undefined, isFromRHP
return;
}
if (isFromRHP) {
Navigation.dismissModal();
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToRemove}`];
if (report && isTrackExpenseReport(report)) {
const trackReportRoute = Navigation.getReportRouteByID(reportIDToRemove);
if (trackReportRoute?.key) {
Navigation.removeScreenFromNavigationStateByKey(trackReportRoute.key);
}
}
Navigation.isNavigationReady().then(() => Navigation.dismissModal());
}
Navigation.isNavigationReady().then(() => {
Navigation.goBack(backRoute);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/ReportDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -969,9 +969,9 @@ function ReportDetailsPage({policies, report, route, reportMetadata}: ReportDeta
Navigation.dismissModal();
} else {
setDeleteTransactionNavigateBackUrl(urlToNavigateBack);
navigateBackOnDeleteTransaction(urlToNavigateBack as Route, true);
navigateBackOnDeleteTransaction(urlToNavigateBack as Route, true, report.reportID);
}
}, [iouTransactionID, requestParentReportAction, isSingleTransactionView, isTransactionDeleted, moneyRequestReport?.reportID]);
}, [iouTransactionID, requestParentReportAction, isSingleTransactionView, isTransactionDeleted, moneyRequestReport?.reportID, report.reportID]);

const mentionReportContextValue = useMemo(() => ({currentReportID: report.reportID, exactlyMatch: true}), [report.reportID]);

Expand Down
12 changes: 12 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,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()),
getPreviousTrackReport: 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 All @@ -88,6 +99,7 @@ jest.mock('@src/libs/actions/Report', () => {
notifyNewAction: jest.fn(),
};
});

jest.mock('@src/libs/Navigation/isSearchTopmostCentralPane', () => jest.fn());

const CARLOS_EMAIL = 'cmartins@expensifail.com';
Expand Down