diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 9436c8692210..b3b8ec9a39f7 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -45,29 +45,24 @@ Onyx.connect({ }, }); -let policyIDList = []; +let allPolicies = []; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY, waitForCollectionCallback: true, - callback: (policies) => { - policyIDList = _.compact(_.pluck(policies, 'id')); - }, + callback: policies => allPolicies = policies, }); -// When we reconnect the app, we don't want to fetch workspaces created optimistically while offline since they don't exist yet in the back-end. -// If we fetched them then they'd return as empty objects which would clear out the optimistic policy values initially created locally. -// Once the re-queued call to CreateWorkspace returns, the full contents of the workspace excluded here should be correctly saved into Onyx. -let policyIDListExcludingWorkspacesCreatedOffline = []; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.POLICY, - waitForCollectionCallback: true, - callback: (policies) => { - const policiesExcludingWorkspacesCreatedOffline = _.reject(policies, - policy => lodashGet(policy, 'pendingAction', null) === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD - && lodashGet(policy, 'type', null) === CONST.POLICY.TYPE.FREE); - policyIDListExcludingWorkspacesCreatedOffline = _.compact(_.pluck(policiesExcludingWorkspacesCreatedOffline, 'id')); - }, -}); +/** + * @param {Array} policies + * @return {Array} array of policy ids +*/ +function getNonOptimisticPolicyIDs(policies) { + return _.chain(policies) + .reject(policy => lodashGet(policy, 'pendingAction', null) === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) + .pluck('id') + .compact() + .value(); +} /** * @param {String} locale @@ -116,28 +111,36 @@ AppState.addEventListener('change', (nextAppState) => { * Fetches data needed for app initialization */ function openApp() { - API.read('OpenApp', {policyIDList}, { - optimisticData: [ - { - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: ONYXKEYS.IS_LOADING_REPORT_DATA, - value: true, - }, - ], - successData: [ - { - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: ONYXKEYS.IS_LOADING_REPORT_DATA, - value: false, - }, - ], - failureData: [ - { - onyxMethod: CONST.ONYX.METHOD.MERGE, - key: ONYXKEYS.IS_LOADING_REPORT_DATA, - value: false, - }, - ], + // We need a fresh connection/callback here to make sure that the list of policyIDs that is sent to OpenApp is the most updated list from Onyx + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY, + waitForCollectionCallback: true, + callback: (policies) => { + Onyx.disconnect(connectionID); + API.read('OpenApp', {policyIDList: getNonOptimisticPolicyIDs(policies)}, { + optimisticData: [ + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: ONYXKEYS.IS_LOADING_REPORT_DATA, + value: true, + }, + ], + successData: [ + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: ONYXKEYS.IS_LOADING_REPORT_DATA, + value: false, + }, + ], + failureData: [ + { + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: ONYXKEYS.IS_LOADING_REPORT_DATA, + value: false, + }, + ], + }); + }, }); } @@ -145,7 +148,7 @@ function openApp() { * Refreshes data when the app reconnects */ function reconnectApp() { - API.write('ReconnectApp', {policyIDListExcludingWorkspacesCreatedOffline}, { + API.write('ReconnectApp', {policyIDList: getNonOptimisticPolicyIDs(allPolicies)}, { optimisticData: [{ onyxMethod: CONST.ONYX.METHOD.MERGE, key: ONYXKEYS.IS_LOADING_REPORT_DATA,