-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemoFlowService.js
More file actions
216 lines (191 loc) · 6.03 KB
/
demoFlowService.js
File metadata and controls
216 lines (191 loc) · 6.03 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'use strict';
require('app')
.factory('demoFlowService', demoFlowService);
function demoFlowService(
$http,
$localStorage,
$q,
$rootScope,
currentOrg,
defaultContainerUrl,
errs,
featureFlags,
fetchGitHubRepoBranch,
github,
keypather,
patchOrgMetadata,
promisify,
setUiState
) {
if (isInDemoFlow()) {
addEventListeners();
}
function resetFlags () {
deleteItem('hasSeenHangTightMessage');
deleteItem('hasSeenUrlCallout');
deleteItem('hasSeenAddBranchCTA');
deleteItem('hasAddedBranch');
deleteItem('clickedPrLink');
}
/**
* This should only be called in the FF popover
*/
function internalResetFlags() {
setUiState('demo.explanationUi', false);
}
function setItem (key, value) {
$localStorage[currentOrg.poppa.attrs.id + '-' + key] = value;
}
function getItem (key) {
return $localStorage[currentOrg.poppa.attrs.id + '-' + key];
}
function deleteItem (key) {
delete $localStorage[currentOrg.poppa.attrs.id + '-' + key];
}
function isInDemoFlow () {
return !keypather.get(currentOrg, 'poppa.attrs.metadata.hasCompletedDemo');
}
function endDemoFlow () {
return $q.when()
.then(function () {
if (isInDemoFlow()) {
return patchOrgMetadata(currentOrg.poppa.id(), {
metadata: {
hasAha: false,
hasCompletedDemo: true,
hasConfirmedSetup: true
}
})
.then(function (updatedOrg) {
resetFlags();
currentOrg.poppa.attrs.metadata = updatedOrg.metadata;
});
}
});
}
function addEventListeners () {
// listen to pr link clicking
$rootScope.$on('demo::prLinkClicked', function () {
setItem('clickedPrLink', true);
});
// listen for the url open event if the user hasn't done it yet
if (featureFlags.flags.demoAutoAddBranch && !$localStorage.hasSeenUrlCallout) {
var unregisterContainerUrlClickListener = $rootScope.$on('clickedOpenContainerUrl', function (event, instance) {
unregisterContainerUrlClickListener();
forkNewInstance(instance)
.then(function () {
if (currentOrg.isPersonalAccount()) {
submitDemoPR(instance)
.catch(function (err) {
if (keypather.get(err, 'errors[0].message').match(/(pull request.*exists)/)) {
return instance;
}
errs.handler(err);
});
}
});
});
}
// listen for the click on a new instance if the user has clicked open url
if (featureFlags.flags.demoAutoAddBranch && getItem('hasSeenUrlCallout') && !getItem('hasSeenAddBranchCTA')) {
addBranchListener();
}
}
function addBranchListener () {
var unregisterStateChangeListener = $rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
var instanceName = keypather.get(toParams, 'instanceName');
if (instanceName && instanceName.match(/dark-theme/)) {
setItem('hasSeenAddBranchCTA', true);
hasAddedBranch(true);
unregisterStateChangeListener();
}
});
}
function checkStatusOnInstance (instance) {
// This is needed to fix an issue with 'Response for preflight has invalid HTTP status code 404'
// Caused by the X-CSRF-TOKEN
var url = defaultContainerUrl(instance, true);
return $http({
method: 'GET',
url: url,
headers: {
'X-CSRF-TOKEN': undefined
}
})
.then(function (res) {
return res.status >= 200 && res.status < 300;
})
.catch(function () {
return true;
});
}
function forkNewInstance (instance) {
var acv = instance.contextVersion.getMainAppCodeVersion();
var fullReponame = acv.attrs.repo.split('/');
var orgName = fullReponame[0];
var repoName = fullReponame[1];
addBranchListener();
return fetchGitHubRepoBranch(orgName, repoName, 'dark-theme')
.then(function (branch) {
var sha = branch.commit.sha;
var branchName = branch.name;
return promisify(instance, 'fork')(branchName, sha);
});
}
function hasSeenHangTightMessage () {
return getItem('hasSeenHangTightMessage');
}
function submitDemoPR (instance) {
var repoOwner = keypather.get(instance, 'attrs.owner.username');
var repoName = instance.getRepoName();
return github.createPR(repoOwner, repoName, 'master', 'dark-theme');
}
function hasSeenUrlCallout () {
return getItem('hasSeenUrlCallout');
}
function hasAddedBranch (value) {
if (value !== undefined) {
setItem('hasAddedBranch', value);
}
return getItem('hasAddedBranch');
}
$rootScope.$on('demo::dismissUrlCallout', function ($event, instanceId) {
if (!hasSeenUrlCallout()) {
setItem('hasSeenUrlCallout', instanceId);
}
});
$rootScope.$on('demo::end', function () {
return endDemoFlow();
});
function shouldAddPR () {
return currentOrg.isPersonalAccount();
}
function shouldShowTeamCTA () {
return currentOrg.isPersonalAccount() && !!getItem('clickedPrLink');
}
function shouldShowServicesCTA () {
return !currentOrg.isPersonalAccount() && isInDemoFlow() && hasAddedBranch();
}
function shouldShowAddBranchCTA (instance) {
return isInDemoFlow() && !getItem('hasSeenAddBranchCTA') && instance.attrs.id === getItem('hasSeenUrlCallout');
}
return {
addBranchListener: addBranchListener,
checkStatusOnInstance: checkStatusOnInstance,
deleteItem: deleteItem,
endDemoFlow: endDemoFlow,
getItem: getItem,
hasAddedBranch: hasAddedBranch,
hasSeenHangTightMessage: hasSeenHangTightMessage,
hasSeenUrlCallout: hasSeenUrlCallout,
shouldAddPR: shouldAddPR,
isInDemoFlow: isInDemoFlow,
resetFlags: resetFlags,
internalResetFlags: internalResetFlags,
setItem: setItem,
submitDemoPR: submitDemoPR,
shouldShowAddBranchCTA: shouldShowAddBranchCTA,
shouldShowTeamCTA: shouldShowTeamCTA,
shouldShowServicesCTA: shouldShowServicesCTA
};
}