Skip to content

Commit

Permalink
Merge pull request #6417 from Automattic/add/guided-tours-log-selecto…
Browse files Browse the repository at this point in the history
…rs-rebased

Guided Tours: Find & show tours based on state selectors
  • Loading branch information
mcsf authored Jul 18, 2016
2 parents aad2994 + fbb5cdb commit 9e64cde
Show file tree
Hide file tree
Showing 13 changed files with 432 additions and 55 deletions.
4 changes: 3 additions & 1 deletion client/boot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ function reduxStoreReady( reduxStore ) {

// clear notices
page( '*', function( context, next ) {
context.store.dispatch( setRouteAction( context.pathname ) );
context.store.dispatch( setRouteAction(
context.pathname,
context.query ) );
next();
} );

Expand Down
12 changes: 2 additions & 10 deletions client/layout/guided-tours/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import i18n from 'i18n-calypso';
*/
import { getSelectedSite } from 'state/ui/selectors';

function get( tour = 'main' ) {
function get( tour ) {
const tours = {
main: {
version: '20160601',
Expand Down Expand Up @@ -108,17 +108,9 @@ function get( tour = 'main' ) {
linkUrl: 'https://learn.wordpress.com',
},
},
test: {
version: '20160516',
init: {
description: 'Testing multi tour support',
text: i18n.translate( 'Single step tour!' ),
type: 'FinishStep',
},
}
};

return tours[ tour ] || tours.main;
return tours[ tour ] || null;
}

export default {
Expand Down
6 changes: 5 additions & 1 deletion client/layout/guided-tours/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ class GuidedTours extends Component {
return true;
};
const proceedToNextStep = () => {
this.props.nextGuidedTourStep( { stepName: nextStepName } );
this.props.nextGuidedTourStep( {
stepName: nextStepName,
tour: this.props.tourState.tour,
} );
};
const abortTour = () => {
const ERROR_WAITED_TOO_LONG = 'waited too long for next target';
Expand All @@ -99,6 +102,7 @@ class GuidedTours extends Component {
this.currentTarget && this.currentTarget.classList.remove( 'guided-tours__overlay' );
this.props.quitGuidedTour( Object.assign( {
stepName: this.props.tourState.stepName,
tour: this.props.tourState.tour,
}, options ) );
}

Expand Down
21 changes: 15 additions & 6 deletions client/state/ui/action-log/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* External dependencies
*/
import includes from 'lodash/includes';
import takeRight from 'lodash/takeRight';
import { takeRight } from 'lodash';

/**
* Internal dependencies
Expand All @@ -14,18 +13,28 @@ import {
ROUTE_SET,
} from 'state/action-types';

const isRelevantActionType = includes.bind( null, [
const relevantTypes = {
GUIDED_TOUR_UPDATE,
THEMES_RECEIVE,
PREVIEW_IS_SHOWING,
ROUTE_SET,
] );
};

const isRelevantAction = ( action ) =>
relevantTypes.hasOwnProperty( action.type ) &&
( typeof relevantTypes[ action.type ] !== 'function' ||
relevantTypes[ action.type ]( action ) );

const newAction = ( action ) => ( {
...action, timestamp: Date.now()
} );

const maybeAdd = ( state, action ) =>
action
? takeRight( [ ...state, action ], 50 )
: state;

export default ( state = [], action ) =>
isRelevantActionType( action.type )
? takeRight( [ ...state, newAction( action ) ], 50 )
isRelevantAction( action )
? maybeAdd( state, newAction( action ) )
: state;
21 changes: 19 additions & 2 deletions client/state/ui/action-log/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { expect } from 'chai';
import { useFakeTimers } from 'test/helpers/use-sinon';
import {
ROUTE_SET,
COMMENTS_LIKE,
} from 'state/action-types';
import reducer from '../reducer';

Expand All @@ -25,11 +26,11 @@ describe( 'reducer', () => {
const actions = [
{
type: ROUTE_SET,
path: '/menus/77203074',
path: '/design/77203074',
},
{
type: ROUTE_SET,
path: '/menus/foobar',
path: '/design/foobar',
},
];
const state = actions.reduce( reducer, undefined );
Expand All @@ -39,4 +40,20 @@ describe( 'reducer', () => {
{ ...actions[ 1 ], timestamp: 1337 },
] );
} );

it( 'should discard them if payload is irrelevant', () => {
const actions = [
{
type: COMMENTS_LIKE,
path: '/menus/77203074',
},
{
type: COMMENTS_LIKE,
path: '/menus/foobar',
},
];
const state = actions.reduce( reducer, undefined );

expect( state ).to.eql( [] );
} );
} );
10 changes: 6 additions & 4 deletions client/state/ui/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ export function setAllSitesSelected() {
/**
* Returns an action object signalling that the current route is to be changed
*
* @param {String} path Route path
* @return {Object} Action object
* @param {String} path Route path
* @param {Object} [query] Query arguments
* @return {Object} Action object
*/
export function setRoute( path ) {
export function setRoute( path, query = {} ) {
return {
type: ROUTE_SET,
path
path,
query,
};
}

Expand Down
8 changes: 5 additions & 3 deletions client/state/ui/guided-tours/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import guidedToursConfig from 'layout/guided-tours/config';
import { savePreference } from 'state/preferences/actions';
import { getPreference } from 'state/preferences/selectors';

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

const trackEvent = recordTracksEvent( `calypso_guided_tours_${ finished ? 'finished' : 'quit' }`, {
Expand All @@ -31,14 +32,15 @@ export function quitGuidedTour( { tour = 'main', stepName, finished, error } ) {
} );

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

export function nextGuidedTourStep( { tour = 'main', stepName } ) {
export function nextGuidedTourStep( { tour, stepName } ) {
const nextAction = {
type: GUIDED_TOUR_UPDATE,
tour,
stepName,
};

Expand Down
Loading

0 comments on commit 9e64cde

Please sign in to comment.