Skip to content
Merged
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
52 changes: 16 additions & 36 deletions src/libs/Navigation/CustomActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,28 @@ import lodashGet from 'lodash/get';
import linkingConfig from './linkingConfig';
import navigationRef from './navigationRef';

/**
* @returns {Object}
*/
function getActiveState() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a duplicate method for this....Please remove the other one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks rajat. Just wanted to add to remember to always test your PR before putting it out for review. If you test the current code on ios you'll see,
image

which would quickly guide you to remove the extra method. Additionally, if you need more time you can always create a draft PR first without any assignees such that once its ready for review you can click on the ready for review button and it will then auto assign the reviewers.

// We use our RootState as the dispatch's state is relative to the active navigator and might not contain our active screen.
return navigationRef.current.getRootState();
}

/**
* Go back to the Main Drawer
* @param {Object} navigationRef
*/
function navigateBackToRootDrawer() {
let isLeavingNestedDrawerNavigator = false;

// This should take us to the first view of the modal's stack navigator
navigationRef.current.dispatch((state) => {
// If this is a nested drawer navigator then we pop the screen and
// prevent calling goBack() as it's default behavior is to toggle open the active drawer
if (state.type === 'drawer') {
isLeavingNestedDrawerNavigator = true;
return StackActions.pop();
}

// If there are multiple routes then we can pop back to the first route
if (state.routes.length > 1) {
return StackActions.popToTop();
}

// Otherwise, we are already on the last page of a modal so just do nothing here as goBack() will navigate us
// back to the screen we were on before we opened the modal.
return StackActions.pop(0);
const activeState = getActiveState();

// To navigate to the main drawer Route, pop to the first route on the Root Stack Navigator as the main drawer is always the first route that is activated.
// It will pop all fullscreen and RHN modals that are over the main drawer.
// It won't work when the main drawer is not the first route of the Root Stack Navigator which is not the case ATM.
navigationRef.current.dispatch({
...StackActions.popToTop(),
target: activeState.key,
});

if (isLeavingNestedDrawerNavigator) {
return;
}

// Navigate back to where we were before we launched the modal
if (navigationRef.current.canGoBack()) {
navigationRef.current.goBack();
}
}

/**
Expand Down Expand Up @@ -69,14 +57,6 @@ function getScreenNameFromState(state) {
return getRouteFromState(state).name || '';
}

/**
* @returns {Object}
*/
function getActiveState() {
// We use our RootState as the dispatch's state is relative to the active navigator and might not contain our active screen.
return navigationRef.current.getRootState();
}

/**
* Special accomodation must be made for navigating to a screen inside a DrawerNavigator (e.g. our ReportScreen). The web/mWeb default behavior when
* calling "navigate()" does not give us the browser history we would expect for a typical web paradigm (e.g. that navigating from one screen another
Expand Down