diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 2e2a493ed468f..6e98afa085f1a 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -54,6 +54,21 @@ Onyx.connect({ }, }); +// 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 {String} locale */ @@ -124,7 +139,7 @@ function openApp() { * Refreshes data when the app reconnects */ function reconnectApp() { - API.read('ReconnectApp', {policyIDList}, { + API.read('ReconnectApp', {policyIDListExcludingWorkspacesCreatedOffline}, { optimisticData: [{ onyxMethod: CONST.ONYX.METHOD.MERGE, key: ONYXKEYS.IS_LOADING_REPORT_DATA, diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index a707a0ac9a851..5de94702631d6 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -809,10 +809,7 @@ function createWorkspace() { expenseReportActionData, } = Report.buildOptimisticWorkspaceChats(policyID, workspaceName); - // We need to use makeRequestWithSideEffects as we try to redirect to the policy right after creation - // The policy hasn't been merged in Onyx data at this point, leading to an intermittent Not Found screen - // eslint-disable-next-line rulesdir/no-api-side-effects-method - API.makeRequestWithSideEffects('CreateWorkspace', { + API.write('CreateWorkspace', { policyID, announceChatReportID, adminsChatReportID, @@ -822,7 +819,7 @@ function createWorkspace() { }, { optimisticData: [{ - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { id: policyID, @@ -835,7 +832,7 @@ function createWorkspace() { }, }, { - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST}${policyID}`, value: { [sessionEmail]: { @@ -845,32 +842,32 @@ function createWorkspace() { }, }, { - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${announceChatReportID}`, value: announceChatData, }, { - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceChatReportID}`, value: announceReportActionData, }, { - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${adminsChatReportID}`, value: adminsChatData, }, { - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${adminsChatReportID}`, value: adminsReportActionData, }, { - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${expenseChatReportID}`, value: expenseChatData, }, { - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportID}`, value: expenseReportActionData, }], @@ -961,9 +958,9 @@ function createWorkspace() { key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseChatReportID}`, value: null, }], - }).then(() => { - Navigation.navigate(ROUTES.getWorkspaceInitialRoute(policyID)); }); + + Navigation.navigate(ROUTES.getWorkspaceInitialRoute(policyID)); } function openWorkspaceReimburseView(policyID) {