-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathDemoActions.js
108 lines (98 loc) · 3.87 KB
/
DemoActions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import CONST from '../../CONST';
import * as API from '../API';
import * as ReportUtils from '../ReportUtils';
import Navigation from '../Navigation/Navigation';
import ROUTES from '../../ROUTES';
import ONYXKEYS from '../../ONYXKEYS';
import * as Localize from '../Localize';
/**
* @param {String} workspaceOwnerEmail email of the workspace owner
* @param {String} apiCommand
*/
function createDemoWorkspaceAndNavigate(workspaceOwnerEmail, apiCommand) {
// Try to navigate to existing demo workspace expense chat if it exists in Onyx
const demoWorkspaceChatReportID = ReportUtils.getPolicyExpenseChatReportIDByOwner(workspaceOwnerEmail);
if (demoWorkspaceChatReportID) {
// We must call goBack() to remove the demo route from nav history
Navigation.goBack();
Navigation.navigate(ROUTES.getReportRoute(demoWorkspaceChatReportID));
return;
}
// We use makeRequestWithSideEffects here because we need to get the workspace chat report ID to navigate to it after it's created
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects(apiCommand).then((response) => {
// Get report updates from Onyx response data
const reportUpdate = _.find(response.onyxData, ({key}) => key === ONYXKEYS.COLLECTION.REPORT);
if (!reportUpdate) {
// If there's no related onyx data, navigate the user home so they're not stuck.
Navigation.goBack();
Navigation.navigate(ROUTES.HOME);
return;
}
// Get the policy expense chat update
const policyExpenseChatReport = _.find(reportUpdate.value, ({chatType}) => chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT);
if (!policyExpenseChatReport) {
// If there's no related onyx data, navigate the user home so they're not stuck.
Navigation.goBack();
Navigation.navigate(ROUTES.HOME);
return;
}
// Navigate to the new policy expense chat report
// Note: We must call goBack() to remove the demo route from history
Navigation.goBack();
Navigation.navigate(ROUTES.getReportRoute(policyExpenseChatReport.reportID));
});
}
function runSbeDemo() {
createDemoWorkspaceAndNavigate(CONST.EMAIL.SBE, 'CreateSbeDemoWorkspace');
}
function runSaastrDemo() {
createDemoWorkspaceAndNavigate(CONST.EMAIL.SAASTR, 'CreateSaastrDemoWorkspace');
}
/**
* Runs code for specific demos, based on the provided URL
*
* @param {String} url - URL user is navigating to via deep link (or regular link in web)
*/
function runDemoByURL(url = '') {
const cleanUrl = (url || '').toLowerCase();
if (cleanUrl.endsWith(ROUTES.SAASTR)) {
Onyx.set(ONYXKEYS.DEMO_INFO, {
saastr: {
isBeginningDemo: true,
},
});
} else if (cleanUrl.endsWith(ROUTES.SBE)) {
Onyx.set(ONYXKEYS.DEMO_INFO, {
sbe: {
isBeginningDemo: true,
},
});
} else {
// No demo is being run, so clear out demo info
Onyx.set(ONYXKEYS.DEMO_INFO, null);
}
}
/**
* @param {Object} demoInfo
* @returns {Object}
*/
function getCustomTextForDemo(demoInfo = {}) {
if (lodashGet(demoInfo, 'saastr.isBeginningDemo')) {
return {
customHeadline: Localize.translateLocal('demos.saastr.signInWelcome'),
customHeroBody: Localize.translateLocal('demos.saastr.heroBody'),
};
}
if (lodashGet(demoInfo, 'sbe.isBeginningDemo')) {
return {
customHeadline: Localize.translateLocal('demos.sbe.signInWelcome'),
customHeroBody: Localize.translateLocal('demos.sbe.heroBody'),
};
}
return {};
}
export {runSaastrDemo, runSbeDemo, runDemoByURL, getCustomTextForDemo};