From 5392b028558e01f7e8c322a34822e3b72ecb452c Mon Sep 17 00:00:00 2001 From: Rafael Date: Thu, 8 Jul 2021 22:04:47 -0300 Subject: [PATCH 1/3] some changes to pushDrawerRoute and Navigation --- src/libs/Navigation/CustomActions.js | 19 +++++++++++++++++-- src/libs/Navigation/Navigation.js | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/libs/Navigation/CustomActions.js b/src/libs/Navigation/CustomActions.js index d29cbed73b9f..b1be25f0f5b4 100644 --- a/src/libs/Navigation/CustomActions.js +++ b/src/libs/Navigation/CustomActions.js @@ -1,4 +1,4 @@ -import {CommonActions} from '@react-navigation/native'; +import {CommonActions, StackActions, DrawerActions} from '@react-navigation/native'; /** * In order to create the desired browser navigation behavior on web and mobile web we need to replace any @@ -12,8 +12,23 @@ import {CommonActions} from '@react-navigation/native'; * @param {Object} params * @returns {Function} */ -function pushDrawerRoute(screenName, params) { +function pushDrawerRoute(screenName, params, navigationRef) { return (state) => { + + + // Avoid the navigation and refocus the report if we're trying to navigate to our active report + const rootState = navigationRef.current.getRootState(); + const activeReportID = rootState.routes[0].state.routes[0].params.reportID; + + console.log(`activeReportID: ${activeReportID}, params.reportID: ${params.reportID}`) + console.log(params); + if (activeReportID === params.reportID) { + if (state.type != 'drawer') { + navigationRef.current.dispatch(StackActions.pop()); + } + return DrawerActions.closeDrawer(); + } + // Non Drawer navigators have routes and not history so we'll fallback to navigate() in the case where we are // unable to push a new screen onto the history stack e.g. navigating to a ReportScreen via a modal screen. // Note: One downside of this is that the history will be reset. diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 480d4fa4c3ba..8d0b307a0d00 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -62,7 +62,7 @@ function navigate(route = ROUTES.HOME) { // have a participants route since those should go through linkTo() as they open a different screen. const {reportID, isParticipantsRoute} = ROUTES.parseReportRouteParams(route); if (reportID && !isParticipantsRoute) { - navigationRef.current.dispatch(CustomActions.pushDrawerRoute(SCREENS.REPORT, {reportID})); + navigationRef.current.dispatch(CustomActions.pushDrawerRoute(SCREENS.REPORT, {reportID}, navigationRef)); return; } From 05a3bed3b214bdb72754893e2dd4a77c3e0a792b Mon Sep 17 00:00:00 2001 From: Rafael Date: Fri, 9 Jul 2021 00:41:14 -0300 Subject: [PATCH 2/3] changes to customActions --- src/libs/Navigation/CustomActions.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libs/Navigation/CustomActions.js b/src/libs/Navigation/CustomActions.js index b1be25f0f5b4..1a7f596a0a15 100644 --- a/src/libs/Navigation/CustomActions.js +++ b/src/libs/Navigation/CustomActions.js @@ -1,4 +1,5 @@ import {CommonActions, StackActions, DrawerActions} from '@react-navigation/native'; +import lodashGet from 'lodash/get'; /** * In order to create the desired browser navigation behavior on web and mobile web we need to replace any @@ -17,13 +18,13 @@ function pushDrawerRoute(screenName, params, navigationRef) { // Avoid the navigation and refocus the report if we're trying to navigate to our active report + // We use our RootState as the dispatch's state is relative to the active navigator and might + // not contain our active report. const rootState = navigationRef.current.getRootState(); - const activeReportID = rootState.routes[0].state.routes[0].params.reportID; + const activeReportID = lodashGet(rootState, 'routes[0].state.routes[0].params.reportID', '') - console.log(`activeReportID: ${activeReportID}, params.reportID: ${params.reportID}`) - console.log(params); if (activeReportID === params.reportID) { - if (state.type != 'drawer') { + if (state.type !== 'drawer') { navigationRef.current.dispatch(StackActions.pop()); } return DrawerActions.closeDrawer(); From b353c8d44db20347bcf9ac20411f60b7a96d2d75 Mon Sep 17 00:00:00 2001 From: Rafael Date: Fri, 9 Jul 2021 01:18:41 -0300 Subject: [PATCH 3/3] lint fixes --- src/libs/Navigation/CustomActions.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libs/Navigation/CustomActions.js b/src/libs/Navigation/CustomActions.js index 1a7f596a0a15..5f8762600acf 100644 --- a/src/libs/Navigation/CustomActions.js +++ b/src/libs/Navigation/CustomActions.js @@ -11,17 +11,16 @@ import lodashGet from 'lodash/get'; * * @param {String} screenName * @param {Object} params + * @param {Object} navigationRef * @returns {Function} */ function pushDrawerRoute(screenName, params, navigationRef) { return (state) => { - - // Avoid the navigation and refocus the report if we're trying to navigate to our active report // We use our RootState as the dispatch's state is relative to the active navigator and might // not contain our active report. const rootState = navigationRef.current.getRootState(); - const activeReportID = lodashGet(rootState, 'routes[0].state.routes[0].params.reportID', '') + const activeReportID = lodashGet(rootState, 'routes[0].state.routes[0].params.reportID', ''); if (activeReportID === params.reportID) { if (state.type !== 'drawer') {