forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
396 lines (346 loc) · 12.3 KB
/
controller.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import config from '@automattic/calypso-config';
import { isEmpty } from 'lodash';
import page from 'page';
import { createElement } from 'react';
import store from 'store';
import { notFound } from 'calypso/controller';
import { recordPageView } from 'calypso/lib/analytics/page-view';
import { login } from 'calypso/lib/paths';
import { sectionify } from 'calypso/lib/route';
import flows from 'calypso/signup/config/flows';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import { updateDependencies } from 'calypso/state/signup/actions';
import { getSignupDependencyStore } from 'calypso/state/signup/dependency-store/selectors';
import { setCurrentFlowName, setPreviousFlowName } from 'calypso/state/signup/flow/actions';
import { getCurrentFlowName } from 'calypso/state/signup/flow/selectors';
import { getSignupProgress } from 'calypso/state/signup/progress/selectors';
import { requestSite } from 'calypso/state/sites/actions';
import { getSiteId } from 'calypso/state/sites/selectors';
import { setSelectedSiteId } from 'calypso/state/ui/actions';
import { setLayoutFocus } from 'calypso/state/ui/layout-focus/actions';
import { getStepComponent } from './config/step-components';
import SignupComponent from './main';
import {
retrieveSignupDestination,
clearSignupDestinationCookie,
getSignupCompleteFlowName,
wasSignupCheckoutPageUnloaded,
} from './storageUtils';
import {
getStepUrl,
canResumeFlow,
getFlowName,
getStepName,
getStepSectionName,
getValidPath,
getFlowPageTitle,
shouldForceLogin,
isReskinnedFlow,
} from './utils';
/**
* Constants
*/
const basePageTitle = 'Signup'; // used for analytics, doesn't require translation
/**
* Module variables
*/
let initialContext;
let previousFlowName;
const removeWhiteBackground = function () {
if ( ! document ) {
return;
}
document.body.classList.remove( 'is-white-signup' );
};
export const addVideoPressSignupClassName = () => {
if ( ! document ) {
return;
}
document.body.classList.add( 'is-videopress-signup' );
};
export const addVideoPressTvSignupClassName = () => {
if ( ! document ) {
return;
}
document.body.classList.add( 'is-videopress-tv-signup' );
};
export const addP2SignupClassName = () => {
if ( ! document ) {
return;
}
document.body.classList.add( 'is-p2-signup' );
};
export const removeP2SignupClassName = function () {
if ( ! document ) {
return;
}
document.body.classList.remove( 'is-p2-signup' );
};
export default {
redirectTests( context, next ) {
const isLoggedIn = isUserLoggedIn( context.store.getState() );
const currentFlowName = getFlowName( context.params, isLoggedIn );
if ( isReskinnedFlow( currentFlowName ) ) {
next();
} else if (
context.pathname.indexOf( 'domain' ) >= 0 ||
context.pathname.indexOf( 'plan' ) >= 0 ||
context.pathname.indexOf( 'onboarding-registrationless' ) >= 0 ||
context.pathname.indexOf( 'wpcc' ) >= 0 ||
context.pathname.indexOf( 'launch-only' ) >= 0 ||
context.params.flowName === 'account' ||
context.params.flowName === 'crowdsignal' ||
context.params.flowName === 'pressable-nux' ||
context.params.flowName === 'clone-site'
) {
removeWhiteBackground();
next();
} else if ( context.pathname.includes( 'p2' ) ) {
addP2SignupClassName();
removeWhiteBackground();
next();
} else if ( context.query.flow === 'videopress-tv' ) {
addVideoPressTvSignupClassName();
removeWhiteBackground();
next();
} else if ( context.pathname.includes( 'videopress' ) ) {
addVideoPressSignupClassName();
removeWhiteBackground();
next();
} else {
next();
return;
}
},
redirectWithoutLocaleIfLoggedIn( context, next ) {
const userLoggedIn = isUserLoggedIn( context.store.getState() );
if ( userLoggedIn && context.params.lang ) {
const flowName = getFlowName( context.params, userLoggedIn );
const stepName = getStepName( context.params );
const stepSectionName = getStepSectionName( context.params );
let urlWithoutLocale = getStepUrl( flowName, stepName, stepSectionName );
if ( config.isEnabled( 'wpcom-user-bootstrap' ) ) {
return page.redirect( urlWithoutLocale );
}
if ( ! isEmpty( context.query ) ) {
urlWithoutLocale += '?' + context.querystring;
}
if ( ! isEmpty( context.hash ) ) {
urlWithoutLocale += '#' + context.hashstring;
}
window.location = urlWithoutLocale;
return;
}
next();
},
saveInitialContext( context, next ) {
if ( ! initialContext ) {
initialContext = Object.assign( {}, context );
}
next();
},
redirectToFlow( context, next ) {
const userLoggedIn = isUserLoggedIn( context.store.getState() );
const flowName = getFlowName( context.params, userLoggedIn );
const localeFromParams = context.params.lang;
const localeFromStore = ! userLoggedIn ? store.get( 'signup-locale' ) : '';
const signupProgress = getSignupProgress( context.store.getState() );
// Special case for the user step which may use oauth2 redirect flow
// Check if there is a valid flow in progress to resume
// We're limited in the number of redirect uris we can provide so we only have a single one at /start/user
if ( context.params.flowName === 'user' ) {
const alternativeFlowName = getCurrentFlowName( context.store.getState() );
if (
alternativeFlowName &&
alternativeFlowName !== flowName &&
canResumeFlow( alternativeFlowName, signupProgress, userLoggedIn )
) {
window.location =
getStepUrl(
alternativeFlowName,
getStepName( context.params ),
getStepSectionName( context.params ),
localeFromStore
) +
( context.querystring ? '?' + context.querystring : '' ) +
( context.hashstring ? '#' + context.hashstring : '' );
return;
}
}
// Store the previous flow name (so we know from what flow we transitioned from).
if ( ! previousFlowName ) {
const persistedFlowName = getCurrentFlowName( context.store.getState() );
if ( persistedFlowName ) {
previousFlowName = persistedFlowName;
context.store.dispatch( setPreviousFlowName( previousFlowName ) );
}
}
context.store.dispatch( setCurrentFlowName( flowName ) );
if ( ! userLoggedIn && shouldForceLogin( flowName, userLoggedIn ) ) {
return page.redirect( login( { redirectTo: context.path } ) );
}
// if flow can be resumed, use saved locale
if (
! userLoggedIn &&
! localeFromParams &&
localeFromStore &&
canResumeFlow( flowName, signupProgress, userLoggedIn )
) {
window.location =
getStepUrl(
flowName,
getStepName( context.params ),
getStepSectionName( context.params ),
localeFromStore
) +
( context.querystring ? '?' + context.querystring : '' ) +
( context.hashstring ? '#' + context.hashstring : '' );
return;
}
if ( context.pathname !== getValidPath( context.params, userLoggedIn ) ) {
return page.redirect(
getValidPath( context.params, userLoggedIn ) +
( context.querystring ? '?' + context.querystring : '' )
);
}
store.set( 'signup-locale', localeFromParams );
next();
},
async start( context, next ) {
const userLoggedIn = isUserLoggedIn( context.store.getState() );
const basePath = sectionify( context.path );
const flowName = getFlowName( context.params, userLoggedIn );
const stepName = getStepName( context.params );
const stepSectionName = getStepSectionName( context.params );
const { providesDependenciesInQuery, excludeFromManageSiteFlows } = flows.getFlow(
flowName,
userLoggedIn
);
// Update initialContext to help woocommerce-install support site switching.
if ( 'woocommerce-install' === flowName ) {
if ( context?.query?.back_to ) {
// forces back_to update
context.store.dispatch( updateDependencies( { back_to: context.query.back_to } ) );
}
initialContext = context;
}
const { query } = initialContext;
// wait for the step component module to load
const stepComponent = await getStepComponent( stepName );
recordPageView( basePath, basePageTitle + ' > Start > ' + flowName + ' > ' + stepName, {
flow: flowName,
} );
context.store.dispatch( setLayoutFocus( 'content' ) );
context.store.dispatch( setCurrentFlowName( flowName ) );
const searchParams = new URLSearchParams( window.location.search );
const isAddNewSiteFlow = searchParams.has( 'ref' );
if ( isAddNewSiteFlow ) {
clearSignupDestinationCookie();
}
// Checks if the user entered the signup flow via browser back from checkout page,
// and if they did, we'll show a modified domain step to prevent creating duplicate sites,
// check pau2Xa-1Io-p2#comment-6759.
const signupDestinationCookieExists = retrieveSignupDestination();
const isReEnteringFlow = getSignupCompleteFlowName() === flowName;
const isReEnteringSignupViaBrowserBack =
wasSignupCheckoutPageUnloaded() && signupDestinationCookieExists && isReEnteringFlow;
const isManageSiteFlow =
! excludeFromManageSiteFlows && ! isAddNewSiteFlow && isReEnteringSignupViaBrowserBack;
// If the flow has siteId or siteSlug as query dependencies, we should not clear selected site id
if (
! providesDependenciesInQuery?.includes( 'siteId' ) &&
! providesDependenciesInQuery?.includes( 'siteSlug' ) &&
! isManageSiteFlow
) {
context.store.dispatch( setSelectedSiteId( null ) );
}
// Set referral parameter in signup dependency store so we can retrieve it in getSignupDestination().
const refParameter = query && query.ref;
// Set design parameters in signup depencency store so we can retrieve it in getChecklistThemeDestination().
const themeParameter = query && query.theme;
const themeType = query && query.theme_type;
const styleVariation = query && query.style_variation;
const headerPatternId = query && query.header_pattern_id;
const footerPatternId = query && query.footer_pattern_id;
const sectionPatternIds = query && query.pattern_ids;
// Set plugin parameter in signup dependency store so we can retrieve it in getWithPluginDestination().
const pluginParameter = query && query.plugin;
const pluginBillingPeriod = query && query.billing_period;
const additionalDependencies = {
...( refParameter && { refParameter } ),
...( themeParameter && { themeParameter } ),
...( themeType && { themeType } ),
...( styleVariation && { styleVariation } ),
...( headerPatternId && { headerPatternId } ),
...( footerPatternId && { footerPatternId } ),
...( sectionPatternIds && { sectionPatternIds } ),
...( pluginParameter && { pluginParameter } ),
...( pluginBillingPeriod && { pluginBillingPeriod } ),
};
if ( ! isEmpty( additionalDependencies ) ) {
context.store.dispatch( updateDependencies( additionalDependencies ) );
}
context.primary = createElement( SignupComponent, {
store: context.store,
path: context.path,
initialContext,
locale: context.params.lang,
flowName,
queryObject: query,
refParameter,
stepName,
stepSectionName,
stepComponent,
pageTitle: getFlowPageTitle( flowName, userLoggedIn ),
isManageSiteFlow,
} );
next();
},
setSelectedSiteForSignup( context, next ) {
const { getState, dispatch } = context.store;
const userLoggedIn = isUserLoggedIn( getState() );
const flowName = getFlowName( context.params, userLoggedIn );
const signupDependencies = getSignupDependencyStore( getState() );
let siteIdOrSlug;
if ( 'woocommerce-install' === flowName ) {
// forces query precedence on woocommerce-install
siteIdOrSlug = context.query?.siteSlug || signupDependencies?.siteSlug;
} else {
siteIdOrSlug =
signupDependencies?.siteSlug ||
context.query?.siteSlug ||
signupDependencies?.siteId ||
context.query?.siteId;
}
if ( ! siteIdOrSlug ) {
next();
return;
}
const siteId = getSiteId( getState(), siteIdOrSlug );
if ( siteId ) {
dispatch( setSelectedSiteId( siteId ) );
next();
} else {
// Fetch the site by siteIdOrSlug and then try to select again
dispatch( requestSite( siteIdOrSlug ) )
.catch( () => {
notFound( context, next );
return null;
} )
.then( () => {
let freshSiteId = getSiteId( getState(), siteIdOrSlug );
if ( ! freshSiteId ) {
const wpcomStagingFragment = siteIdOrSlug.replace(
/\.wordpress\.com$/,
'.wpcomstaging.com'
);
freshSiteId = getSiteId( getState(), wpcomStagingFragment );
}
if ( freshSiteId ) {
dispatch( setSelectedSiteId( freshSiteId ) );
}
next();
} );
}
},
};