Skip to content

Commit

Permalink
Navigate in transition page so navigation is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
neil-marcellini committed May 2, 2022
1 parent 4c65e1e commit d1800a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
42 changes: 17 additions & 25 deletions src/libs/Navigation/AppNavigator/AuthScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,12 @@ class AuthScreens extends React.Component {
// Load policies, maybe creating a new policy first.
Linking.getInitialURL()
.then((url) => {
if (!url) {
return;
}
const path = new URL(url).pathname;
const params = new URLSearchParams(url);
const exitTo = params.get('exitTo');
const email = params.get('email');
const isLoggingInAsNewUser = this.props.session && this.props.session.email !== email;
const shouldCreateFreePolicy = !isLoggingInAsNewUser
&& Str.startsWith(path, Str.normalizeUrl(ROUTES.TRANSITION))
&& exitTo === ROUTES.WORKSPACE_NEW;
if (shouldCreateFreePolicy) {
if (this.shouldCreateFreePolicy(url)) {
Policy.createAndGetPolicyList();
return;
}

Policy.getPolicyList();
if (!isLoggingInAsNewUser && exitTo) {
this.navigateToExitRoute(exitTo);
}
});

// Refresh the personal details, timezone and betas every 30 minutes
Expand Down Expand Up @@ -198,17 +185,22 @@ class AuthScreens extends React.Component {
}

/**
* Navigate to the transition exit route
*
* @param {String} exitTo
* @param {String} [url]
* @returns {Boolean}
*/
navigateToExitRoute(exitTo) {
// In order to navigate to a modal, we first have to dismiss the current modal. Without dismissing the current modal, if the user cancels out of the workspace modal,
// then they will be routed back to /transition/<accountID>/<email>/<authToken>/workspace/<policyID>/card and we don't want that. We want them to go back to `/`
// and by calling dismissModal(), the /transition/... route is removed from history so the user will get taken to `/` if they cancel out of the new workspace modal.
Log.info('[AuthScreens] Dismissing LogOutOldUserPage and navigating to the transition exit route');
Navigation.dismissModal();
Navigation.navigate(exitTo);
shouldCreateFreePolicy(url = '') {
if (!url) {
return false;
}

const path = new URL(url).pathname;
const params = new URLSearchParams(url);
const exitTo = params.get('exitTo');
const email = params.get('email');
const isLoggingInAsNewUser = !_.isNull(this.props.session.email) && (email !== this.props.session.email);
return !isLoggingInAsNewUser
&& Str.startsWith(path, Str.normalizeUrl(ROUTES.TRANSITION))
&& exitTo === ROUTES.WORKSPACE_NEW;
}

render() {
Expand Down
18 changes: 18 additions & 0 deletions src/pages/LogOutOldUserPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import ONYXKEYS from '../ONYXKEYS';
import * as Session from '../libs/actions/Session';
import FullScreenLoadingIndicator from '../components/FullscreenLoadingIndicator';
import Log from '../libs/Log';
import Navigation from '../libs/Navigation/Navigation';
import ROUTES from '../ROUTES';

const propTypes = {
/** The parameters needed to authenticate with a short lived token are in the URL */
Expand Down Expand Up @@ -52,7 +54,23 @@ class LogOutOldUserPage extends Component {
if (this.props.session && this.props.session.email !== email) {
Log.info('[LogOutOldUserPage] Different user signed in - signing out');
Session.signOutAndRedirectToSignIn();
return;
}

// exitTo is URI encoded because it could contain a variable number of slashes (i.e. "workspace/new" vs "workspace/<ID>/card")
const exitTo = decodeURIComponent(lodashGet(this.props.route.params, 'exitTo', ''));
if (exitTo === ROUTES.WORKSPACE_NEW) {
// New workspace creation is handled in AuthScreens, not in its own screen
Log.info('[LoginWithShortLivedTokenPage] exitTo is workspace/new - handling new workspace creation in AuthScreens');
return;
}

// In order to navigate to a modal, we first have to dismiss the current modal. Without dismissing the current modal, if the user cancels out of the workspace modal,
// then they will be routed back to /transition/<accountID>/<email>/<authToken>/workspace/<policyID>/card and we don't want that. We want them to go back to `/`
// and by calling dismissModal(), the /transition/... route is removed from history so the user will get taken to `/` if they cancel out of the new workspace modal.
Log.info('[LoginWithShortLivedTokenPage] Dismissing LoginWithShortLivedTokenPage and navigating to exitTo');
Navigation.dismissModal();
Navigation.navigate(exitTo);
}

render() {
Expand Down

0 comments on commit d1800a5

Please sign in to comment.