Skip to content

Commit 9aec7e9

Browse files
authored
Merge pull request #89192 from software-mansion-labs/@OlGierd03/Spend-fix-Change-Approver-button-navigation
Fix issue #88942: Fix navigation after pressing Change Approver button
2 parents 7014cd9 + 0681a26 commit 9aec7e9

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

src/ROUTES.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,15 @@ const ROUTES = {
491491
return `search/move-transactions/search/${encodeURIComponent(backTo)}` as const;
492492
},
493493
},
494-
CHANGE_APPROVER_SEARCH_RHP: 'search/change-approver',
494+
CHANGE_APPROVER_SEARCH_RHP: {
495+
route: 'search/change-approver/search/:backTo?',
496+
getRoute: (backTo?: string) => {
497+
if (!backTo) {
498+
return 'search/change-approver/search' as const;
499+
}
500+
return `search/change-approver/search/${encodeURIComponent(backTo)}` as const;
501+
},
502+
},
495503
CHANGE_APPROVER_ADD_APPROVER_SEARCH_RHP: 'search/change-approver/add',
496504

497505
// This is a utility route used to go to the user's concierge chat, or the sign-in page if the user's not authenticated

src/hooks/useSearchBulkActions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
11891189
text: translate('iou.changeApprover.title'),
11901190
value: CONST.SEARCH.BULK_ACTION_TYPES.CHANGE_APPROVER,
11911191
shouldCloseModalOnSelect: true,
1192-
onSelected: () => Navigation.navigate(ROUTES.CHANGE_APPROVER_SEARCH_RHP),
1192+
onSelected: () => navigateToSearchRHP(ROUTES.CHANGE_APPROVER_SEARCH_RHP),
11931193
});
11941194
}
11951195

src/libs/Navigation/linkingConfig/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1949,7 +1949,10 @@ const config: LinkingOptions<RootNavigatorParamList>['config'] = {
19491949
path: ROUTES.MOVE_TRANSACTIONS_SEARCH_RHP.route,
19501950
exact: true,
19511951
},
1952-
[SCREENS.SEARCH.CHANGE_APPROVER.ROOT]: ROUTES.CHANGE_APPROVER_SEARCH_RHP,
1952+
[SCREENS.SEARCH.CHANGE_APPROVER.ROOT]: {
1953+
path: ROUTES.CHANGE_APPROVER_SEARCH_RHP.route,
1954+
exact: true,
1955+
},
19531956
[SCREENS.SEARCH.CHANGE_APPROVER.ADD_APPROVER]: ROUTES.CHANGE_APPROVER_ADD_APPROVER_SEARCH_RHP,
19541957
},
19551958
},

src/pages/Search/SearchChangeApproverPage.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,20 @@ function SearchChangeApproverPage() {
9696
const [onyxReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: getOnyxReports});
9797

9898
const hasAutoAppliedRef = useRef(false);
99+
// Set when navigating to WORKSPACE_UPGRADE; prevents the auto-close in useLayoutEffect from
100+
// dismissing the RHP if selectedReports goes transiently empty before this page unmounts
101+
// TODO: drop this ref once the CHANGE_APPROVER_SEARCH_RHP `backTo` is removed so navigation matches the regular
102+
// "Change approver" flow after upgrading workspace. See https://github.com/Expensify/App/pull/89192.
103+
const hasInitiatedUpgradeRef = useRef(false);
99104
const prevSelectedReportsLength = useRef(0);
105+
106+
useEffect(() => {
107+
if (selectedReports.length === 0) {
108+
return;
109+
}
110+
hasInitiatedUpgradeRef.current = false;
111+
}, [selectedReports.length]);
112+
100113
useEffect(() => {
101114
if (!hasLoadedApp || !selectedReports.length || prevSelectedReportsLength.current === selectedReports.length) {
102115
return;
@@ -123,12 +136,19 @@ function SearchChangeApproverPage() {
123136
const policiesToUpgrade = selectedPolicies.filter((policy) => !isControlPolicy(policy));
124137
if (policiesToUpgrade.length > 1) {
125138
// Bulk upgrade is not supported, so show a general page to guide the user to upgrade manually
126-
Navigation.navigate(ROUTES.WORKSPACE_UPGRADE.getRoute(undefined, undefined, ROUTES.CHANGE_APPROVER_SEARCH_RHP));
139+
hasInitiatedUpgradeRef.current = true;
140+
Navigation.navigate(ROUTES.WORKSPACE_UPGRADE.getRoute(undefined, undefined, ROUTES.CHANGE_APPROVER_ADD_APPROVER_SEARCH_RHP), {forceReplace: true});
127141
return;
128142
}
129143
if (policiesToUpgrade.length === 1) {
144+
hasInitiatedUpgradeRef.current = true;
130145
Navigation.navigate(
131-
ROUTES.WORKSPACE_UPGRADE.getRoute(policiesToUpgrade.at(0)?.id, CONST.UPGRADE_FEATURE_INTRO_MAPPING.multiApprovalLevels.alias, ROUTES.CHANGE_APPROVER_SEARCH_RHP),
146+
ROUTES.WORKSPACE_UPGRADE.getRoute(
147+
policiesToUpgrade.at(0)?.id,
148+
CONST.UPGRADE_FEATURE_INTRO_MAPPING.multiApprovalLevels.alias,
149+
ROUTES.CHANGE_APPROVER_ADD_APPROVER_SEARCH_RHP,
150+
),
151+
{forceReplace: true},
132152
);
133153
return;
134154
}
@@ -208,6 +228,12 @@ function SearchChangeApproverPage() {
208228
return;
209229
}
210230

231+
// Skip during an upgrade round-trip: selectedReports may be transiently empty while
232+
// navigating to WORKSPACE_UPGRADE, and closing the RHP would kill the Add Approver flow.
233+
if (hasInitiatedUpgradeRef.current) {
234+
return;
235+
}
236+
211237
Navigation.setNavigationActionToMicrotaskQueue(() => {
212238
Navigation.closeRHPFlow();
213239
});

0 commit comments

Comments
 (0)