diff --git a/src/Expensify.js b/src/Expensify.js index 6ea72a9ba582..93224dfd9321 100644 --- a/src/Expensify.js +++ b/src/Expensify.js @@ -22,6 +22,7 @@ import compose from './libs/compose'; import withLocalize, {withLocalizePropTypes} from './components/withLocalize'; import * as User from './libs/actions/User'; import NetworkConnection from './libs/NetworkConnection'; +import Navigation from './libs/Navigation/Navigation'; Onyx.registerLogger(({level, message}) => { if (level === 'alert') { @@ -141,6 +142,9 @@ class Expensify extends PureComponent { setNavigationReady() { this.setState({isNavigationReady: true}); + + // Navigate to any pending routes now that the NavigationContainer is ready + Navigation.setIsNavigationReady(); } /** diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index ae54d59f7aca..e937e501597a 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -22,6 +22,8 @@ const drawerIsReadyPromise = new Promise((resolve) => { }); let isLoggedIn = false; +let pendingRoute = null; + Onyx.connect({ key: ONYXKEYS.SESSION, callback: val => isLoggedIn = Boolean(val && val.authToken), @@ -130,17 +132,22 @@ function isDrawerRoute(route) { */ function navigate(route = ROUTES.HOME) { if (!canNavigate('navigate', {route})) { + // Store intended route if the navigator is not yet available, + // we will try again after the NavigationContainer is ready + Log.hmmm(`[Navigation] Container not yet ready, storing route as pending: ${route}`); + pendingRoute = route; return; } if (route === ROUTES.HOME) { - if (isLoggedIn) { + if (isLoggedIn && pendingRoute === null) { openDrawer(); return; } // If we're navigating to the signIn page while logged out, pop whatever screen is on top // since it's guaranteed that the sign in page will be underneath (since it's the initial route). + // Also, if we're coming from a link to validate login (pendingRoute is not null), we want to pop the loading screen. navigationRef.current.dispatch(StackActions.pop()); return; } @@ -210,6 +217,19 @@ function isActiveRoute(routePath) { return getActiveRoute().substring(1) === routePath; } +/** + * Navigate to the route that we originally intended to go to + * but the NavigationContainer was not ready when navigate() was called + */ +function goToPendingRoute() { + if (pendingRoute === null) { + return; + } + Log.hmmm(`[Navigation] Container now ready, going to pending route: ${pendingRoute}`); + navigate(pendingRoute); + pendingRoute = null; +} + /** * @returns {Promise} */ @@ -218,6 +238,7 @@ function isNavigationReady() { } function setIsNavigationReady() { + goToPendingRoute(); resolveNavigationIsReadyPromise(); } diff --git a/src/libs/actions/User.js b/src/libs/actions/User.js index 4e9ccc88478c..c1f11e361065 100644 --- a/src/libs/actions/User.js +++ b/src/libs/actions/User.js @@ -18,14 +18,11 @@ import * as Link from './Link'; import getSkinToneEmojiFromIndex from '../../components/EmojiPicker/getSkinToneEmojiFromIndex'; import * as SequentialQueue from '../Network/SequentialQueue'; import PusherUtils from '../PusherUtils'; -import DateUtils from '../DateUtils'; -let sessionAuthToken = ''; let currentUserAccountID = ''; Onyx.connect({ key: ONYXKEYS.SESSION, callback: (val) => { - sessionAuthToken = lodashGet(val, 'authToken', ''); currentUserAccountID = lodashGet(val, 'accountID', ''); }, }); @@ -208,30 +205,22 @@ function setSecondaryLoginAndNavigate(login, password) { * @param {String} validateCode */ function validateLogin(accountID, validateCode) { - const isLoggedIn = !_.isEmpty(sessionAuthToken); Onyx.merge(ONYXKEYS.ACCOUNT, {...CONST.DEFAULT_ACCOUNT_DATA, isLoading: true}); - DeprecatedAPI.ValidateEmail({ + const optimisticData = [ + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: ONYXKEYS.ACCOUNT, + value: { + isLoading: false, + }, + }, + ]; + API.write('ValidateLogin', { accountID, validateCode, - }).then((response) => { - if (response.jsonCode === 200) { - const {email} = response; - - if (isLoggedIn) { - getUserDetails(); - } else { - // Let the user know we've successfully validated their login - const success = lodashGet(response, 'message', `Your secondary login ${email} has been validated.`); - Onyx.merge(ONYXKEYS.ACCOUNT, {success}); - } - } else { - Onyx.merge(ONYXKEYS.ACCOUNT, {errors: {[DateUtils.getMicroseconds()]: Localize.translateLocal('resendValidationForm.validationCodeFailedMessage')}}); - } - }).finally(() => { - Onyx.merge(ONYXKEYS.ACCOUNT, {isLoading: false}); - Navigation.navigate(ROUTES.HOME); - }); + }, {optimisticData}); + Navigation.navigate(ROUTES.HOME); } /**