Skip to content

Commit

Permalink
Merge pull request #13029 from Expensify/vit-experiementWithStagingNa…
Browse files Browse the repository at this point in the history
…tive

Enable native staging apps to use Staging API
  • Loading branch information
marcochavezf authored Dec 5, 2022
2 parents 2e69dac + 7c53f31 commit 36a009b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/components/TestToolMenu.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import styles from '../styles/styles';
Expand All @@ -14,6 +15,8 @@ import TestToolRow from './TestToolRow';
import networkPropTypes from './networkPropTypes';
import compose from '../libs/compose';
import {withNetwork} from './OnyxProvider';
import getPlatform from '../libs/getPlatform';
import CONST from '../CONST';

const propTypes = {
/** User object in Onyx */
Expand Down Expand Up @@ -42,7 +45,7 @@ const TestToolMenu = props => (
This enables QA and internal testers to take advantage of sandbox environments for 3rd party services like Plaid and Onfido. */}
<TestToolRow title="Use Staging Server">
<Switch
isOn={lodashGet(props, 'user.shouldUseStagingServer', true)}
isOn={lodashGet(props, 'user.shouldUseStagingServer', _.contains([CONST.PLATFORM.WEB, CONST.PLATFORM.DESKTOP], getPlatform()))}
onToggle={() => User.setShouldUseStagingServer(!lodashGet(props, 'user.shouldUseStagingServer', true))}
/>
</TestToolRow>
Expand Down
10 changes: 7 additions & 3 deletions src/libs/HttpUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import CONFIG from '../CONFIG';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';
import HttpsError from './Errors/HttpsError';
import shouldUseStagingServer from './shouldUseStagingServer';
import getPlatform from './getPlatform';

let shouldUseStagingServer = false;
// Desktop and web use staging config too so we we should default to staging API endpoint if on those platforms
const shouldDefaultToStaging = _.contains([CONST.PLATFORM.WEB, CONST.PLATFORM.DESKTOP], getPlatform());
let stagingServerToggleState = false;
Onyx.connect({
key: ONYXKEYS.USER,
callback: val => shouldUseStagingServer = lodashGet(val, 'shouldUseStagingServer', true),
callback: val => stagingServerToggleState = lodashGet(val, 'shouldUseStagingServer', shouldDefaultToStaging),
});

let shouldFailAllRequests = false;
Expand Down Expand Up @@ -105,7 +109,7 @@ function xhr(command, data, type = CONST.NETWORK.METHOD.POST, shouldUseSecure =

let apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.URL_API_ROOT;

if (CONFIG.IS_IN_STAGING && shouldUseStagingServer) {
if (shouldUseStagingServer(stagingServerToggleState)) {
apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.STAGING_SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.STAGING_EXPENSIFY_URL;
}

Expand Down
13 changes: 13 additions & 0 deletions src/libs/shouldUseStagingServer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import CONFIG from '../../CONFIG';

/**
* Helper method used to decide which API endpoint to call
*
* @param {Boolean} stagingServerToggleState
* @returns {Boolean}
*/
function shouldUseStagingServer(stagingServerToggleState) {
return CONFIG.IS_IN_STAGING && stagingServerToggleState;
}

export default shouldUseStagingServer;
13 changes: 13 additions & 0 deletions src/libs/shouldUseStagingServer/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Environment from '../Environment/Environment';

/**
* Helper method used to decide which API endpoint to call in the Native apps.
* We build the staging native apps with production env config so we cannot rely on those values,
* hence we will decide solely on the value of the shouldUseStagingServer value (always false in production).
*
* @param {Boolean} stagingServerToggleState
* @returns {Boolean}
*/
export default function shouldUseStagingServer(stagingServerToggleState) {
return !Environment.isDevelopment() && stagingServerToggleState;
}

0 comments on commit 36a009b

Please sign in to comment.