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

Fix removal of deleted workspaces from the client #12129

Merged
merged 6 commits into from
Nov 17, 2022
Merged
Changes from 3 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
116 changes: 62 additions & 54 deletions src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ Onyx.connect({
},
});

let policyIDList = [];
let allPolicies = [];
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (policies) => {
policyIDList = _.compact(_.pluck(policies, 'id'));
allPolicies = policies;
},
aldo-expensify marked this conversation as resolved.
Show resolved Hide resolved
});

// 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'));
},
});
/**
* 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure that this description makes sense to me. When I read this:

If we fetched them then they'd return as empty objects

The first thing I think is... why don't we just update the API to not return them as empty objects?

Once the re-queued call to CreateWorkspace returns

I'm not really sure what this means. What does "re-queue" mean in this context and why does it happen for CreateWorkspace? What does "returns" mean (return is a statement at the end of a method)?

A lot of this comment is assuming a lot about network requests, and other things happening in Onyx, yet the code inside the method has nothing to do with any of that. The code inside the method is just filtering policies. I would suggest simply renaming this method to getNonOptimisticPolicyIDs and drop the method description entirely. With a name like that, it's clear what the method does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure that this description makes sense to me. When I read this:

If we fetched them then they'd return as empty objects

The first thing I think is... why don't we just update the API to not return them as empty objects?

Yeah, the comment seems to be wrong about this, the API doesn't return them as "empty object" it creates Onyx updates setting them to null. This code: https://github.com/Expensify/Web-Expensify/blob/383a1bff0220bf2b795715f371ad443dc634283c/lib/AppInit.php#L87-L103

Once the re-queued call to CreateWorkspace returns

I'm not really sure what this means. What does "re-queue" mean in this context and why does it happen for CreateWorkspace? What does "returns" mean (return is a statement at the end of a method)?

A lot of this comment is assuming a lot about network requests, and other things happening in Onyx, yet the code inside the method has nothing to do with any of that. The code inside the method is just filtering policies. I would suggest simply renaming this method to getNonOptimisticPolicyIDs and drop the method description entirely. With a name like that, it's clear what the method does.

Agreed!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropped the long description

*
* @param {Array} policies
* @return {Array<String>} array of policy ids
*/
function getPolicyIDListExcludingWorkspacesCreatedOffline(policies) {
aldo-expensify marked this conversation as resolved.
Show resolved Hide resolved
const policiesExcludingWorkspacesCreatedOffline = _.reject(policies,
policy => lodashGet(policy, 'pendingAction', null) === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD
&& lodashGet(policy, 'type', null) === CONST.POLICY.TYPE.FREE);
aldo-expensify marked this conversation as resolved.
Show resolved Hide resolved
return _.compact(_.pluck(policiesExcludingWorkspacesCreatedOffline, 'id'));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored in a function that can be reused


/**
* @param {String} locale
Expand Down Expand Up @@ -116,51 +116,59 @@ 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,
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_INITIAL_APP_DATA,
value: true,
},
],
successData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: false,
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_INITIAL_APP_DATA,
value: false,
},
],
failureData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: false,
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_INITIAL_APP_DATA,
value: false,
},
],
// We need a fresh connection/callback here to make sure that we have loaded the policies from Onyx before sending the OpenApp request
aldo-expensify marked this conversation as resolved.
Show resolved Hide resolved
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (policies) => {
Onyx.disconnect(connectionID);
marcaaron marked this conversation as resolved.
Show resolved Hide resolved
API.read('OpenApp', {policyIDList: getPolicyIDListExcludingWorkspacesCreatedOffline(policies)}, {
optimisticData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: true,
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_INITIAL_APP_DATA,
value: true,
},
],
successData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: false,
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_INITIAL_APP_DATA,
value: false,
},
],
failureData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
value: false,
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_INITIAL_APP_DATA,
value: false,
},
],
});
},
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks like a lot of changes, but it is just being wrapped by Onyx.connect({

}

/**
* Refreshes data when the app reconnects
*/
function reconnectApp() {
API.write('ReconnectApp', {policyIDListExcludingWorkspacesCreatedOffline}, {
API.write('ReconnectApp', {policyIDList: getPolicyIDListExcludingWorkspacesCreatedOffline(allPolicies)}, {
optimisticData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
Expand Down