File size: 6,795 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { translate } from 'i18n-calypso';
import { filter, find, includes, isEmpty, pick, sortBy } from 'lodash';
import { addQueryArgs } from 'calypso/lib/url';
import flows from 'calypso/signup/config/flows';
import { getStepModuleName } from 'calypso/signup/config/step-components';
import steps from 'calypso/signup/config/steps-pure';

const { defaultFlowName } = flows;

function getDefaultFlowName() {
	return defaultFlowName;
}

export function getFlowName( parameters, isUserLoggedIn ) {
	return parameters.flowName && isFlowName( parameters.flowName, isUserLoggedIn )
		? parameters.flowName
		: getDefaultFlowName();
}

function isFlowName( pathFragment, isUserLoggedIn ) {
	return ! isEmpty( flows.getFlow( pathFragment, isUserLoggedIn ) );
}

export function getStepName( parameters ) {
	return find( pick( parameters, [ 'flowName', 'stepName' ] ), isStepName );
}

export function isFirstStepInFlow( flowName, stepName, isUserLoggedIn ) {
	const { steps: stepsBelongingToFlow } = flows.getFlow( flowName, isUserLoggedIn );
	return stepsBelongingToFlow.indexOf( stepName ) === 0;
}

function isStepName( pathFragment ) {
	return ! isEmpty( steps[ pathFragment ] );
}

export function getStepSectionName( parameters ) {
	return find( pick( parameters, [ 'stepName', 'stepSectionName' ] ), isStepSectionName );
}

function isStepSectionName( pathFragment ) {
	return ! isStepName( pathFragment );
}

export function getStepUrl(
	flowName,
	stepName,
	stepSectionName,
	localeSlug,
	params = {},
	frameworkParam = null
) {
	const flow = flowName ? `/${ flowName }` : '';
	const step = stepName ? `/${ stepName }` : '';
	const section = stepSectionName ? `/${ stepSectionName }` : '';
	const locale = localeSlug ? `/${ localeSlug }` : '';
	const framework =
		frameworkParam ||
		( typeof window !== 'undefined' && window.location.pathname.startsWith( '/setup' )
			? '/setup'
			: '/start' );

	const url =
		flowName === defaultFlowName && framework === '/start'
			? // we don't include the default flow name in the route in /start
			  framework + step + section + locale
			: framework + flow + step + section + locale;
	return addQueryArgs( params, url );
}

export function getValidPath( parameters, isUserLoggedIn ) {
	const locale = parameters.lang;
	const flowName = getFlowName( parameters, isUserLoggedIn );
	const currentFlowSteps = flows.getFlow( flowName, isUserLoggedIn ).steps;
	const stepName = getStepName( parameters ) || currentFlowSteps[ 0 ];
	const stepSectionName = getStepSectionName( parameters );

	if ( currentFlowSteps.length === 0 ) {
		return '/';
	}

	return getStepUrl( flowName, stepName, stepSectionName, locale );
}

export function getPreviousStepName( flowName, currentStepName, isUserLoggedIn ) {
	const flow = flows.getFlow( flowName, isUserLoggedIn );
	return flow.steps[ flow.steps.indexOf( currentStepName ) - 1 ];
}

export function getNextStepName( flowName, currentStepName, isUserLoggedIn ) {
	const flow = flows.getFlow( flowName, isUserLoggedIn );
	return flow.steps[ flow.steps.indexOf( currentStepName ) + 1 ];
}

export function getFlowSteps( flowName, isUserLoggedIn ) {
	const flow = flows.getFlow( flowName, isUserLoggedIn );
	return flow.steps;
}

export function getFlowPageTitle( flowName, isUserLoggedIn ) {
	const flow = flows.getFlow( flowName, isUserLoggedIn );
	return flow.pageTitle || translate( 'Create a site' );
}

export function getFlowDestination(
	flowName,
	isUserLoggedIn,
	dependencies,
	localeSlug,
	goesThroughCheckout
) {
	const flow = flows.getFlow( flowName, isUserLoggedIn );
	return typeof flow?.destination === 'function'
		? flow.destination( { flowName, ...dependencies }, localeSlug, goesThroughCheckout )
		: flow?.destination;
}

export function getDestination( destination, dependencies, flowName, localeSlug ) {
	return flows.filterDestination( destination, dependencies, flowName, localeSlug );
}

export function getThemeForDesignType( designType ) {
	switch ( designType ) {
		case 'blog':
			return 'pub/independent-publisher-2';
		case 'grid':
			return 'pub/altofocus';
		case 'page':
			return 'pub/dara';
		case 'store':
			return 'pub/dara';
		default:
			return 'pub/twentyseventeen';
	}
}

export function getFilteredSteps( flowName, progress, isUserLoggedIn ) {
	const flow = flows.getFlow( flowName, isUserLoggedIn );

	if ( ! flow ) {
		return [];
	}

	return sortBy(
		// filter steps...
		filter( progress, ( step ) => includes( flow.steps, step.stepName ) ),
		// then order according to the flow definition...
		( { stepName } ) => flow.steps.indexOf( stepName )
	);
}

export function getFirstInvalidStep( flowName, progress, isUserLoggedIn ) {
	return find( getFilteredSteps( flowName, progress, isUserLoggedIn ), { status: 'invalid' } );
}

export function getCompletedSteps( flowName, progress, options = {}, isUserLoggedIn ) {
	// Option to check that the current `flowName` matches the `lastKnownFlow`.
	// This is to ensure that when resuming progress, we only do so if
	// the last known flow matches the one that the user is returning to.
	if ( options.shouldMatchFlowName ) {
		return filter(
			getFilteredSteps( flowName, progress, isUserLoggedIn ),
			( step ) => 'in-progress' !== step.status && step.lastKnownFlow === flowName
		);
	}
	return filter(
		getFilteredSteps( flowName, progress, isUserLoggedIn ),
		( step ) => 'in-progress' !== step.status
	);
}

export function canResumeFlow( flowName, progress, isUserLoggedIn ) {
	const flow = flows.getFlow( flowName, isUserLoggedIn );
	const flowStepsInProgressStore = getCompletedSteps(
		flowName,
		progress,
		{
			shouldMatchFlowName: true,
		},
		isUserLoggedIn
	);
	return flowStepsInProgressStore.length > 0 && ! flow.disallowResume;
}

export const shouldForceLogin = ( flowName, userLoggedIn ) => {
	const flow = flows.getFlow( flowName, userLoggedIn );
	return !! flow && flow.forceLogin;
};

/**
 * Derive if the "plans" step actually will be visible to the customer in a given flow after the domain step
 * i.e. Check "launch-site" flow while having a purchased paid plan
 * @param  {Object} flowSteps steps in the current flow
 * @returns {boolean} true indicates that "plans" step will be one of the next steps in the flow
 */
export const isPlanSelectionAvailableLaterInFlow = ( flowSteps ) => {
	/**
	 * Caveat here even though "plans" step maybe available in a flow it might not be active
	 * i.e. Check flow "domain"
	 */

	const plansIndex = flowSteps.findIndex( ( stepName ) =>
		[ 'plans', 'plans-pm' ].includes( getStepModuleName( stepName ) )
	);
	const domainsIndex = flowSteps.findIndex(
		( stepName ) => getStepModuleName( stepName ) === 'domains'
	);
	const isPlansStepExistsInFutureOfFlow = plansIndex > 0 && plansIndex > domainsIndex;

	return isPlansStepExistsInFutureOfFlow;
};