-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Remove error when all workspace is deleted #29781
Changes from 1 commit
d36a762
1dff6dd
165308b
b8a97da
9f606ed
9b24175
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,12 @@ Onyx.connect({ | |
callback: (val) => (allPersonalDetails = val), | ||
}); | ||
|
||
let reimbursementAccount; | ||
Onyx.connect({ | ||
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, | ||
callback: (val) => (reimbursementAccount = val), | ||
}); | ||
|
||
let allRecentlyUsedCategories = {}; | ||
Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES, | ||
|
@@ -81,6 +87,36 @@ function updateLastAccessedWorkspace(policyID) { | |
Onyx.set(ONYXKEYS.LAST_ACCESSED_WORKSPACE_POLICY_ID, policyID); | ||
} | ||
|
||
/** | ||
* Check if the user has any active free policies (aka workspaces) | ||
* | ||
* @param {Array} policies | ||
* @returns {Boolean} | ||
*/ | ||
function hasActiveFreePolicy(policies) { | ||
const adminFreePolicies = _.filter(policies, (policy) => policy && policy.type === CONST.POLICY.TYPE.FREE && policy.role === CONST.POLICY.ROLE.ADMIN); | ||
|
||
if (adminFreePolicies.length === 0) { | ||
return false; | ||
} | ||
|
||
if (_.some(adminFreePolicies, (policy) => !policy.pendingAction)) { | ||
return true; | ||
} | ||
|
||
if (_.some(adminFreePolicies, (policy) => policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD)) { | ||
return true; | ||
} | ||
|
||
if (_.some(adminFreePolicies, (policy) => policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)) { | ||
return false; | ||
} | ||
|
||
// If there are no add or delete pending actions the only option left is an update | ||
// pendingAction, in which case we should return true. | ||
return true; | ||
} | ||
|
||
/** | ||
* Delete the workspace | ||
* | ||
|
@@ -89,6 +125,10 @@ function updateLastAccessedWorkspace(policyID) { | |
* @param {String} policyName | ||
*/ | ||
function deleteWorkspace(policyID, reports, policyName) { | ||
const filteredPolicies = _.filter(allPolicies, (policy) => policy.id !== policyID); | ||
const hasActivePolicy = hasActiveFreePolicy(filteredPolicies); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this var is only used 1 place, we can call it directly in the place it used to avoid headaches in naming |
||
const oldReimbursementAccount = reimbursementAccount; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to assign to a temporary var There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need it to store the old value. The current value can be changed after the optimisticData is merged in Onyx. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, but we already built There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you're right. Updated. |
||
|
||
const optimisticData = [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
|
@@ -125,6 +165,18 @@ function deleteWorkspace(policyID, reports, policyName) { | |
value: optimisticReportActions, | ||
}; | ||
}), | ||
|
||
...(!hasActivePolicy | ||
? [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, | ||
value: { | ||
errors: null, | ||
}, | ||
}, | ||
] | ||
: []), | ||
]; | ||
|
||
// Restore the old report stateNum and statusNum | ||
|
@@ -139,6 +191,11 @@ function deleteWorkspace(policyID, reports, policyName) { | |
oldPolicyName, | ||
}, | ||
})), | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT, | ||
value: oldReimbursementAccount, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should restore the |
||
}, | ||
]; | ||
|
||
// We don't need success data since the push notification will update | ||
|
@@ -162,36 +219,6 @@ function isAdminOfFreePolicy(policies) { | |
return _.some(policies, (policy) => policy && policy.type === CONST.POLICY.TYPE.FREE && policy.role === CONST.POLICY.ROLE.ADMIN); | ||
} | ||
|
||
/** | ||
* Check if the user has any active free policies (aka workspaces) | ||
* | ||
* @param {Array} policies | ||
* @returns {Boolean} | ||
*/ | ||
function hasActiveFreePolicy(policies) { | ||
const adminFreePolicies = _.filter(policies, (policy) => policy && policy.type === CONST.POLICY.TYPE.FREE && policy.role === CONST.POLICY.ROLE.ADMIN); | ||
|
||
if (adminFreePolicies.length === 0) { | ||
return false; | ||
} | ||
|
||
if (_.some(adminFreePolicies, (policy) => !policy.pendingAction)) { | ||
return true; | ||
} | ||
|
||
if (_.some(adminFreePolicies, (policy) => policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD)) { | ||
return true; | ||
} | ||
|
||
if (_.some(adminFreePolicies, (policy) => policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)) { | ||
return false; | ||
} | ||
|
||
// If there are no add or delete pending actions the only option left is an update | ||
// pendingAction, in which case we should return true. | ||
return true; | ||
} | ||
|
||
/** | ||
* Remove the passed members from the policy employeeList | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use lodash instead of underscore for the new code.