From d42410d5670107dd70a0ead3266bf945e9a3a725 Mon Sep 17 00:00:00 2001 From: Hitesh Agja Date: Sun, 13 Feb 2022 23:53:03 +0530 Subject: [PATCH 1/4] Improvement in navigateBackToRootDrawer method in CustomActions --- src/libs/Navigation/CustomActions.js | 40 +++++++++------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/src/libs/Navigation/CustomActions.js b/src/libs/Navigation/CustomActions.js index 245d5d7340b..08d29d0e9c9 100644 --- a/src/libs/Navigation/CustomActions.js +++ b/src/libs/Navigation/CustomActions.js @@ -6,40 +6,24 @@ import lodashGet from 'lodash/get'; import linkingConfig from './linkingConfig'; import navigationRef from './navigationRef'; +/** + * @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(); +} + /** * 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(); + 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(); - } } /** From 14a938b68ae3d9a3738e81d6ef8bb0b639c09568 Mon Sep 17 00:00:00 2001 From: Hitesh Agja Date: Mon, 14 Feb 2022 20:00:30 +0530 Subject: [PATCH 2/4] Code comment and explaination for navigationRef.current.dispatch. --- src/libs/Navigation/CustomActions.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libs/Navigation/CustomActions.js b/src/libs/Navigation/CustomActions.js index 08d29d0e9c9..e87ebe9b31e 100644 --- a/src/libs/Navigation/CustomActions.js +++ b/src/libs/Navigation/CustomActions.js @@ -20,6 +20,9 @@ function getActiveState() { */ function navigateBackToRootDrawer() { const activeState = getActiveState(); + + //Dispatch "popToTop" action takes you back to the first screen in the stack at the same time + //it will prevent bubbling of action and limit it to activeState by specifing target. navigationRef.current.dispatch({ ...StackActions.popToTop(), target: activeState.key, From 4db894e6727057ca2cc8a029e75399d8ccfc0fbc Mon Sep 17 00:00:00 2001 From: Hitesh Agja Date: Tue, 15 Feb 2022 11:21:40 +0530 Subject: [PATCH 3/4] Code comment updated for navigationRef.current.dispatch explaination. Also removed duplicate getActiveState method. --- src/libs/Navigation/CustomActions.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/libs/Navigation/CustomActions.js b/src/libs/Navigation/CustomActions.js index e87ebe9b31e..656b89b79b3 100644 --- a/src/libs/Navigation/CustomActions.js +++ b/src/libs/Navigation/CustomActions.js @@ -21,8 +21,9 @@ function getActiveState() { function navigateBackToRootDrawer() { const activeState = getActiveState(); - //Dispatch "popToTop" action takes you back to the first screen in the stack at the same time - //it will prevent bubbling of action and limit it to activeState by specifing target. + // 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, @@ -56,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 From aa5f9a4e9617964f3871b382565843b9045db50a Mon Sep 17 00:00:00 2001 From: Hitesh Agja Date: Wed, 16 Feb 2022 19:03:53 +0530 Subject: [PATCH 4/4] trailing space lint error resolved. --- src/libs/Navigation/CustomActions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Navigation/CustomActions.js b/src/libs/Navigation/CustomActions.js index 656b89b79b3..aa3083c573d 100644 --- a/src/libs/Navigation/CustomActions.js +++ b/src/libs/Navigation/CustomActions.js @@ -22,7 +22,7 @@ function navigateBackToRootDrawer() { 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 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(),