Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor get PolicySummaryList in App #9906

Merged
merged 8 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
});
}

/**
* 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