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

Allow retryable optimistic workspace creation #11130

Merged
merged 11 commits into from
Sep 21, 2022
17 changes: 16 additions & 1 deletion src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -124,7 +139,7 @@ function openApp() {
* Refreshes data when the app reconnects
*/
function reconnectApp() {
API.read('ReconnectApp', {policyIDList}, {
API.read('ReconnectApp', {policyIDListExcludingWorkspacesCreatedOffline}, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter expected in the Web API is policyIDList, not policyIDListExcludingWorkspacesCreatedOffline 😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll correct this in this PR btw: #12129

optimisticData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
Expand Down
25 changes: 11 additions & 14 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
policyID,
announceChatReportID,
adminsChatReportID,
Expand All @@ -822,7 +819,7 @@ function createWorkspace() {
},
{
optimisticData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
onyxMethod: CONST.ONYX.METHOD.SET,
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
id: policyID,
Expand All @@ -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]: {
Expand All @@ -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,
}],
Expand Down Expand Up @@ -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) {
Expand Down