diff --git a/src/ONYXKEYS.js b/src/ONYXKEYS.js index 0bb4ed4ea3f4..e3ec98b6052e 100755 --- a/src/ONYXKEYS.js +++ b/src/ONYXKEYS.js @@ -170,6 +170,9 @@ export default { // Is report data loading? IS_LOADING_REPORT_DATA: 'isLoadingReportData', + // Is policy data loading? + IS_LOADING_POLICY_DATA: 'isLoadingPolicyData', + // Are we loading the create policy room command IS_LOADING_CREATE_POLICY_ROOM: 'isLoadingCratePolicyRoom', diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index eba39252f6ce..4f9137355a6f 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -210,9 +210,11 @@ function deletePolicy(policyID) { * and we also don't have to wait for full policies to load before navigating to the new policy. */ function getPolicyList() { + Onyx.set(ONYXKEYS.IS_LOADING_POLICY_DATA, true); API.GetPolicySummaryList() .then((data) => { if (data.jsonCode !== 200) { + Onyx.set(ONYXKEYS.IS_LOADING_POLICY_DATA, false); return; } @@ -224,6 +226,8 @@ function getPolicyList() { if (!_.isEmpty(policyCollection)) { updateAllPolicies(policyCollection); } + + Onyx.set(ONYXKEYS.IS_LOADING_POLICY_DATA, false); }); } diff --git a/src/libs/actions/Session/index.js b/src/libs/actions/Session/index.js index 8d21d8ff73d5..ddccec8d71bd 100644 --- a/src/libs/actions/Session/index.js +++ b/src/libs/actions/Session/index.js @@ -20,6 +20,7 @@ import NetworkConnection from '../../NetworkConnection'; import * as User from '../User'; import * as ValidationUtils from '../../ValidationUtils'; import * as Authentication from '../../Authentication'; +import * as Welcome from '../Welcome'; let credentials = {}; Onyx.connect({ @@ -347,6 +348,7 @@ function cleanupSession() { PushNotification.clearNotifications(); Pusher.disconnect(); Timers.clearAll(); + Welcome.resetReadyCheck(); } function clearAccountMessages() { diff --git a/src/libs/actions/WelcomeActions.js b/src/libs/actions/Welcome.js similarity index 60% rename from src/libs/actions/WelcomeActions.js rename to src/libs/actions/Welcome.js index c67fab49f2e7..58935bcf54bd 100644 --- a/src/libs/actions/WelcomeActions.js +++ b/src/libs/actions/Welcome.js @@ -8,17 +8,60 @@ import * as Policy from './Policy'; import ONYXKEYS from '../../ONYXKEYS'; import NameValuePair from './NameValuePair'; import CONST from '../../CONST'; +import createOnReadyTask from '../createOnReadyTask'; + +const readyTask = createOnReadyTask(); + +let isFirstTimeNewExpensifyUser; +let isLoadingReportData = true; +let isLoadingPolicyData = true; + +/** + * Check that a few requests have completed so that the welcome action can proceed: + * + * - Whether we are a first time new expensify user + * - Whether we have loaded all policies the server knows about + * - Whether we have loaded all reports the server knows about + */ +function checkOnReady() { + if (!_.isBoolean(isFirstTimeNewExpensifyUser) || isLoadingPolicyData || isLoadingReportData) { + return; + } + + readyTask.setIsReady(); +} -/* Flag for new users used to show welcome actions on first load */ -let isFirstTimeNewExpensifyUser = false; Onyx.connect({ key: ONYXKEYS.NVP_IS_FIRST_TIME_NEW_EXPENSIFY_USER, - callback: val => isFirstTimeNewExpensifyUser = val, + initWithStoredValues: false, + callback: (val) => { + isFirstTimeNewExpensifyUser = val; + checkOnReady(); + }, +}); + +Onyx.connect({ + key: ONYXKEYS.IS_LOADING_REPORT_DATA, + initWithStoredValues: false, + callback: (val) => { + isLoadingReportData = val; + checkOnReady(); + }, +}); + +Onyx.connect({ + key: ONYXKEYS.IS_LOADING_POLICY_DATA, + initWithStoredValues: false, + callback: (val) => { + isLoadingPolicyData = val; + checkOnReady(); + }, }); const allReports = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, + initWithStoredValues: false, callback: (val, key) => { if (!val || !key) { return; @@ -45,12 +88,10 @@ Onyx.connect({ * * @param {Object} params * @param {Object} params.routes - * @param {Function} params.hideCreateMenu + * @param {Function} params.showCreateMenu */ -function show({routes, hideCreateMenu}) { - // NOTE: This setTimeout is required due to a bug in react-navigation where modals do not display properly in a drawerContent - // This is a short-term workaround, see this issue for updates on a long-term solution: https://github.com/Expensify/App/issues/5296 - setTimeout(() => { +function show({routes, showCreateMenu}) { + readyTask.isReady().then(() => { if (!isFirstTimeNewExpensifyUser) { return; } @@ -72,15 +113,19 @@ function show({routes, hideCreateMenu}) { const exitingToWorkspaceRoute = lodashGet(loginWithShortLivedTokenRoute, 'params.exitTo', '') === 'workspace/new'; const isDisplayingWorkspaceRoute = topRouteName.toLowerCase().includes('workspace') || exitingToWorkspaceRoute; - // It's also possible that we already have a workspace policy. In either case we will not hide the menu but do still want to set the NVP in this case - // since the user does not need to create a workspace. + // If user is not already an admin of a free policy and we are not navigating them to their workspace or creating a new workspace via workspace/new then + // we will show the create menu. if (!Policy.isAdminOfFreePolicy(allPolicies) && !isDisplayingWorkspaceRoute) { - hideCreateMenu(); + showCreateMenu(); } - }, 1500); + }); +} + +function resetReadyCheck() { + readyTask.reset(); } export { - // eslint-disable-next-line import/prefer-default-export show, + resetReadyCheck, }; diff --git a/src/pages/home/sidebar/SidebarScreen.js b/src/pages/home/sidebar/SidebarScreen.js index 158ba45a26ac..c2f55f3d1826 100755 --- a/src/pages/home/sidebar/SidebarScreen.js +++ b/src/pages/home/sidebar/SidebarScreen.js @@ -22,7 +22,7 @@ import Permissions from '../../../libs/Permissions'; import ONYXKEYS from '../../../ONYXKEYS'; import * as Policy from '../../../libs/actions/Policy'; import Performance from '../../../libs/Performance'; -import * as WelcomeAction from '../../../libs/actions/WelcomeActions'; +import * as Welcome from '../../../libs/actions/Welcome'; const propTypes = { /* Beta features list */ @@ -58,7 +58,7 @@ class SidebarScreen extends Component { Timing.start(CONST.TIMING.SIDEBAR_LOADED, true); const routes = lodashGet(this.props.navigation.getState(), 'routes', []); - WelcomeAction.show({routes, hideCreateMenu: this.hideCreateMenu}); + Welcome.show({routes, showCreateMenu: this.showCreateMenu}); } /**