Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When navigating to the same report, don't reset the navigation state #3945

Merged
merged 3 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 18 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 @@ -12,8 +13,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
// 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', '')
Copy link
Contributor Author

@rdjuric rdjuric Jul 9, 2021

Choose a reason for hiding this comment

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

This doesn't look very good but should be safe. Maybe a better idea would be making this a getActiveReport(rootState) function that searches for a route of type === 'drawer' and get's the reportID from it?

Not sure.


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