From 2eceaa7b74ae9122de57670271549a16059034be Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Mon, 7 Sep 2020 17:57:56 -0600 Subject: [PATCH 1/7] Update the redirectTo when clicking links in teh sidebar --- src/page/home/sidebar/SidebarLink.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/page/home/sidebar/SidebarLink.js b/src/page/home/sidebar/SidebarLink.js index ade838375d665..b9076fde35d4b 100644 --- a/src/page/home/sidebar/SidebarLink.js +++ b/src/page/home/sidebar/SidebarLink.js @@ -8,6 +8,7 @@ import styles from '../../../style/StyleSheet'; import withIon from '../../../components/withIon'; import PressableLink from '../../../components/PressableLink'; import compose from '../../../lib/compose'; +import Ion from '../../../lib/Ion'; const propTypes = { // The ID of the report for this link @@ -33,6 +34,11 @@ const defaultProps = { isUnread: false, }; +const onClick = (props) => { + Ion.set(IONKEYS.APP_REDIRECT_TO, `/${props.reportID}`); + props.onLinkClick(); +}; + const SidebarLink = (props) => { const reportIDInUrl = parseInt(props.match.params.reportID, 10); const isReportActive = reportIDInUrl === props.reportID; @@ -44,7 +50,7 @@ const SidebarLink = (props) => { return ( - + onClick(props)} to={`/${props.reportID}`} style={linkActiveStyle}> {props.reportName} From d9e1700505a460cf32221e50367414f84e9d3334 Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Tue, 8 Sep 2020 14:02:15 -0600 Subject: [PATCH 2/7] Create actions/app.js and make all app_redirects use the new redirect method --- src/Expensify.js | 12 ++--------- src/lib/actions/App.js | 25 +++++++++++++++++++++++ src/lib/actions/Report.js | 3 ++- src/lib/actions/SignInRedirect.js | 3 ++- src/page/home/sidebar/ChatSwitcherView.js | 3 ++- src/page/home/sidebar/SidebarLink.js | 3 ++- src/page/home/sidebar/SidebarLinks.js | 3 ++- 7 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 src/lib/actions/App.js diff --git a/src/Expensify.js b/src/Expensify.js index 3b4a8797425e7..e555f98343799 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'; @@ -65,15 +66,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 +86,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..706a9e0151ef9 --- /dev/null +++ b/src/lib/actions/App.js @@ -0,0 +1,25 @@ +import Ion from '../Ion'; +import IONKEYS from '../../IONKEYS'; + +/** + * 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} params.match + */ +function recordCurrentRoute({match}) { + Ion.set(IONKEYS.CURRENT_URL, match.url); + if (match.url === this.props.redirectTo) { + Ion.set(IONKEYS.APP_REDIRECT_TO, ''); + } +} + +function redirect(url) { + const formattedURL = url.charAt(0) === '/' ? 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 928e9521e7fd2..d1274c538160f 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.js' // List of reportIDs that we define in .env const configReportIDs = CONFIG.REPORT_IDS.split(',').map(Number); @@ -234,7 +235,7 @@ function updateReportWithNewAction(reportID, reportAction) { 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..a648117d897fb 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.js'; /** * 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 66598d0f6e966..b694bac3a2a5e 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 {fetchChatReport} 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 => fetchChatReport([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/SidebarLink.js b/src/page/home/sidebar/SidebarLink.js index b9076fde35d4b..31b109dd3c638 100644 --- a/src/page/home/sidebar/SidebarLink.js +++ b/src/page/home/sidebar/SidebarLink.js @@ -9,6 +9,7 @@ import withIon from '../../../components/withIon'; import PressableLink from '../../../components/PressableLink'; import compose from '../../../lib/compose'; import Ion from '../../../lib/Ion'; +import {redirect} from '../../../lib/actions/App'; const propTypes = { // The ID of the report for this link @@ -35,7 +36,7 @@ const defaultProps = { }; const onClick = (props) => { - Ion.set(IONKEYS.APP_REDIRECT_TO, `/${props.reportID}`); + redirect(props.reportID); props.onLinkClick(); }; diff --git a/src/page/home/sidebar/SidebarLinks.js b/src/page/home/sidebar/SidebarLinks.js index a9d8af337f588..b7b14da56dcc8 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 @@ -123,7 +124,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); } }); }), From 4d16031a587bdbb15f5893b8314b28e8675362ec Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Tue, 8 Sep 2020 14:04:24 -0600 Subject: [PATCH 3/7] Remove original solution --- src/page/home/sidebar/SidebarLink.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/page/home/sidebar/SidebarLink.js b/src/page/home/sidebar/SidebarLink.js index 31b109dd3c638..ade838375d665 100644 --- a/src/page/home/sidebar/SidebarLink.js +++ b/src/page/home/sidebar/SidebarLink.js @@ -8,8 +8,6 @@ import styles from '../../../style/StyleSheet'; import withIon from '../../../components/withIon'; import PressableLink from '../../../components/PressableLink'; import compose from '../../../lib/compose'; -import Ion from '../../../lib/Ion'; -import {redirect} from '../../../lib/actions/App'; const propTypes = { // The ID of the report for this link @@ -35,11 +33,6 @@ const defaultProps = { isUnread: false, }; -const onClick = (props) => { - redirect(props.reportID); - props.onLinkClick(); -}; - const SidebarLink = (props) => { const reportIDInUrl = parseInt(props.match.params.reportID, 10); const isReportActive = reportIDInUrl === props.reportID; @@ -51,7 +44,7 @@ const SidebarLink = (props) => { return ( - onClick(props)} to={`/${props.reportID}`} style={linkActiveStyle}> + {props.reportName} From caeb6c29db4e9ff8c914d7ccfc265375249c261f Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Tue, 8 Sep 2020 15:02:42 -0600 Subject: [PATCH 4/7] remove typo --- src/lib/actions/SignInRedirect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/actions/SignInRedirect.js b/src/lib/actions/SignInRedirect.js index a648117d897fb..cfd1a8ad91db1 100644 --- a/src/lib/actions/SignInRedirect.js +++ b/src/lib/actions/SignInRedirect.js @@ -1,7 +1,7 @@ import Ion from '../Ion'; import IONKEYS from '../../IONKEYS'; import ROUTES from '../../ROUTES'; -import {redirect} from './App.js'; +import {redirect} from './App'; /** * Redirects to the sign in page and handles adding any exitTo params to the URL. From ddb5423d5285ba3232793ea204553fc3fc15b53d Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Tue, 8 Sep 2020 15:03:17 -0600 Subject: [PATCH 5/7] Pass currentRedirectTo and check for string before checking first character --- src/Expensify.js | 3 +-- src/lib/actions/App.js | 15 +++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Expensify.js b/src/Expensify.js index e555f98343799..4d414a598d60f 100644 --- a/src/Expensify.js +++ b/src/Expensify.js @@ -41,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 = { @@ -86,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} - + recordCurrentRoute(data.match, this.props.redirectTo)} /> diff --git a/src/lib/actions/App.js b/src/lib/actions/App.js index 706a9e0151ef9..152721fccf11b 100644 --- a/src/lib/actions/App.js +++ b/src/lib/actions/App.js @@ -5,17 +5,24 @@ import IONKEYS from '../../IONKEYS'; * 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} params.match + * @param {object} match + * @param {string} currentRedirectTo */ -function recordCurrentRoute({match}) { +function recordCurrentRoute(match, currentRedirectTo) { Ion.set(IONKEYS.CURRENT_URL, match.url); - if (match.url === this.props.redirectTo) { + if (match.url === currentRedirectTo) { Ion.set(IONKEYS.APP_REDIRECT_TO, ''); } } + +/** + * Redirect the app to a new page by updating the state in Ion + * + * @param url + */ function redirect(url) { - const formattedURL = url.charAt(0) === '/' ? url : `/${url}`; + const formattedURL = (typeof url === 'string' && url.startsWith('/')) ? url : `/${url}`; Ion.set(IONKEYS.APP_REDIRECT_TO, formattedURL); } From 0fcc30abb4a5583c4b9a922acc94f87247b9c5d6 Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Tue, 8 Sep 2020 15:06:12 -0600 Subject: [PATCH 6/7] Style --- src/lib/actions/App.js | 4 ++-- src/lib/actions/Report.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/actions/App.js b/src/lib/actions/App.js index 152721fccf11b..3fa779a1bb517 100644 --- a/src/lib/actions/App.js +++ b/src/lib/actions/App.js @@ -10,7 +10,7 @@ import IONKEYS from '../../IONKEYS'; */ function recordCurrentRoute(match, currentRedirectTo) { Ion.set(IONKEYS.CURRENT_URL, match.url); - if (match.url === currentRedirectTo) { + if (match.url === currentRedirectTo) { Ion.set(IONKEYS.APP_REDIRECT_TO, ''); } } @@ -19,7 +19,7 @@ function recordCurrentRoute(match, currentRedirectTo) { /** * Redirect the app to a new page by updating the state in Ion * - * @param url + * @param {mixed} url */ function redirect(url) { const formattedURL = (typeof url === 'string' && url.startsWith('/')) ? url : `/${url}`; diff --git a/src/lib/actions/Report.js b/src/lib/actions/Report.js index d1274c538160f..79a2cc9ba4d49 100644 --- a/src/lib/actions/Report.js +++ b/src/lib/actions/Report.js @@ -10,7 +10,7 @@ import promiseAllSettled from '../promiseAllSettled'; import ExpensiMark from '../ExpensiMark'; import Notification from '../Notification'; import * as PersonalDetails from './PersonalDetails'; -import {redirect} from './App.js' +import {redirect} from './App'; // List of reportIDs that we define in .env const configReportIDs = CONFIG.REPORT_IDS.split(',').map(Number); From 7181a657871eda5648873cf8d1feb29a34d2bb06 Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Tue, 8 Sep 2020 17:46:57 -0600 Subject: [PATCH 7/7] Connect to Ion correctly --- src/Expensify.js | 2 +- src/lib/actions/App.js | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Expensify.js b/src/Expensify.js index 4d414a598d60f..0de40845ed703 100644 --- a/src/Expensify.js +++ b/src/Expensify.js @@ -85,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} - recordCurrentRoute(data.match, this.props.redirectTo)} /> + diff --git a/src/lib/actions/App.js b/src/lib/actions/App.js index 3fa779a1bb517..f9d591d4dc747 100644 --- a/src/lib/actions/App.js +++ b/src/lib/actions/App.js @@ -1,14 +1,19 @@ 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 - * @param {string} currentRedirectTo */ -function recordCurrentRoute(match, currentRedirectTo) { +function recordCurrentRoute({match}) { Ion.set(IONKEYS.CURRENT_URL, match.url); if (match.url === currentRedirectTo) { Ion.set(IONKEYS.APP_REDIRECT_TO, '');