File size: 3,544 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
import page from '@automattic/calypso-router';
import { IntentScreen } from '@automattic/onboarding';
import { useTranslate } from 'i18n-calypso';
import { useEffect } from 'react';
import intentImageUrl from 'calypso/assets/images/onboarding/intent.svg';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { preventWidows } from 'calypso/lib/formatting';
import { addQueryArgs } from 'calypso/lib/route';
import useBranchSteps from 'calypso/signup/hooks/use-branch-steps';
import StepWrapper from 'calypso/signup/step-wrapper';
import { useDispatch, useSelector } from 'calypso/state';
import { canCurrentUser } from 'calypso/state/selectors/can-current-user';
import { saveSignupStep, submitSignupStep } from 'calypso/state/signup/progress/actions';
import { getSiteId } from 'calypso/state/sites/selectors';
import { useIntents, useIntentsAlt } from './intents';
import type { IntentFlag } from './types';
import type { Dependencies } from 'calypso/signup/types';

interface Props {
	goToNextStep: () => void;
	signupDependencies: any;
	stepName: string;
	queryObject: {
		siteSlug?: string;
		siteId?: string;
	};
}

export const EXCLUDED_STEPS: { [ key: string ]: string[] } = {
	write: [ 'store-options', 'store-features' ],
	build: [ 'site-options', 'starting-point', 'courses', 'store-options', 'store-features' ],
	sell: [ 'site-options', 'starting-point', 'courses' ],
	wpadmin: [ 'store-options', 'store-features', 'site-options', 'starting-point', 'courses' ],
};

const EXTERNAL_FLOW: { [ key: string ]: string } = {
	import: '/setup/site-setup/import',
};

const getExcludedSteps = ( providedDependencies?: Dependencies ) =>
	EXCLUDED_STEPS[ providedDependencies?.intent ];

export default function IntentStep( props: Props ) {
	const dispatch = useDispatch();
	const translate = useTranslate();
	const { goToNextStep, stepName, queryObject } = props;
	const headerText = translate( 'Where will you start?' );
	const subHeaderText = translate( 'You can change your mind at any time.' );
	const branchSteps = useBranchSteps( stepName, getExcludedSteps );

	const siteId =
		useSelector( ( state ) => getSiteId( state, queryObject.siteSlug as string ) ) ||
		queryObject?.siteId;
	const canImport = useSelector( ( state ) =>
		canCurrentUser( state, siteId as number, 'manage_options' )
	);

	const intents = useIntents();
	const intentsAlt = useIntentsAlt( canImport );

	const submitIntent = ( intent: IntentFlag ) => {
		const providedDependencies = { intent };

		recordTracksEvent( 'calypso_signup_intent_select', providedDependencies );

		if ( EXTERNAL_FLOW[ intent ] ) {
			dispatch( submitSignupStep( { stepName }, providedDependencies ) );
			page( addQueryArgs( queryObject, EXTERNAL_FLOW[ intent ] ) );
		} else {
			branchSteps( providedDependencies );
			dispatch( submitSignupStep( { stepName }, providedDependencies ) );
			goToNextStep();
		}
	};

	// Only do following things when mounted
	useEffect( () => {
		dispatch( saveSignupStep( { stepName } ) );
	}, [] ); // eslint-disable-line react-hooks/exhaustive-deps

	return (
		<StepWrapper
			headerText={ headerText }
			fallbackHeaderText={ headerText }
			headerImageUrl={ intentImageUrl }
			subHeaderText={ subHeaderText }
			fallbackSubHeaderText={ subHeaderText }
			stepContent={
				<IntentScreen
					intents={ intents }
					intentsAlt={ intentsAlt }
					onSelect={ submitIntent }
					preventWidows={ preventWidows }
				/>
			}
			align="left"
			hideSkip
			isHorizontalLayout
			siteId={ siteId }
			{ ...props }
		/>
	);
}