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 all 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
85 changes: 44 additions & 41 deletions src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>} 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
Expand Down Expand Up @@ -116,36 +111,44 @@ 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);
marcaaron marked this conversation as resolved.
Show resolved Hide resolved
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,
},
],
});
},
});
}

/**
* 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,
Expand Down