From 49878f195b3b39f4c806f9870165e7b67500af2c Mon Sep 17 00:00:00 2001 From: Rory Abraham Date: Fri, 8 Oct 2021 10:16:38 -0700 Subject: [PATCH] Load policy summaries first, then full policies --- src/libs/API.js | 12 ++++++++++++ src/libs/actions/Policy.js | 39 +++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/libs/API.js b/src/libs/API.js index 2933b6fef98..b87711ef3ef 100644 --- a/src/libs/API.js +++ b/src/libs/API.js @@ -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} */ @@ -1093,6 +1104,7 @@ export { GetShortLivedAuthToken, GetIOUReport, GetPolicyList, + GetPolicySummaryList, GetReportSummaryList, GetRequestCountryCode, Graphite_Timer, diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 7e406843214..3b5284a95b6 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -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} 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 @@ -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) { @@ -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); } }); }