Skip to content

Commit

Permalink
Merge pull request #3945 from rdjuric/avoidResetIfSameReport
Browse files Browse the repository at this point in the history
When navigating to the same report, don't reset the navigation state
  • Loading branch information
Joel Bettner authored Jul 9, 2021
2 parents 2e81800 + b353c8d commit 16ff439
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/libs/Navigation/CustomActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {CommonActions} from '@react-navigation/native';
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
Expand All @@ -10,10 +11,24 @@ import {CommonActions} from '@react-navigation/native';
*
* @param {String} screenName
* @param {Object} params
* @param {Object} navigationRef
* @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
// 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', '');

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.
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 16ff439

Please sign in to comment.