diff --git a/src/Expensify.js b/src/Expensify.js index 3b4a8797425e7..0de40845ed703 100644 --- a/src/Expensify.js +++ b/src/Expensify.js @@ -1,6 +1,7 @@ import React, {Component} from 'react'; import {View} from 'react-native'; import PropTypes from 'prop-types'; +import {recordCurrentRoute} from './lib/actions/App'; // import {Beforeunload} from 'react-beforeunload'; import SignInPage from './page/SignInPage'; @@ -40,7 +41,6 @@ class Expensify extends Component { // Initialize this client as being an active client ActiveClientManager.init(); - this.recordCurrentRoute = this.recordCurrentRoute.bind(this); this.removeLoadingState = this.removeLoadingState.bind(this); this.state = { @@ -65,15 +65,6 @@ class Expensify extends Component { }); } - /** - * Keep the current route match stored in Ion so other libs can access it - * - * @param {object} params.match - */ - recordCurrentRoute({match}) { - Ion.set(IONKEYS.CURRENT_URL, match.url); - } - render() { // Until the authToken has been initialized from Ion, display a blank page if (this.state.isLoading) { @@ -94,7 +85,7 @@ class Expensify extends Component { {/* If there is ever a property for redirecting, we do the redirect here */} {/* Leave this as a ternary or else iOS throws an error about text not being wrapped in */} {redirectTo ? : null} - + diff --git a/src/lib/actions/App.js b/src/lib/actions/App.js new file mode 100644 index 0000000000000..f9d591d4dc747 --- /dev/null +++ b/src/lib/actions/App.js @@ -0,0 +1,37 @@ +import Ion from '../Ion'; +import IONKEYS from '../../IONKEYS'; + +let currentRedirectTo; +Ion.connect({ + key: IONKEYS.APP_REDIRECT_TO, + callback: val => currentRedirectTo = val, +}); + +/** + * Keep the current route match stored in Ion so other libs can access it + * Also reset the app_redirect_to in Ion so that if we go back to the current url the state will update + * + * @param {object} match + */ +function recordCurrentRoute({match}) { + Ion.set(IONKEYS.CURRENT_URL, match.url); + if (match.url === currentRedirectTo) { + Ion.set(IONKEYS.APP_REDIRECT_TO, ''); + } +} + + +/** + * Redirect the app to a new page by updating the state in Ion + * + * @param {mixed} url + */ +function redirect(url) { + const formattedURL = (typeof url === 'string' && url.startsWith('/')) ? url : `/${url}`; + Ion.set(IONKEYS.APP_REDIRECT_TO, formattedURL); +} + +export { + recordCurrentRoute, + redirect, +}; diff --git a/src/lib/actions/Report.js b/src/lib/actions/Report.js index fcdede6a91673..ef25d2e643595 100644 --- a/src/lib/actions/Report.js +++ b/src/lib/actions/Report.js @@ -10,6 +10,7 @@ import promiseAllSettled from '../promiseAllSettled'; import ExpensiMark from '../ExpensiMark'; import Notification from '../Notification'; import * as PersonalDetails from './PersonalDetails'; +import {redirect} from './App'; let currentUserEmail; let currentUserAccountID; @@ -211,12 +212,13 @@ function updateReportWithNewAction(reportID, reportAction) { return; } + console.debug('[NOTIFICATION] Creating notification'); Notification.showCommentNotification({ reportAction, onClick: () => { // Navigate to this report onClick - Ion.set(IONKEYS.APP_REDIRECT_TO, `/${reportID}`); + redirect(reportID); } }); } diff --git a/src/lib/actions/SignInRedirect.js b/src/lib/actions/SignInRedirect.js index ff7c1a7473948..cfd1a8ad91db1 100644 --- a/src/lib/actions/SignInRedirect.js +++ b/src/lib/actions/SignInRedirect.js @@ -1,6 +1,7 @@ import Ion from '../Ion'; import IONKEYS from '../../IONKEYS'; import ROUTES from '../../ROUTES'; +import {redirect} from './App'; /** * Redirects to the sign in page and handles adding any exitTo params to the URL. @@ -24,7 +25,7 @@ function redirectToSignIn() { const urlWithExitTo = url === '/' ? ROUTES.SIGNIN : `${ROUTES.SIGNIN}/exitTo${url}`; - return Ion.set(IONKEYS.APP_REDIRECT_TO, urlWithExitTo); + return redirect(urlWithExitTo); }); } diff --git a/src/page/home/sidebar/ChatSwitcherView.js b/src/page/home/sidebar/ChatSwitcherView.js index f2208a3bee86a..92918480a876d 100644 --- a/src/page/home/sidebar/ChatSwitcherView.js +++ b/src/page/home/sidebar/ChatSwitcherView.js @@ -9,6 +9,7 @@ import KeyboardShortcut from '../../../lib/KeyboardShortcut'; import ChatSwitcherList from './ChatSwitcherList'; import ChatSwitcherSearchForm from './ChatSwitcherSearchForm'; import {fetchOrCreateChatReport} from '../../../lib/actions/Report'; +import {redirect} from '../../../lib/actions/App'; const propTypes = { // A method that is triggered when the TextInput gets focus @@ -126,7 +127,7 @@ class ChatSwitcherView extends React.Component { fetchChatReportAndRedirect(option) { Ion.get(IONKEYS.MY_PERSONAL_DETAILS, 'login') .then(currentLogin => fetchOrCreateChatReport([currentLogin, option.login])) - .then(reportID => Ion.set(IONKEYS.APP_REDIRECT_TO, `/${reportID}`)); + .then(reportID => redirect(reportID)); this.reset(); } diff --git a/src/page/home/sidebar/SidebarLinks.js b/src/page/home/sidebar/SidebarLinks.js index 1af2ef40dd678..356a474e2585d 100644 --- a/src/page/home/sidebar/SidebarLinks.js +++ b/src/page/home/sidebar/SidebarLinks.js @@ -15,6 +15,7 @@ import ChatSwitcherView from './ChatSwitcherView'; import SafeAreaInsetPropTypes from '../../SafeAreaInsetPropTypes'; import compose from '../../../lib/compose'; import {withRouter} from '../../../lib/Router'; +import {redirect} from '../../../lib/actions/App'; const propTypes = { // These are from withRouter @@ -125,7 +126,7 @@ export default compose( // If we're on the home page, then redirect to the first report ID if (currentURL === '/' && firstReportID) { - Ion.set(IONKEYS.APP_REDIRECT_TO, `/${firstReportID}`); + redirect(firstReportID); } }); }),