File size: 2,868 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
import { RootChild } from '@automattic/components';
import { defer } from 'lodash';
import { Component } from 'react';
import { connect } from 'react-redux';
import QueryPreferences from 'calypso/components/data/query-preferences';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { nextGuidedTourStep, quitGuidedTour } from 'calypso/state/guided-tours/actions';
import { getGuidedTourState } from 'calypso/state/guided-tours/selectors';
import { getLastAction } from 'calypso/state/ui/action-log/selectors';
import { getSectionName, isSectionLoading } from 'calypso/state/ui/selectors';
import AllTours from './all-tours';
import './style.scss';

class GuidedToursComponent extends Component {
	shouldComponentUpdate( nextProps ) {
		return this.props.tourState !== nextProps.tourState;
	}

	start = ( { step, tour, tourVersion: tour_version } ) => {
		if ( tour && tour_version ) {
			this.props.dispatch( nextGuidedTourStep( { step, tour } ) );
			recordTracksEvent( 'calypso_guided_tours_start', { tour, tour_version } );
		}
	};

	next = ( { step, tour, tourVersion, nextStepName, skipping = false } ) => {
		if ( ! skipping && step ) {
			recordTracksEvent( 'calypso_guided_tours_seen_step', {
				tour,
				step,
				tour_version: tourVersion,
			} );
		}

		defer( () => {
			this.props.dispatch( nextGuidedTourStep( { tour, stepName: nextStepName } ) );
		} );
	};

	quit = ( { step, tour, tourVersion: tour_version, isLastStep } ) => {
		if ( step ) {
			recordTracksEvent( 'calypso_guided_tours_seen_step', {
				tour,
				step,
				tour_version,
			} );
		}

		recordTracksEvent( `calypso_guided_tours_${ isLastStep ? 'finished' : 'quit' }`, {
			step,
			tour,
			tour_version,
		} );

		this.props.dispatch( quitGuidedTour( { tour, stepName: step, finished: isLastStep } ) );
	};

	render() {
		const { tour: tourName, stepName = 'init', shouldShow } = this.props.tourState;

		if ( ! shouldShow ) {
			return null;
		}

		return (
			<RootChild>
				<div className="guided-tours__root">
					<QueryPreferences />
					<AllTours
						sectionName={ this.props.sectionName }
						shouldPause={ this.props.shouldPause }
						tourName={ tourName }
						stepName={ stepName }
						lastAction={ this.props.lastAction }
						isValid={ this.props.isValid }
						next={ this.next }
						quit={ this.quit }
						start={ this.start }
						dispatch={ this.props.dispatch }
					/>
				</div>
			</RootChild>
		);
	}
}

const getTourWhenState = ( state ) => ( when ) => !! when( state );

export default connect( ( state ) => {
	const tourState = getGuidedTourState( state );
	const shouldPause = isSectionLoading( state ) || tourState.isPaused;
	return {
		sectionName: getSectionName( state ),
		shouldPause,
		tourState,
		isValid: getTourWhenState( state ),
		lastAction: getLastAction( state ),
	};
} )( GuidedToursComponent );