Skip to content

Commit

Permalink
Merge pull request #9906 from Expensify/cmartins-refactorPolicyList
Browse files Browse the repository at this point in the history
Refactor get PolicySummaryList in App
  • Loading branch information
luacmartins authored Jul 20, 2022
2 parents 036b36c + 8b8137e commit 6b1116e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/libs/Navigation/AppNavigator/AuthScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -338,5 +338,8 @@ export default compose(
session: {
key: ONYXKEYS.SESSION,
},
allPolicies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
}),
)(AuthScreens);
51 changes: 37 additions & 14 deletions src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -85,36 +98,48 @@ 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(),
Report.fetchAllReports(true, 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),
});
}

/**
Expand All @@ -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();
});
}

Expand Down Expand Up @@ -159,7 +184,6 @@ function setUpPoliciesAndNavigate(session) {
Linking.getInitialURL()
.then((url) => {
if (!url) {
Policy.getPolicyList();
return;
}
const path = new URL(url).pathname;
Expand All @@ -173,7 +197,6 @@ function setUpPoliciesAndNavigate(session) {
Policy.createAndGetPolicyList();
return;
}
Policy.getPolicyList();
if (!isLoggingInAsNewUser && exitTo) {
Navigation.isNavigationReady()
.then(() => {
Expand All @@ -187,7 +210,7 @@ function setUpPoliciesAndNavigate(session) {

// When the app reconnects from being offline, fetch all initialization data
NetworkConnection.onReconnect(() => {
getAppData(true);
getAppData();
reconnectApp();
});

Expand Down

0 comments on commit 6b1116e

Please sign in to comment.