diff --git a/src/ONYXKEYS.js b/src/ONYXKEYS.js index 03119bd6a65b..13b64d521e4f 100755 --- a/src/ONYXKEYS.js +++ b/src/ONYXKEYS.js @@ -81,6 +81,7 @@ export default { REPORT_ACTIONS_DRAFTS: 'reportActionsDrafts_', REPORT_USER_IS_TYPING: 'reportUserIsTyping_', REPORT_IOUS: 'reportIOUs_', + POLICY: 'policy_', }, // Indicates which locale should be used diff --git a/src/libs/API.js b/src/libs/API.js index 8b2d283050c1..f6c56c3936aa 100644 --- a/src/libs/API.js +++ b/src/libs/API.js @@ -405,6 +405,17 @@ function GetIOUReport(parameters) { return Network.post(commandName, parameters); } +/** + * @returns {Promise} + */ +function GetPolicySummaryList() { + const commandName = 'Get'; + const parameters = { + returnValueList: 'policySummaryList', + }; + return Network.post(commandName, parameters); +} + /** * @returns {Promise} */ @@ -808,6 +819,7 @@ export { Get, GetAccountStatus, GetIOUReport, + GetPolicySummaryList, GetRequestCountryCode, Graphite_Timer, Log, diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 8ab7eb203911..5c59b384e353 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -27,6 +27,7 @@ import Navigation from '../Navigation'; import * as User from '../../actions/User'; import {setModalVisibility} from '../../actions/Modal'; import NameValuePair from '../../actions/NameValuePair'; +import {getPolicySummaries} from '../../actions/Policy'; import modalCardStyleInterpolator from './modalCardStyleInterpolator'; import createCustomModalStackNavigator from './createCustomModalStackNavigator'; @@ -122,6 +123,7 @@ class AuthScreens extends React.Component { fetchAllReports(true, true, true); fetchCountryCodeByRequestIP(); UnreadIndicatorUpdater.listenForReportChanges(); + getPolicySummaries(); // Refresh the personal details, timezone and betas every 30 minutes // There is no pusher event that sends updated personal details data yet diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js new file mode 100644 index 000000000000..af04011c3c94 --- /dev/null +++ b/src/libs/actions/Policy.js @@ -0,0 +1,36 @@ +import _ from 'underscore'; +import Onyx from 'react-native-onyx'; +import {GetPolicySummaryList} from '../API'; +import ONYXKEYS from '../../ONYXKEYS'; + +/** + * Takes a full policy summary that is returned from the policySummaryList and simplifies it so we are only storing + * the pieces of data that we need to in Onyx + * + * @param {Object} fullPolicy + * @param {String} fullPolicy.name + * @returns {Object} + */ +function getSimplifiedPolicyObject(fullPolicy) { + return { + name: fullPolicy.name, + }; +} + +function getPolicySummaries() { + GetPolicySummaryList() + .then((data) => { + if (data.jsonCode === 200) { + const policyDataToStore = _.reduce(data.policySummaryList, (memo, policy) => ({ + ...memo, + [`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`]: getSimplifiedPolicyObject(policy), + }), {}); + Onyx.mergeCollection(ONYXKEYS.COLLECTION.POLICY, policyDataToStore); + } + }); +} + +export { + // eslint-disable-next-line import/prefer-default-export + getPolicySummaries, +};