File size: 1,787 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
import {
	GUIDED_TOUR_UPDATE,
	GUIDED_TOUR_PAUSE,
	GUIDED_TOUR_RESUME,
} from 'calypso/state/action-types';
import { savePreference } from 'calypso/state/preferences/actions';
import { getPreference } from 'calypso/state/preferences/selectors';

import 'calypso/state/guided-tours/init';
import 'calypso/state/ui/init';

export function quitGuidedTour( { tour, stepName, finished } ) {
	const quitAction = {
		type: GUIDED_TOUR_UPDATE,
		shouldShow: false,
		shouldReallyShow: false,
		tour,
		stepName,
		finished,
	};

	return ( dispatch, getState ) => {
		dispatch( addSeenGuidedTour( getState, tour, finished ) );
		dispatch( quitAction );
	};
}

export function nextGuidedTourStep( { tour, stepName } ) {
	return {
		type: GUIDED_TOUR_UPDATE,
		tour,
		stepName,
		isPaused: false,
	};
}

export function requestGuidedTour( tour ) {
	return nextGuidedTourStep( { tour, stepName: 'init' } );
}

// TODO(mcsf): come up with a much better (read: safer) solution
//
// The way we persist which tours have been seen by the user is subject to
// concurrency issues and history loss, since adding a tour to that collection
// is actually achieved by adding a tour to the client's copy of the collection
// and saving that as the new history.

function addSeenGuidedTour( getState, tourName, finished = false ) {
	return savePreference( 'guided-tours-history', [
		...getPreference( getState(), 'guided-tours-history' ),
		{
			timestamp: Date.now(),
			tourName,
			finished,
		},
	] );
}

export function resetGuidedToursHistory() {
	return savePreference( 'guided-tours-history', [] );
}

export function pauseGuidedTour() {
	return {
		type: GUIDED_TOUR_PAUSE,
		isPaused: true,
	};
}

export function resumeGuidedTour() {
	return {
		type: GUIDED_TOUR_RESUME,
		isPaused: false,
	};
}