Skip to content

Commit

Permalink
fix storing policyID in q param
Browse files Browse the repository at this point in the history
  • Loading branch information
Kicu committed Sep 2, 2024
1 parent 7426771 commit 87df38f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function handleQueryWithPolicyID(query: SearchQueryString, activePolicyID?: stri
return query;
}

const policyID = queryJSON.policyID ?? activePolicyID;
const policyID = activePolicyID ?? queryJSON.policyID;
const policy = PolicyUtils.getPolicy(policyID);

// In case policy is missing or there is no policy currently selected via WorkspaceSwitcher we remove it
Expand Down
19 changes: 13 additions & 6 deletions src/libs/Navigation/getPolicyIDFromState.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import SCREENS from '@src/SCREENS';
import extractPolicyIDFromQuery from './extractPolicyIDFromQuery';
import getTopmostBottomTabRoute from './getTopmostBottomTabRoute';
import getTopmostCentralPaneRoute from './getTopmostCentralPaneRoute';
import type {RootStackParamList, State} from './types';

/**
* returns policyID value if one exists in navigation state
*
* PolicyID in this app can be stored in two ways:
* - on most screens but NOT Search as `policyID` param
* - on Search related screens as policyID filter inside `q` (SearchQuery) param
* - on most screens but NOT Search as `policyID` param (on bottom tab screens)
* - on Search related screens as policyID filter inside `q` (SearchQuery) param (only for SEARCH_CENTRAL_PANE)
*/
const getPolicyIDFromState = (state: State<RootStackParamList>): string | undefined => {
const topmostBottomTabRoute = getTopmostBottomTabRoute(state);

const policyID = topmostBottomTabRoute && topmostBottomTabRoute.params && 'policyID' in topmostBottomTabRoute.params && topmostBottomTabRoute.params?.policyID;
if (policyID) {
return topmostBottomTabRoute.params?.policyID as string;
if (!topmostBottomTabRoute) {
return;
}

if (topmostBottomTabRoute.name === SCREENS.SEARCH.BOTTOM_TAB) {
const topmostCentralPaneRoute = getTopmostCentralPaneRoute(state);
return extractPolicyIDFromQuery(topmostCentralPaneRoute);
}

return extractPolicyIDFromQuery(topmostBottomTabRoute);
const policyID = topmostBottomTabRoute && topmostBottomTabRoute.params && 'policyID' in topmostBottomTabRoute.params && topmostBottomTabRoute.params?.policyID;
return policyID ? (topmostBottomTabRoute.params?.policyID as string) : undefined;
};

export default getPolicyIDFromState;
18 changes: 8 additions & 10 deletions src/libs/Navigation/switchPolicyID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ function getActionForBottomTabNavigator(action: StackNavigationAction, state: Na

if (name === SCREENS.SEARCH.CENTRAL_PANE) {
name = SCREENS.SEARCH.BOTTOM_TAB;
}

if (!params) {
} else if (!params) {
params = {policyID};
} else {
params.policyID = policyID;
Expand Down Expand Up @@ -109,19 +107,19 @@ export default function switchPolicyID(navigation: NavigationContainerRef<RootSt
// If the layout is wide we need to push matching central pane route to the stack.
if (shouldAddToCentralPane) {
const params: CentralPaneRouteParams = {...topmostCentralPaneRoute?.params};

if (isOpeningSearchFromBottomTab && params.q) {
delete params.policyID;
const queryJSON = SearchUtils.buildSearchQueryJSON(params.q);

if (policyID) {
const queryJSON = SearchUtils.buildSearchQueryJSON(params.q);
if (queryJSON) {
queryJSON.policyID = policyID;
params.q = SearchUtils.buildSearchQueryString(queryJSON);
}
} else {
const queryJSON = SearchUtils.buildSearchQueryJSON(params.q);
if (queryJSON) {
delete queryJSON.policyID;
params.q = SearchUtils.buildSearchQueryString(queryJSON);
}
} else if (queryJSON) {
delete queryJSON.policyID;
params.q = SearchUtils.buildSearchQueryString(queryJSON);
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ type BackToAndForwardToParms = {
forwardTo?: Routes;
};

type SearchNavigatorParamList = {
[SCREENS.SEARCH.BOTTOM_TAB]: undefined;
[SCREENS.SEARCH.CENTRAL_PANE]: undefined;
[SCREENS.SEARCH.REPORT_RHP]: undefined;
};

type SettingsNavigatorParamList = {
[SCREENS.SETTINGS.SHARE_CODE]: undefined;
[SCREENS.SETTINGS.PROFILE.ROOT]: undefined;
Expand Down Expand Up @@ -1248,7 +1242,7 @@ type ExplanationModalNavigatorParamList = {

type BottomTabNavigatorParamList = {
[SCREENS.HOME]: {policyID?: string};
[SCREENS.SEARCH.BOTTOM_TAB]: CentralPaneScreensParamList[typeof SCREENS.SEARCH.CENTRAL_PANE];
[SCREENS.SEARCH.BOTTOM_TAB]: undefined;
[SCREENS.SETTINGS.ROOT]: {policyID?: string};
};

Expand Down Expand Up @@ -1396,7 +1390,6 @@ export type {
RoomInviteNavigatorParamList,
RoomMembersNavigatorParamList,
RootStackParamList,
SearchNavigatorParamList,
SettingsNavigatorParamList,
SignInNavigatorParamList,
FeatureTrainingNavigatorParamList,
Expand Down
11 changes: 2 additions & 9 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,9 @@ function getSortedReportData(data: ReportListItemType[]) {
function getCurrentSearchParams() {
const rootState = navigationRef.getRootState() as State<RootStackParamList>;

const lastSearchCentralPaneRoute = rootState.routes.filter((route) => route.name === SCREENS.SEARCH.CENTRAL_PANE).at(-1);
const lastSearchBottomTabRoute = rootState.routes[0].state?.routes.filter((route) => route.name === SCREENS.SEARCH.BOTTOM_TAB).at(-1);
const lastSearchRoute = rootState.routes.filter((route) => route.name === SCREENS.SEARCH.CENTRAL_PANE).at(-1);

if (lastSearchCentralPaneRoute) {
return lastSearchCentralPaneRoute.params as AuthScreensParamList[typeof SCREENS.SEARCH.CENTRAL_PANE];
}

if (lastSearchBottomTabRoute) {
return lastSearchBottomTabRoute.params as BottomTabNavigatorParamList[typeof SCREENS.SEARCH.BOTTOM_TAB];
}
return lastSearchRoute ? (lastSearchRoute.params as AuthScreensParamList[typeof SCREENS.SEARCH.CENTRAL_PANE]) : undefined;
}

function isSearchResultsEmpty(searchResults: SearchResults) {
Expand Down

0 comments on commit 87df38f

Please sign in to comment.