diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 13920d1c6dd4..57c961b30a79 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -117,8 +117,8 @@ class AuthScreens extends React.Component { // Listen for report changes and fetch some data we need on initialization UnreadIndicatorUpdater.listenForReportChanges(); - App.getAppData(false); - App.openApp(); + App.getAppData(); + App.openApp(this.props.allPolicies); App.fixAccountAndReloadData(); App.setUpPoliciesAndNavigate(this.props.session); @@ -338,5 +338,8 @@ export default compose( session: { key: ONYXKEYS.SESSION, }, + allPolicies: { + key: ONYXKEYS.COLLECTION.POLICY, + }, }), )(AuthScreens); diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index d5eafab203ba..a2bc02d9b8bd 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -2,6 +2,7 @@ import {AppState, Linking} from 'react-native'; import Onyx from 'react-native-onyx'; import lodashGet from 'lodash/get'; import Str from 'expensify-common/lib/str'; +import _ from 'underscore'; import * as API from '../API'; import ONYXKEYS from '../../ONYXKEYS'; import * as DeprecatedAPI from '../deprecatedAPI'; @@ -33,6 +34,18 @@ Onyx.connect({ initWithStoredValues: false, }); +const allPolicies = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY, + callback: (val, key) => { + if (!val || !key) { + return; + } + + allPolicies[key] = {...allPolicies[key], ...val}; + }, +}); + /** * @param {String} url */ @@ -85,17 +98,11 @@ AppState.addEventListener('change', (nextAppState) => { /** * Fetches data needed for app initialization - * - * @param {Boolean} shouldSyncPolicyList Should be false if the initial policy needs to be created. Otherwise, should be true. * @returns {Promise} */ -function getAppData(shouldSyncPolicyList = true) { +function getAppData() { BankAccounts.fetchUserWallet(); - if (shouldSyncPolicyList) { - Policy.getPolicyList(); - } - // We should update the syncing indicator when personal details and reports are both done fetching. return Promise.all([ PersonalDetails.fetchPersonalDetails(), @@ -103,18 +110,36 @@ function getAppData(shouldSyncPolicyList = true) { ]); } +/** + * Gets a comma separated list of locally stored policy ids + * + * @param {Array} policies + * @return {String} + */ +function getPolicyIDList(policies) { + return _.chain(policies) + .filter(Boolean) + .map(policy => policy.id) + .join(','); +} + /** * Fetches data needed for app initialization + * @param {Array} policies */ -function openApp() { - API.read('OpenApp'); +function openApp(policies) { + API.read('OpenApp', { + policyIDList: getPolicyIDList(policies), + }); } /** * Refreshes data when the app reconnects */ function reconnectApp() { - API.read('ReconnectApp'); + API.read('ReconnectApp', { + policyIDList: getPolicyIDList(allPolicies), + }); } /** @@ -128,7 +153,7 @@ function fixAccountAndReloadData() { return; } Log.info('FixAccount found updates for this user, so data will be reinitialized', true, response); - getAppData(false); + getAppData(); }); } @@ -159,7 +184,6 @@ function setUpPoliciesAndNavigate(session) { Linking.getInitialURL() .then((url) => { if (!url) { - Policy.getPolicyList(); return; } const path = new URL(url).pathname; @@ -173,7 +197,6 @@ function setUpPoliciesAndNavigate(session) { Policy.createAndGetPolicyList(); return; } - Policy.getPolicyList(); if (!isLoggingInAsNewUser && exitTo) { Navigation.isNavigationReady() .then(() => { @@ -187,7 +210,7 @@ function setUpPoliciesAndNavigate(session) { // When the app reconnects from being offline, fetch all initialization data NetworkConnection.onReconnect(() => { - getAppData(true); + getAppData(); reconnectApp(); });