2017-07-28 17:32:46 +00:00
|
|
|
import actionTypes from '../actionTypes';
|
|
|
|
import nextState from './nextState';
|
2016-12-13 10:25:17 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
/**
|
|
|
|
* Reducer for actions that modify the state of the preview model
|
|
|
|
*
|
|
|
|
* @param {Object} state before action
|
|
|
|
* @param {Object} action Redux action that modified state.
|
|
|
|
* Must have `type` property.
|
|
|
|
* @return {Object} state after action
|
|
|
|
*/
|
2017-07-28 17:32:46 +00:00
|
|
|
export default function preview( state, action ) {
|
2017-02-02 02:24:12 +00:00
|
|
|
if ( state === undefined ) {
|
|
|
|
state = {
|
|
|
|
enabled: undefined,
|
|
|
|
activeLink: undefined,
|
|
|
|
activeEvent: undefined,
|
|
|
|
activeToken: '',
|
|
|
|
shouldShow: false,
|
|
|
|
isUserDwelling: false
|
|
|
|
};
|
|
|
|
}
|
2016-12-13 10:25:17 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
switch ( action.type ) {
|
|
|
|
case actionTypes.BOOT:
|
|
|
|
return nextState( state, {
|
|
|
|
enabled: action.isEnabled
|
|
|
|
} );
|
|
|
|
case actionTypes.SETTINGS_CHANGE:
|
|
|
|
return nextState( state, {
|
|
|
|
enabled: action.enabled
|
|
|
|
} );
|
|
|
|
case actionTypes.LINK_DWELL:
|
|
|
|
// New interaction
|
|
|
|
if ( action.el !== state.activeLink ) {
|
2016-12-13 18:04:03 +00:00
|
|
|
return nextState( state, {
|
2017-02-02 02:24:12 +00:00
|
|
|
activeLink: action.el,
|
|
|
|
activeEvent: action.event,
|
|
|
|
activeToken: action.token,
|
2017-01-09 19:08:46 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
// When the user dwells on a link with their keyboard, a preview is
|
|
|
|
// renderered, and then dwells on another link, the ABANDON_END
|
|
|
|
// action will be ignored.
|
|
|
|
//
|
|
|
|
// Ensure that all the preview is hidden.
|
|
|
|
shouldShow: false,
|
2016-12-13 10:25:17 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
isUserDwelling: true
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
// Dwelling back into the same link
|
2016-12-13 10:25:17 +00:00
|
|
|
return nextState( state, {
|
|
|
|
isUserDwelling: true
|
|
|
|
} );
|
2017-02-02 02:24:12 +00:00
|
|
|
}
|
2017-01-09 19:08:46 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
case actionTypes.ABANDON_END:
|
|
|
|
if ( action.token === state.activeToken && !state.isUserDwelling ) {
|
2016-12-13 10:25:17 +00:00
|
|
|
return nextState( state, {
|
2017-02-02 02:24:12 +00:00
|
|
|
activeLink: undefined,
|
|
|
|
activeToken: undefined,
|
|
|
|
activeEvent: undefined,
|
|
|
|
fetchResponse: undefined,
|
|
|
|
shouldShow: false
|
2016-12-13 10:25:17 +00:00
|
|
|
} );
|
2017-02-02 02:24:12 +00:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
|
|
|
|
case actionTypes.PREVIEW_DWELL:
|
|
|
|
return nextState( state, {
|
|
|
|
isUserDwelling: true
|
|
|
|
} );
|
2017-01-09 19:08:46 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
case actionTypes.ABANDON_START:
|
|
|
|
return nextState( state, {
|
|
|
|
isUserDwelling: false
|
|
|
|
} );
|
|
|
|
|
|
|
|
case actionTypes.FETCH_START:
|
|
|
|
return nextState( state, {
|
|
|
|
fetchResponse: undefined
|
|
|
|
} );
|
2017-03-26 18:19:24 +00:00
|
|
|
case actionTypes.FETCH_COMPLETE:
|
2017-04-07 12:40:55 +00:00
|
|
|
if ( action.token === state.activeToken ) {
|
2016-12-13 10:25:17 +00:00
|
|
|
return nextState( state, {
|
2017-02-02 02:24:12 +00:00
|
|
|
fetchResponse: action.result,
|
2017-04-21 10:59:38 +00:00
|
|
|
shouldShow: state.isUserDwelling
|
2016-12-13 10:25:17 +00:00
|
|
|
} );
|
2017-02-02 02:24:12 +00:00
|
|
|
}
|
2016-12-13 10:25:17 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
/* falls through */
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2017-07-28 17:32:46 +00:00
|
|
|
}
|