Skip to content

Commit 6eab33e

Browse files
committed
Update SearchNavigator and fix handling policyID
1 parent c0bd8fe commit 6eab33e

File tree

6 files changed

+93
-64
lines changed

6 files changed

+93
-64
lines changed

src/libs/Navigation/AppNavigator/AuthScreens.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const loadWorkspaceJoinUser = () => require<ReactComponentModule>('@pages/worksp
9595
const loadReportSplitNavigator = () => require<ReactComponentModule>('./Navigators/ReportsSplitNavigator').default;
9696
const loadSettingsSplitNavigator = () => require<ReactComponentModule>('./Navigators/SettingsSplitNavigator').default;
9797
const loadWorkspaceSplitNavigator = () => require<ReactComponentModule>('./Navigators/WorkspaceSplitNavigator').default;
98-
const loadSearchNavigator = () => require<ReactComponentModule>('./Navigators/SearchNavigator').default;
98+
const loadSearchNavigator = () => require<ReactComponentModule>('./Navigators/SearchFullscreenNavigator').default;
9999

100100
function initializePusher() {
101101
return Pusher.init({
@@ -452,7 +452,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
452452

453453
return (
454454
<ComposeProviders components={[OptionsListContextProvider, ActiveWorkspaceContextProvider, ReportIDsContextProvider, SearchContextProvider]}>
455-
<RootStack.Navigator persistentScreens={[NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, SCREENS.SEARCH.ROOT]}>
455+
<RootStack.Navigator persistentScreens={[NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR]}>
456456
{/* This has to be the first navigator in auth screens. */}
457457
<RootStack.Screen
458458
name={NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}

src/libs/Navigation/AppNavigator/Navigators/SearchNavigator.tsx renamed to src/libs/Navigation/AppNavigator/Navigators/SearchFullscreenNavigator.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import type {SearchFullscreenNavigatorParamList} from '@libs/Navigation/types';
33
import * as SearchQueryUtils from '@libs/SearchQueryUtils';
44
import FreezeWrapper from '@navigation/AppNavigator/FreezeWrapper';
5+
import useRootNavigatorScreenOptions from '@navigation/AppNavigator/useRootNavigatorScreenOptions';
56
import createPlatformStackNavigator from '@navigation/PlatformStackNavigation/createPlatformStackNavigator';
67
import SCREENS from '@src/SCREENS';
78
import type ReactComponentModule from '@src/types/utils/ReactComponentModule';
@@ -11,11 +12,13 @@ const loadSearchMoneyReportPage = () => require<ReactComponentModule>('@pages/Se
1112

1213
const Stack = createPlatformStackNavigator<SearchFullscreenNavigatorParamList>();
1314

14-
function SearchNavigator() {
15+
function SearchFullscreenNavigator() {
16+
const rootNavigatorScreenOptions = useRootNavigatorScreenOptions();
17+
1518
return (
1619
<FreezeWrapper>
1720
<Stack.Navigator
18-
screenOptions={{headerShown: false}}
21+
screenOptions={rootNavigatorScreenOptions.fullScreen}
1922
defaultCentralScreen={SCREENS.SEARCH.ROOT}
2023
>
2124
<Stack.Screen
@@ -32,6 +35,6 @@ function SearchNavigator() {
3235
);
3336
}
3437

35-
SearchNavigator.displayName = 'SearchNavigator';
38+
SearchFullscreenNavigator.displayName = 'SearchFullscreenNavigator';
3639

37-
export default SearchNavigator;
40+
export default SearchFullscreenNavigator;

src/libs/Navigation/AppNavigator/createRootStackNavigator/GetStateForActionHandlers.ts

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,30 @@ function handleSwitchPolicyID(
8989
setActiveWorkspaceID: (workspaceID: string | undefined) => void,
9090
) {
9191
const lastRoute = state.routes.at(-1);
92-
if (lastRoute?.name === SCREENS.SEARCH.ROOT) {
93-
const currentParams = lastRoute.params as RootNavigatorParamList[typeof SCREENS.SEARCH.ROOT];
94-
const queryJSON = SearchQueryUtils.buildSearchQueryJSON(currentParams.q);
95-
if (!queryJSON) {
96-
return null;
97-
}
92+
if (lastRoute?.name === NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR) {
93+
const searchRoute = lastRoute.state?.routes?.findLast((route) => route.name === SCREENS.SEARCH.ROOT);
94+
if (searchRoute?.name === SCREENS.SEARCH.ROOT) {
95+
// Fixme something is wrong in this state
96+
const searchParams = searchRoute?.params;
97+
const queryJSON = SearchQueryUtils.buildSearchQueryJSON(searchParams.q);
98+
if (!queryJSON) {
99+
return null;
100+
}
101+
102+
if (action.payload.policyID) {
103+
queryJSON.policyID = action.payload.policyID;
104+
} else {
105+
delete queryJSON.policyID;
106+
}
107+
108+
const newAction = StackActions.push(SCREENS.SEARCH.ROOT, {
109+
...searchRoute,
110+
q: SearchQueryUtils.buildSearchQueryString(queryJSON),
111+
});
98112

99-
if (action.payload.policyID) {
100-
queryJSON.policyID = action.payload.policyID;
101-
} else {
102-
delete queryJSON.policyID;
113+
setActiveWorkspaceID(action.payload.policyID);
114+
return stackRouter.getStateForAction(state, newAction, configOptions);
103115
}
104-
105-
const newAction = StackActions.push(SCREENS.SEARCH.ROOT, {
106-
...currentParams,
107-
q: SearchQueryUtils.buildSearchQueryString(queryJSON),
108-
});
109-
110-
setActiveWorkspaceID(action.payload.policyID);
111-
return stackRouter.getStateForAction(state, newAction, configOptions);
112116
}
113117
if (lastRoute?.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR) {
114118
const newAction = StackActions.push(NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, {policyID: action.payload.policyID});
@@ -182,37 +186,42 @@ function handlePushSearchPageAction(
182186
stackRouter: Router<StackNavigationState<ParamListBase>, CommonActions.Action | StackActionType>,
183187
setActiveWorkspaceID: (workspaceID: string | undefined) => void,
184188
) {
185-
const currentParams = action.payload.params as RootNavigatorParamList[typeof SCREENS.SEARCH.ROOT];
186-
const queryJSON = SearchQueryUtils.buildSearchQueryJSON(currentParams.q);
187-
188-
if (!queryJSON) {
189-
return null;
190-
}
189+
let updatedAction = action;
190+
const currentParams = action.payload.params as RootNavigatorParamList[typeof NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR];
191+
if (currentParams?.screen === SCREENS.SEARCH.ROOT) {
192+
const searchParams = currentParams?.params;
193+
const queryJSON = SearchQueryUtils.buildSearchQueryJSON(searchParams.q);
194+
if (!queryJSON) {
195+
return null;
196+
}
191197

192-
if (!queryJSON.policyID) {
193-
const policyID = getPolicyIDFromState(state as State<RootNavigatorParamList>);
198+
if (!queryJSON.policyID) {
199+
const policyID = getPolicyIDFromState(state as State<RootNavigatorParamList>);
194200

195-
if (policyID) {
196-
queryJSON.policyID = policyID;
201+
if (policyID) {
202+
queryJSON.policyID = policyID;
203+
} else {
204+
delete queryJSON.policyID;
205+
}
197206
} else {
198-
delete queryJSON.policyID;
207+
setActiveWorkspaceID(queryJSON.policyID);
199208
}
200-
} else {
201-
setActiveWorkspaceID(queryJSON.policyID);
202-
}
203209

204-
const modifiedAction = {
205-
...action,
206-
payload: {
207-
...action.payload,
208-
params: {
209-
...action.payload.params,
210-
q: SearchQueryUtils.buildSearchQueryString(queryJSON),
210+
updatedAction = {
211+
...action,
212+
payload: {
213+
...action.payload,
214+
params: {
215+
...action.payload.params,
216+
params: {
217+
q: SearchQueryUtils.buildSearchQueryString(queryJSON),
218+
},
219+
},
211220
},
212-
},
213-
};
221+
};
222+
}
214223

215-
return stackRouter.getStateForAction(state, modifiedAction, configOptions);
224+
return stackRouter.getStateForAction(state, updatedAction, configOptions);
216225
}
217226

218227
/**

src/libs/Navigation/AppNavigator/createRootStackNavigator/RootStackRouter.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ import isSideModalNavigator from '@libs/Navigation/helpers/isSideModalNavigator'
88
import * as Welcome from '@userActions/Welcome';
99
import CONST from '@src/CONST';
1010
import NAVIGATORS from '@src/NAVIGATORS';
11-
import SCREENS from '@src/SCREENS';
12-
import * as GetStateForActionHandlers from './GetStateForActionHandlers';
11+
import {
12+
handleDismissModalAction,
13+
handleNavigatingToModalFromModal,
14+
handleOpenWorkspaceSplitAction,
15+
handlePushReportSplitAction,
16+
handlePushSearchPageAction,
17+
handleSwitchPolicyID,
18+
} from './GetStateForActionHandlers';
1319
import syncBrowserHistory from './syncBrowserHistory';
1420
import type {DismissModalActionType, OpenWorkspaceSplitActionType, PushActionType, RootStackNavigatorAction, RootStackNavigatorRouterOptions, SwitchPolicyIdActionType} from './types';
1521

@@ -65,24 +71,24 @@ function RootStackRouter(options: RootStackNavigatorRouterOptions) {
6571
...stackRouter,
6672
getStateForAction(state: StackNavigationState<ParamListBase>, action: RootStackNavigatorAction, configOptions: RouterConfigOptions) {
6773
if (isOpenWorkspaceSplitAction(action)) {
68-
return GetStateForActionHandlers.handleOpenWorkspaceSplitAction(state, action, configOptions, stackRouter);
74+
return handleOpenWorkspaceSplitAction(state, action, configOptions, stackRouter);
6975
}
7076

7177
if (isSwitchPolicyIdAction(action)) {
72-
return GetStateForActionHandlers.handleSwitchPolicyID(state, action, configOptions, stackRouter, setActiveWorkspaceID);
78+
return handleSwitchPolicyID(state, action, configOptions, stackRouter, setActiveWorkspaceID);
7379
}
7480

7581
if (isDismissModalAction(action)) {
76-
return GetStateForActionHandlers.handleDismissModalAction(state, configOptions, stackRouter);
82+
return handleDismissModalAction(state, configOptions, stackRouter);
7783
}
7884

7985
if (isPushAction(action)) {
8086
if (action.payload.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR) {
81-
return GetStateForActionHandlers.handlePushReportSplitAction(state, action, configOptions, stackRouter, setActiveWorkspaceID);
87+
return handlePushReportSplitAction(state, action, configOptions, stackRouter, setActiveWorkspaceID);
8288
}
8389

84-
if (action.payload.name === SCREENS.SEARCH.ROOT) {
85-
return GetStateForActionHandlers.handlePushSearchPageAction(state, action, configOptions, stackRouter, setActiveWorkspaceID);
90+
if (action.payload.name === NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR) {
91+
return handlePushSearchPageAction(state, action, configOptions, stackRouter, setActiveWorkspaceID);
8692
}
8793
}
8894

@@ -93,7 +99,7 @@ function RootStackRouter(options: RootStackNavigatorRouterOptions) {
9399
}
94100

95101
if (isNavigatingToModalFromModal(state, action)) {
96-
return GetStateForActionHandlers.handleNavigatingToModalFromModal(state, action, configOptions, stackRouter);
102+
return handleNavigatingToModalFromModal(state, action, configOptions, stackRouter);
97103
}
98104

99105
return stackRouter.getStateForAction(state, action, configOptions);

src/libs/Navigation/helpers/getPolicyIDFromState.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import extractPolicyIDFromQuery from './extractPolicyIDFromQuery';
1111
* - on Search related screens as policyID filter inside `q` (SearchQuery) param (only for SEARCH_CENTRAL_PANE)
1212
*/
1313
const getPolicyIDFromState = (state: State<RootNavigatorParamList>): string | undefined => {
14-
const lastPolicyRoute = state?.routes?.findLast((route) => route.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR || route.name === SCREENS.SEARCH.ROOT);
14+
const lastPolicyRoute = state?.routes?.findLast((route) => route.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR || route.name === NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR);
1515
if (lastPolicyRoute?.params && 'policyID' in lastPolicyRoute.params) {
1616
return lastPolicyRoute?.params?.policyID;
1717
}
1818

19-
if (lastPolicyRoute) {
20-
return extractPolicyIDFromQuery(lastPolicyRoute as NavigationPartialRoute<string>);
19+
// Handle SEARCH navigator
20+
const lastSearchRoute = lastPolicyRoute?.state?.routes?.findLast((route) => route.name === SCREENS.SEARCH.ROOT);
21+
22+
if (lastSearchRoute) {
23+
return extractPolicyIDFromQuery(lastSearchRoute as NavigationPartialRoute);
2124
}
2225

2326
return undefined;

tests/navigation/NavigateTests.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,17 @@ describe('Navigate', () => {
154154
index: 0,
155155
routes: [
156156
{
157-
name: SCREENS.SEARCH.ROOT,
158-
params: {
159-
q: 'type:expense status:all sortBy:date sortOrder:desc policyID:1',
157+
name: NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR,
158+
state: {
159+
index: 0,
160+
routes: [
161+
{
162+
name: SCREENS.SEARCH.ROOT,
163+
params: {
164+
q: 'type:expense status:all sortBy:date sortOrder:desc policyID:1',
165+
},
166+
},
167+
],
160168
},
161169
},
162170
],
@@ -167,7 +175,7 @@ describe('Navigate', () => {
167175
const rootStateBeforeNavigate = navigationRef.current?.getRootState();
168176
const lastSplitBeforeNavigate = rootStateBeforeNavigate?.routes.at(-1);
169177
expect(rootStateBeforeNavigate?.index).toBe(0);
170-
expect(lastSplitBeforeNavigate?.name).toBe(SCREENS.SEARCH.ROOT);
178+
expect(lastSplitBeforeNavigate?.name).toBe(NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR);
171179

172180
// When navigate to the Home page when the active workspace is set
173181
act(() => {

0 commit comments

Comments
 (0)