From eba3320ddf6911adae71e013a244ec4d71baae76 Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Mon, 19 Sep 2022 18:18:46 -0700 Subject: [PATCH 01/10] create workspaces with API.write --- src/libs/actions/Policy.js | 6 +++--- src/pages/workspace/withFullPolicy.js | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 5fac77a56380..0f071cbab92c 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -816,7 +816,7 @@ function createWorkspace() { // 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, @@ -965,9 +965,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) { diff --git a/src/pages/workspace/withFullPolicy.js b/src/pages/workspace/withFullPolicy.js index 45f608d685b2..83f078eab74d 100644 --- a/src/pages/workspace/withFullPolicy.js +++ b/src/pages/workspace/withFullPolicy.js @@ -106,10 +106,13 @@ export default function (WrappedComponent) { const isFromFullPolicy = lodashGet(props, 'policy.isFromFullPolicy', false) || lodashGet(props, `policy.policy_${policyID}.isFromFullPolicy`, false); if (_.isString(policyID) && !_.isEmpty(policyID) && (!isFromFullPolicy || !isPreviousRouteInSameWorkspace(currentRoute.name, policyID))) { + console.log(">>>> loading full policy"); Policy.loadFullPolicy(policyID); Policy.updateLastAccessedWorkspace(policyID); } + console.log(">>>> withFullPolicy policy", props.policy); + previousRouteName = currentRoute.name; previousRoutePolicyID = policyID; From abb9b1dea63e4d5563cc04a34d6356f6b02d434c Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Mon, 19 Sep 2022 18:22:01 -0700 Subject: [PATCH 02/10] remove debug statements --- src/pages/workspace/withFullPolicy.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/pages/workspace/withFullPolicy.js b/src/pages/workspace/withFullPolicy.js index 83f078eab74d..45f608d685b2 100644 --- a/src/pages/workspace/withFullPolicy.js +++ b/src/pages/workspace/withFullPolicy.js @@ -106,13 +106,10 @@ export default function (WrappedComponent) { const isFromFullPolicy = lodashGet(props, 'policy.isFromFullPolicy', false) || lodashGet(props, `policy.policy_${policyID}.isFromFullPolicy`, false); if (_.isString(policyID) && !_.isEmpty(policyID) && (!isFromFullPolicy || !isPreviousRouteInSameWorkspace(currentRoute.name, policyID))) { - console.log(">>>> loading full policy"); Policy.loadFullPolicy(policyID); Policy.updateLastAccessedWorkspace(policyID); } - console.log(">>>> withFullPolicy policy", props.policy); - previousRouteName = currentRoute.name; previousRoutePolicyID = policyID; From 5645f57b755983b29ec8fe517befd229d64a5a6f Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Tue, 20 Sep 2022 11:54:48 -0700 Subject: [PATCH 03/10] use policyIDListExcludingWorkspacesWithPendingActions --- src/libs/actions/App.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 2e2a493ed468..5268c67ca24d 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -54,6 +54,20 @@ 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 are correctly saved into Onyx. +let policyIDListExcludingWorkspacesWithPendingActions = []; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY, + waitForCollectionCallback: true, + callback: (policies) => { + const policiesExcludingWorkspacesWithPendingActions = _.reject(policies, policy => lodashGet(policy, 'pendingAction', null) + && lodashGet(policy, 'type', null) === CONST.POLICY.TYPE.FREE); + policyIDListExcludingWorkspacesWithPendingActions = _.compact(_.pluck(policiesExcludingWorkspacesWithPendingActions, 'id')); + }, +}); + /** * @param {String} locale */ @@ -124,7 +138,7 @@ function openApp() { * Refreshes data when the app reconnects */ function reconnectApp() { - API.read('ReconnectApp', {policyIDList}, { + API.read('ReconnectApp', {policyIDListExcludingWorkspacesWithPendingActions}, { optimisticData: [{ onyxMethod: CONST.ONYX.METHOD.MERGE, key: ONYXKEYS.IS_LOADING_REPORT_DATA, From 521366b2185e2be658d4bb883f3c3144a6afe338 Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Tue, 20 Sep 2022 12:06:37 -0700 Subject: [PATCH 04/10] Update App.js --- src/libs/actions/App.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 5268c67ca24d..775aafe60ec6 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -56,7 +56,7 @@ 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 are correctly saved into Onyx. +// Once the re-queued call updating the excluded workspace returns, its contents should be correctly saved into Onyx. let policyIDListExcludingWorkspacesWithPendingActions = []; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY, From 7844d5c0d8a876cab223c711556eb1e24e7a9067 Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Tue, 20 Sep 2022 12:09:02 -0700 Subject: [PATCH 05/10] only exclude workspaces with pending action = add --- src/libs/actions/App.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 775aafe60ec6..f5115cb2c6f0 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -56,13 +56,14 @@ 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 updating the excluded workspace returns, its contents should be correctly saved into Onyx. +// Once the re-queued call to CreateWorkspace returns, the contents of the workspace excluded here should be correctly saved into Onyx. let policyIDListExcludingWorkspacesWithPendingActions = []; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY, waitForCollectionCallback: true, callback: (policies) => { - const policiesExcludingWorkspacesWithPendingActions = _.reject(policies, policy => lodashGet(policy, 'pendingAction', null) + const policiesExcludingWorkspacesWithPendingActions = _.reject(policies, + policy => lodashGet(policy, 'pendingAction', null) === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD && lodashGet(policy, 'type', null) === CONST.POLICY.TYPE.FREE); policyIDListExcludingWorkspacesWithPendingActions = _.compact(_.pluck(policiesExcludingWorkspacesWithPendingActions, 'id')); }, From b1c2db2facbe0fc15de6f12f5fbb40a23e0820a3 Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Tue, 20 Sep 2022 12:13:14 -0700 Subject: [PATCH 06/10] rename to policyIDListExcludingWorkspacesCreatedOffline --- src/libs/actions/App.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index f5115cb2c6f0..91503516fc50 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -57,15 +57,15 @@ 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 contents of the workspace excluded here should be correctly saved into Onyx. -let policyIDListExcludingWorkspacesWithPendingActions = []; +let policyIDListExcludingWorkspacesCreatedOffline = []; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY, waitForCollectionCallback: true, callback: (policies) => { - const policiesExcludingWorkspacesWithPendingActions = _.reject(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); - policyIDListExcludingWorkspacesWithPendingActions = _.compact(_.pluck(policiesExcludingWorkspacesWithPendingActions, 'id')); + policyIDListExcludingWorkspacesCreatedOffline = _.compact(_.pluck(policiesExcludingWorkspacesCreatedOffline, 'id')); }, }); @@ -139,7 +139,7 @@ function openApp() { * Refreshes data when the app reconnects */ function reconnectApp() { - API.read('ReconnectApp', {policyIDListExcludingWorkspacesWithPendingActions}, { + API.read('ReconnectApp', {policyIDListExcludingWorkspacesCreatedOffline}, { optimisticData: [{ onyxMethod: CONST.ONYX.METHOD.MERGE, key: ONYXKEYS.IS_LOADING_REPORT_DATA, From 3dedd09b15eaf0524593f31476e898fc9ea67879 Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Tue, 20 Sep 2022 12:14:41 -0700 Subject: [PATCH 07/10] remove unused comment --- src/libs/actions/Policy.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 50fd83712692..9e84c7be9d69 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -809,9 +809,6 @@ 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.write('CreateWorkspace', { policyID, announceChatReportID, From a7ad213e073dd6ba2125818119876e281826ec39 Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Tue, 20 Sep 2022 12:15:31 -0700 Subject: [PATCH 08/10] Update comment --- src/libs/actions/App.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 91503516fc50..6e98afa085f1 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -56,7 +56,7 @@ 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 contents of the workspace excluded here should be correctly saved into Onyx. +// 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, From ffd1c81a5443a0b9b409b92da7de0121e67182e9 Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Tue, 20 Sep 2022 12:45:18 -0700 Subject: [PATCH 09/10] use set instead of merge --- src/libs/actions/Policy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 9e84c7be9d69..5a1f14c1fe35 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -819,7 +819,7 @@ function createWorkspace() { }, { optimisticData: [{ - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { id: policyID, From dcc100202df876ec9a2c752b07cb93b66843a4cd Mon Sep 17 00:00:00 2001 From: Jasper Huang Date: Tue, 20 Sep 2022 15:42:54 -0700 Subject: [PATCH 10/10] use set instead of merge for all optimistic data --- src/libs/actions/Policy.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 5a1f14c1fe35..5de94702631d 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -832,7 +832,7 @@ function createWorkspace() { }, }, { - onyxMethod: CONST.ONYX.METHOD.MERGE, + onyxMethod: CONST.ONYX.METHOD.SET, key: `${ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST}${policyID}`, value: { [sessionEmail]: { @@ -842,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, }],