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 2 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 getPreviousTrackReport(reportID?: string) {
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 removeScreenByKey(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,
getPreviousTrackReport,
removeScreenByKey,
};

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

function navigateBackOnDeleteTransaction(backRoute: Route | undefined, isFromRHP?: boolean) {
function navigateBackOnDeleteTransaction(backRoute: Route | undefined, isFromRHP?: boolean, reportID?: string) {
if (!backRoute) {
return;
}
Expand All @@ -4322,7 +4322,13 @@ function navigateBackOnDeleteTransaction(backRoute: Route | undefined, isFromRHP
return;
}
if (isFromRHP) {
Navigation.dismissModal();
if (reportID) {
const trackReport = Navigation.getPreviousTrackReport(reportID);
if (trackReport?.key) {
Navigation.removeScreenByKey(trackReport.key);
}
}
Navigation.isNavigationReady().then(() => Navigation.dismissModal());
}
Navigation.isNavigationReady().then(() => {
Navigation.goBack(backRoute);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/ReportDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ function ReportDetailsPage({policies, report, route, reportMetadata}: ReportDeta
Navigation.dismissModal();
} else {
Report.setDeleteTransactionNavigateBackUrl(urlToNavigateBack);
ReportUtils.navigateBackOnDeleteTransaction(urlToNavigateBack as Route, true);
ReportUtils.navigateBackOnDeleteTransaction(urlToNavigateBack as Route, true, report.reportID);
}
}, [caseID, iouTransactionID, moneyRequestReport?.reportID, report, requestParentReportAction, isSingleTransactionView, isTransactionDeleted]);

Expand Down
3 changes: 2 additions & 1 deletion src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,8 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
!isSingleExpenseReport &&
!isSingleInvoiceReport &&
!ReportActionsUtils.isActionOfType(mostRecentReportAction, CONST.REPORT.ACTIONS.TYPE.CREATED) &&
!ReportActionsUtils.isDeletedAction(mostRecentReportAction);
!ReportActionsUtils.isDeletedAction(mostRecentReportAction) &&
(!deleteTransactionNavigateBackUrl || !ReportActionsUtils.isActionOfType(mostRecentReportAction, CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_TRACK_EXPENSE_WHISPER));

const lastRoute = usePrevious(route);
const lastReportActionIDFromRoute = usePrevious(reportActionIDFromRoute);
Expand Down
11 changes: 11 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,19 @@ jest.mock('@src/libs/Navigation/Navigation', () => ({
dismissModal: jest.fn(),
dismissModalWithReport: jest.fn(),
goBack: 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/Navigation/isSearchTopmostCentralPane', () => jest.fn());

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