-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add shouldCreateNewPolicy param to getPolicySummaries action #5706
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1725ba0
Add shouldCreateNewPolicy param to getPolicySummaries action
roryabraham 6ea4750
Get rid of policySummaryList API call, fix race condition
roryabraham 27a0d69
Fix authentication race condition in LogInWithShortLivedTokenPage
roryabraham 12a2930
Deprecate WorkspaceNew page
roryabraham da3ea68
Use Linking.getInitialURL instead of Navigation.getCurrentURL
roryabraham b4c0f71
Remove console log
roryabraham 8327635
Cleanup per review comments
roryabraham cf625d1
Safe-access policyID
roryabraham 7846d77
Merge branch 'main' into Rory-FixLoadingSpinnerRaceCondition
roryabraham 49878f1
Load policy summaries first, then full policies
roryabraham 3fec339
Fix semicolon typo
roryabraham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,32 @@ Onyx.connect({ | |
}); | ||
|
||
/** | ||
* Takes a full policy summary that is returned from the policySummaryList and simplifies it so we are only storing | ||
* Simplifies the employeeList response into an object containing an array of emails | ||
* | ||
* @param {Object} employeeList | ||
* @returns {Array} | ||
*/ | ||
function getSimplifiedEmployeeList(employeeList) { | ||
const employeeListEmails = _.chain(employeeList) | ||
.pluck('email') | ||
.flatten() | ||
.unique() | ||
.value(); | ||
|
||
return employeeListEmails; | ||
} | ||
|
||
/** | ||
* Takes a full policy that is returned from the policyList and simplifies it so we are only storing | ||
* the pieces of data that we need to in Onyx | ||
* | ||
* @param {Object} fullPolicy | ||
* @param {String} fullPolicy.id | ||
* @param {String} fullPolicy.name | ||
* @param {String} fullPolicy.role | ||
* @param {String} fullPolicy.type | ||
* @param {Object} fullPolicy.value.employeeList | ||
* @param {String} [fullPolicy.value.avatarURL] | ||
* @returns {Object} | ||
*/ | ||
function getSimplifiedPolicyObject(fullPolicy) { | ||
|
@@ -38,23 +56,20 @@ function getSimplifiedPolicyObject(fullPolicy) { | |
name: fullPolicy.name, | ||
role: fullPolicy.role, | ||
type: fullPolicy.type, | ||
employeeList: getSimplifiedEmployeeList(lodashGet(fullPolicy, 'value.employeeList')), | ||
avatarURL: lodashGet(fullPolicy, 'value.avatarURL', ''), | ||
}; | ||
} | ||
|
||
/** | ||
* Simplifies the employeeList response into an object containing an array of emails | ||
* | ||
* @param {Object} employeeList | ||
* @returns {Array} | ||
* @param {Array<Object>} policyList | ||
* @returns {Object} | ||
*/ | ||
function getSimplifiedEmployeeList(employeeList) { | ||
const employeeListEmails = _.chain(employeeList) | ||
.pluck('email') | ||
.flatten() | ||
.unique() | ||
.value(); | ||
|
||
return employeeListEmails; | ||
function transformPolicyListToOnyxCollection(policyList) { | ||
return _.reduce(policyList, (memo, policy) => ({ | ||
...memo, | ||
[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: getSimplifiedPolicyObject(policy), | ||
}), {}); | ||
} | ||
|
||
/** | ||
|
@@ -78,35 +93,82 @@ function updateAllPolicies(policyCollection) { | |
} | ||
|
||
/** | ||
* Fetches the policySummaryList from the API and saves a simplified version in Onyx | ||
* Merges the passed in login into the specified policy | ||
* | ||
* @param {String} [name] | ||
* @param {Boolean} [shouldAutomaticallyReroute] | ||
* @returns {Promise} | ||
*/ | ||
function getPolicySummaries() { | ||
API.GetPolicySummaryList() | ||
.then((data) => { | ||
if (data.jsonCode === 200) { | ||
const policyDataToStore = _.reduce(data.policySummaryList, (memo, policy) => ({ | ||
...memo, | ||
[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: getSimplifiedPolicyObject(policy), | ||
}), {}); | ||
updateAllPolicies(policyDataToStore); | ||
function create(name = '', shouldAutomaticallyReroute = true) { | ||
let res = null; | ||
return API.Policy_Create({type: CONST.POLICY.TYPE.FREE, policyName: name}) | ||
.then((response) => { | ||
if (response.jsonCode !== 200) { | ||
// Show the user feedback | ||
const errorMessage = translateLocal('workspace.new.genericFailureMessage'); | ||
Growl.error(errorMessage, 5000); | ||
return; | ||
} | ||
res = response; | ||
|
||
// We are awaiting this merge so that we can guarantee our policy is available to any React components connected to the policies collection before we navigate to a new route. | ||
return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${response.policyID}`, { | ||
employeeList: getSimplifiedEmployeeList(response.policy.employeeList), | ||
id: response.policyID, | ||
type: response.policy.type, | ||
name: response.policy.name, | ||
role: CONST.POLICY.ROLE.ADMIN, | ||
}); | ||
}).then(() => { | ||
const policyID = lodashGet(res, 'policyID'); | ||
if (shouldAutomaticallyReroute) { | ||
Navigation.dismissModal(); | ||
Navigation.navigate(policyID ? ROUTES.getWorkspaceCardRoute(policyID) : ROUTES.HOME); | ||
} | ||
return Promise.resolve(policyID); | ||
}); | ||
} | ||
|
||
/** | ||
* Fetches the policyList from the API and saves a simplified version in Onyx | ||
* Fetches policy list from the API and saves a simplified version in Onyx, optionally creating a new policy first. | ||
* | ||
* More specifically, this action will: | ||
* 1. Optionally create a new policy. | ||
* 2. Fetch policy summaries. | ||
* 3. Optionally navigate to the new policy. | ||
* 4. Then fetch full policies. | ||
* | ||
* This way, we ensure that there's no race condition between creating the new policy and fetching existing ones, | ||
* and we also don't have to wait for full policies to load before navigating to the new policy. | ||
* | ||
* @param {Boolean} [shouldCreateNewPolicy] | ||
*/ | ||
function getPolicyList() { | ||
API.GetPolicyList() | ||
function getPolicyList(shouldCreateNewPolicy = false) { | ||
let newPolicyID; | ||
const createPolicyPromise = shouldCreateNewPolicy | ||
? create('', false) | ||
: Promise.resolve(); | ||
createPolicyPromise | ||
.then((policyID) => { | ||
newPolicyID = policyID; | ||
return API.GetPolicySummaryList(); | ||
}) | ||
.then((data) => { | ||
if (data.jsonCode === 200) { | ||
const policyDataToStore = transformPolicyListToOnyxCollection(data.policySummaryList || []); | ||
updateAllPolicies(policyDataToStore); | ||
} | ||
|
||
if (shouldCreateNewPolicy) { | ||
Navigation.dismissModal(); | ||
Navigation.navigate(newPolicyID ? ROUTES.getWorkspaceCardRoute(newPolicyID) : ROUTES.HOME); | ||
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. NAB, might be good to leave a comment about why we need to navigate to the |
||
} | ||
|
||
return API.GetPolicyList(); | ||
}) | ||
.then((data) => { | ||
if (data.jsonCode === 200) { | ||
const policyDataToStore = _.reduce(data.policyList, (memo, policy) => ({ | ||
...memo, | ||
[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: { | ||
employeeList: getSimplifiedEmployeeList(policy.value.employeeList), | ||
avatarURL: lodashGet(policy, 'value.avatarURL', ''), | ||
}, | ||
}), {}); | ||
const policyDataToStore = transformPolicyListToOnyxCollection(data.policyList || []); | ||
updateAllPolicies(policyDataToStore); | ||
} | ||
}); | ||
|
@@ -212,39 +274,6 @@ function invite(logins, welcomeNote, policyID) { | |
}); | ||
} | ||
|
||
/** | ||
* Merges the passed in login into the specified policy | ||
* | ||
* @param {String} [name] | ||
*/ | ||
function create(name = '') { | ||
let res = null; | ||
API.Policy_Create({type: CONST.POLICY.TYPE.FREE, policyName: name}) | ||
.then((response) => { | ||
if (response.jsonCode !== 200) { | ||
// Show the user feedback | ||
const errorMessage = translateLocal('workspace.new.genericFailureMessage'); | ||
Growl.error(errorMessage, 5000); | ||
return; | ||
} | ||
res = response; | ||
|
||
// We are awaiting this merge so that we can guarantee our policy is available to any React components connected to the policies collection before we navigate to a new route. | ||
return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${response.policyID}`, { | ||
employeeList: getSimplifiedEmployeeList(response.policy.employeeList), | ||
id: response.policyID, | ||
type: response.policy.type, | ||
name: response.policy.name, | ||
role: CONST.POLICY.ROLE.ADMIN, | ||
}); | ||
}).then(() => { | ||
if (res) { | ||
Navigation.dismissModal(); | ||
Navigation.navigate(ROUTES.getWorkspaceCardRoute(res.policyID)); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param {Object} file | ||
* @returns {Promise} | ||
|
@@ -314,7 +343,6 @@ function hideWorkspaceAlertMessage(policyID) { | |
} | ||
|
||
export { | ||
getPolicySummaries, | ||
getPolicyList, | ||
removeMembers, | ||
invite, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
NAB, this makes it sound like there is another option to navigate to a created policy. But what actually happens is... if the policy is created we navigate to it.