Skip to content

Commit

Permalink
Load policy summaries first, then full policies
Browse files Browse the repository at this point in the history
  • Loading branch information
roryabraham committed Oct 8, 2021
1 parent 7846d77 commit 49878f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,17 @@ function GetPolicyList() {
return Network.post(commandName, parameters);
}

/**
* @returns {Promise}
*/
function GetPolicySummaryList() {
const commandName = 'Get';
const parameters = {
returnValueList: 'policySummaryList',
};
return Network.post(commandName, parameters);
}

/**
* @returns {Promise}
*/
Expand Down Expand Up @@ -1093,6 +1104,7 @@ export {
GetShortLivedAuthToken,
GetIOUReport,
GetPolicyList,
GetPolicySummaryList,
GetReportSummaryList,
GetRequestCountryCode,
Graphite_Timer,
Expand Down
39 changes: 32 additions & 7 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,22 @@ function getSimplifiedPolicyObject(fullPolicy) {
name: fullPolicy.name,
role: fullPolicy.role,
type: fullPolicy.type,
employeeList: getSimplifiedEmployeeList(fullPolicy.value.employeeList),
employeeList: getSimplifiedEmployeeList(lodashGet(fullPolicy, 'value.employeeList'));
avatarURL: lodashGet(fullPolicy, 'value.avatarURL', ''),
};
}

/**
* @param {Array<Object>} policyList
* @returns {Object}
*/
function transformPolicyListToOnyxCollection(policyList) {
return _.reduce(policyList, (memo, policy) => ({
...memo,
[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: getSimplifiedPolicyObject(policy),
}), {});
}

/**
* Used to update ALL of the policies at once. If a policy is present locally, but not in the policies object passed here it will be removed.
* @param {Object} policyCollection - object of policy key and partial policy object
Expand Down Expand Up @@ -121,6 +132,15 @@ function create(name = '', shouldAutomaticallyReroute = true) {
/**
* 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(shouldCreateNewPolicy = false) {
Expand All @@ -131,20 +151,25 @@ function getPolicyList(shouldCreateNewPolicy = false) {
createPolicyPromise
.then((policyID) => {
newPolicyID = policyID;
return API.GetPolicyList();
return API.GetPolicySummaryList();
})
.then((data) => {
if (data.jsonCode === 200) {
const policyDataToStore = _.reduce(data.policyList, (memo, policy) => ({
...memo,
[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: getSimplifiedPolicyObject(policy),
}), {});
const policyDataToStore = transformPolicyListToOnyxCollection(data.policySummaryList || []);
updateAllPolicies(policyDataToStore);
}

if (shouldCreateNewPolicy) {
Navigation.dismissModal();
Navigation.navigate(ROUTES.getWorkspaceCardRoute(newPolicyID));
Navigation.navigate(newPolicyID ? ROUTES.getWorkspaceCardRoute(newPolicyID) : ROUTES.HOME);
}

return API.GetPolicyList();
})
.then((data) => {
if (data.jsonCode === 200) {
const policyDataToStore = transformPolicyListToOnyxCollection(data.policyList || []);
updateAllPolicies(policyDataToStore);
}
});
}
Expand Down

0 comments on commit 49878f1

Please sign in to comment.