Skip to content

Commit

Permalink
Merge pull request #12129 from Expensify/aldo_fix-open-app-missing-lo…
Browse files Browse the repository at this point in the history
…cal-policy-ids

Fix removal of deleted workspaces from the client
  • Loading branch information
aldo-expensify authored Nov 17, 2022
2 parents f82f72c + bcd6dce commit 33915a5
Showing 1 changed file with 44 additions and 41 deletions.
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);
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

0 comments on commit 33915a5

Please sign in to comment.