Skip to content

Commit

Permalink
Merge pull request #6080 from Expensify/marcaaron-fixUpPolicyMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
roryabraham authored Nov 4, 2021
2 parents fa83e4c + 51440af commit fcd1dc2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 43 deletions.
30 changes: 21 additions & 9 deletions src/libs/Navigation/AppNavigator/AuthScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Navigation from '../Navigation';
import * as User from '../../actions/User';
import {setModalVisibility} from '../../actions/Modal';
import NameValuePair from '../../actions/NameValuePair';
import {getPolicyList} from '../../actions/Policy';
import * as Policy from '../../actions/Policy';
import modalCardStyleInterpolator from './modalCardStyleInterpolator';
import createCustomModalStackNavigator from './createCustomModalStackNavigator';
import getOperatingSystem from '../../getOperatingSystem';
Expand Down Expand Up @@ -169,15 +169,12 @@ class AuthScreens extends React.Component {
// Load policies, maybe creating a new policy first.
Linking.getInitialURL()
.then((url) => {
// url is null on mobile unless the app was opened via a deeplink
if (url) {
const path = new URL(url).pathname;
const exitTo = new URLSearchParams(url).get('exitTo');
const shouldCreateFreePolicy = Str.startsWith(path, Str.normalizeUrl(ROUTES.LOGIN_WITH_SHORT_LIVED_TOKEN)) && exitTo === ROUTES.WORKSPACE_NEW;
getPolicyList(shouldCreateFreePolicy);
} else {
getPolicyList(false);
if (this.shouldCreateFreePolicy(url)) {
Policy.createAndGetPolicyList();
return;
}

Policy.getPolicyList();
});

// Refresh the personal details, timezone and betas every 30 minutes
Expand Down Expand Up @@ -229,6 +226,21 @@ class AuthScreens extends React.Component {
this.interval = null;
}

/**
* @param {String} [url]
* @returns {Boolean}
*/
shouldCreateFreePolicy(url = '') {
if (!url) {
return false;
}

const path = new URL(url).pathname;
const exitTo = new URLSearchParams(url).get('exitTo');
return Str.startsWith(path, Str.normalizeUrl(ROUTES.LOGIN_WITH_SHORT_LIVED_TOKEN))
&& exitTo === ROUTES.WORKSPACE_NEW;
}

render() {
const commonModalScreenOptions = {
headerShown: false,
Expand Down
72 changes: 42 additions & 30 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import lodashMerge from 'lodash/merge';
import lodashGet from 'lodash/get';
import * as API from '../API';
import ONYXKEYS from '../../ONYXKEYS';
Expand Down Expand Up @@ -102,7 +103,7 @@ function updateAllPolicies(policyCollection) {
* @param {Boolean} [shouldAutomaticallyReroute]
* @returns {Promise}
*/
function create(name = '', shouldAutomaticallyReroute = true) {
function create(name = '') {
let res = null;
return API.Policy_Create({type: CONST.POLICY.TYPE.FREE, policyName: name})
.then((response) => {
Expand All @@ -123,14 +124,22 @@ function create(name = '', shouldAutomaticallyReroute = true) {
role: CONST.POLICY.ROLE.ADMIN,
outputCurrency: response.policy.outputCurrency,
});
}).then(() => {
const policyID = lodashGet(res, 'policyID');
if (shouldAutomaticallyReroute) {
Navigation.dismissModal();
Navigation.navigate(policyID ? ROUTES.getWorkspaceInitialRoute(policyID) : ROUTES.HOME);
}
return Promise.resolve(policyID);
});
}).then(() => Promise.resolve(lodashGet(res, 'policyID')));
}

/**
* @param {String} policyID
*/
function navigateToPolicy(policyID) {
Navigation.dismissModal();
Navigation.navigate(policyID ? ROUTES.getWorkspaceInitialRoute(policyID) : ROUTES.HOME);
}

/**
* @param {String} [name]
*/
function createAndNavigate(name = '') {
create(name).then(navigateToPolicy);
}

/**
Expand All @@ -144,40 +153,41 @@ function create(name = '', shouldAutomaticallyReroute = true) {
*
* 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) {
let newPolicyID;
const createPolicyPromise = shouldCreateNewPolicy
? create('', false)
: Promise.resolve();
createPolicyPromise
.then((policyID) => {
newPolicyID = policyID;
return API.GetPolicySummaryList();
})
function getPolicyList() {
const policyCollection = {};
API.GetPolicySummaryList()
.then((data) => {
if (data.jsonCode === 200) {
const policyDataToStore = transformPolicyListToOnyxCollection(data.policySummaryList || []);
updateAllPolicies(policyDataToStore);
}

if (shouldCreateNewPolicy) {
Navigation.dismissModal();
Navigation.navigate(newPolicyID ? ROUTES.getWorkspaceInitialRoute(newPolicyID) : ROUTES.HOME);
lodashMerge(policyCollection, transformPolicyListToOnyxCollection(data.policySummaryList || []));
}

return API.GetPolicyList();
})
.then((data) => {
if (data.jsonCode === 200) {
const policyDataToStore = transformPolicyListToOnyxCollection(data.policyList || []);
updateAllPolicies(policyDataToStore);
lodashMerge(policyCollection, transformPolicyListToOnyxCollection(data.policyList || []));
}
})
.finally(() => {
if (_.isEmpty(policyCollection)) {
return;
}

updateAllPolicies(policyCollection);
});
}

function createAndGetPolicyList() {
let newPolicyID;
create()
.then((policyID) => {
newPolicyID = policyID;
return getPolicyList();
})
.then(() => navigateToPolicy(newPolicyID));
}

/**
* Is the user an admin of a free policy (aka workspace)?
*
Expand Down Expand Up @@ -364,4 +374,6 @@ export {
update,
setWorkspaceErrors,
hideWorkspaceAlertMessage,
createAndNavigate,
createAndGetPolicyList,
};
8 changes: 4 additions & 4 deletions src/pages/home/sidebar/SidebarScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from '../../../components/Icon/Expensicons';
import Permissions from '../../../libs/Permissions';
import ONYXKEYS from '../../../ONYXKEYS';
import {create, isAdminOfFreePolicy} from '../../../libs/actions/Policy';
import * as Policy from '../../../libs/actions/Policy';
import Performance from '../../../libs/Performance';
import NameValuePair from '../../../libs/actions/NameValuePair';

Expand Down Expand Up @@ -76,7 +76,7 @@ class SidebarScreen extends Component {

// It's also possible that we already have a workspace policy. In either case we will not toggle the menu but do still want to set the NVP in this case since the user does
// not need to create a workspace.
if (!isAdminOfFreePolicy(this.props.allPolicies) && !isDisplayingWorkspaceRoute) {
if (!Policy.isAdminOfFreePolicy(this.props.allPolicies) && !isDisplayingWorkspaceRoute) {
this.toggleCreateMenu();
}

Expand Down Expand Up @@ -183,14 +183,14 @@ class SidebarScreen extends Component {
onSelected: () => Navigation.navigate(ROUTES.IOU_BILL),
},
] : []),
...(Permissions.canUseFreePlan(this.props.betas) && !isAdminOfFreePolicy(this.props.allPolicies) ? [
...(Permissions.canUseFreePlan(this.props.betas) && !Policy.isAdminOfFreePolicy(this.props.allPolicies) ? [
{
icon: NewWorkspace,
iconWidth: 46,
iconHeight: 40,
text: this.props.translate('workspace.new.newWorkspace'),
description: this.props.translate('workspace.new.getTheExpensifyCardAndMore'),
onSelected: () => create(),
onSelected: () => Policy.createAndNavigate(),
},
] : []),
]}
Expand Down

0 comments on commit fcd1dc2

Please sign in to comment.