Jhobs has uploaded a new change for review. https://gerrit.wikimedia.org/r/320811
Change subject: WIP: Reducers ...................................................................... WIP: Reducers I may actually just write a test for the renderer reducer and then submit this as a baseline. Add the rest of the reducers after in their own patches. Change-Id: I8a2296c6846cd4b0552a485e671af1d974944195 --- M Popups.hooks.php M extension.json M resources/ext.popups/actions.js A resources/ext.popups/reducers.js A tests/qunit/ext.popups/reducers.test.js 5 files changed, 119 insertions(+), 4 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups refs/changes/11/320811/1 diff --git a/Popups.hooks.php b/Popups.hooks.php index 09c968c..3193d63 100644 --- a/Popups.hooks.php +++ b/Popups.hooks.php @@ -99,6 +99,7 @@ 'tests/qunit/ext.popups/userSettings.test.js', 'tests/qunit/ext.popups/experiment.test.js', 'tests/qunit/ext.popups/processLinks.test.js', + 'tests/qunit/ext.popups/reducers.test.js', ], 'dependencies' => [ 'ext.popups', diff --git a/extension.json b/extension.json index 5bb8cbe..c124e8f 100644 --- a/extension.json +++ b/extension.json @@ -62,6 +62,7 @@ "resources/ext.popups/experiment.js", "resources/ext.popups/actions.js", "resources/ext.popups/processLinks.js", + "resources/ext.popups/reducers.js", "resources/ext.popups/boot.js" ], "templates": { diff --git a/resources/ext.popups/actions.js b/resources/ext.popups/actions.js index 45117de..3311dab 100644 --- a/resources/ext.popups/actions.js +++ b/resources/ext.popups/actions.js @@ -1,13 +1,28 @@ ( function ( mw, Redux ) { - var actions = {}; + var actions = {}, + types = { + BOOT: 'BOOT', + LINK_DWELL: 'LINK_DWELL', + LINK_ABANDON: 'LINK_ABANDON', + LINK_CLICK: 'LINK_CLICK', + FETCH_START: 'FETCH_START', + FETCH_END: 'FETCH_END', + FETCH_FAILED: 'FETCH_FAILED', + PREVIEW_ANIMATING: 'PREVIEW_ANIMATING', + PREVIEW_INTERACTIVE: 'PREVIEW_INTERACTIVE', + PREVIEW_CLICK: 'PREVIEW_CLICK', + COG_CLICK: 'COG_CLICK', + SETTINGS_DIALOG_RENDERED: 'SETTINGS_DIALOG_RENDERED', + SETTINGS_DIALOG_CLOSED: 'SETTINGS_DIALOG_CLOSED' + }; /** * @param {Function} isUserInCondition See `mw.popups.createExperiment` */ actions.boot = function ( isUserInCondition ) { return { - type: 'BOOT', + type: types.BOOT, isUserInCondition: isUserInCondition() }; }; @@ -21,7 +36,7 @@ */ actions.linkDwell = function ( $el ) { return { - type: 'LINK_DWELL', + type: types.LINK_DWELL, el: $el }; }; @@ -36,10 +51,12 @@ */ actions.linkAbandon = function ( $el ) { return { - type: 'LINK_ABANDON', + type: types.LINK_ABANDON, el: $el }; }; + + mw.popups.actionTypes = types; /** * Creates an object whose methods encapsulate all actions that can be @@ -54,3 +71,4 @@ }; }( mediaWiki, Redux ) ); + diff --git a/resources/ext.popups/reducers.js b/resources/ext.popups/reducers.js new file mode 100644 index 0000000..67d0556 --- /dev/null +++ b/resources/ext.popups/reducers.js @@ -0,0 +1,59 @@ +( function ( mw, $, Redux ) { + mw.popups.reducers = {}; + + /** + * 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 + */ + mw.popups.reducers.preview = function ( state, action ) { + var prevState = typeof state !== 'undefined' ? state : {}; + + switch ( action.type ) { + case mw.popups.actionTypes.BOOT: + return $.extend( {}, prevState, { + enabled: true + } ); + default: + return prevState; + } + }; + + /** + * Reducer for actions that modify the state of the view + * + * @param {Object} state before action + * @param {Object} action Redux action that modified state. + * Must have `type` property. + * @return {Object} state after action + */ + mw.popups.reducers.renderer = function ( state, action ) { + var prevState = typeof state !== 'undefined' ? state : {}; + + switch ( action.type ) { + case mw.popups.actionTypes.PREVIEW_ANIMATING: + return $.extend( {}, prevState, { + isAnimating: true, + isInteractive: false + } ); + default: + return prevState; + } + }; + + /** + * Root reducer for all actions + * + * @param {Object} global state before action + * @param {Object} action Redux action that modified state. + * Must have `type` property. + * @return {Object} global state after action + */ + mw.popups.reducers.rootReducer = Redux.combineReducers( { + model: mw.popups.reducers.preview, + view: mw.popups.reducers.renderer + } ); +}( mediaWiki, jQuery, Redux ) ); diff --git a/tests/qunit/ext.popups/reducers.test.js b/tests/qunit/ext.popups/reducers.test.js new file mode 100644 index 0000000..d7421ec --- /dev/null +++ b/tests/qunit/ext.popups/reducers.test.js @@ -0,0 +1,36 @@ +( function ( mw ) { + + QUnit.module( 'ext.popups/reducers', { + setup: function () { + } + } ); + + QUnit.test( '#rootReducer', function ( assert ) { + assert.expect( 1 ); + + assert.deepEqual( + mw.popups.reducers.rootReducer( undefined, { type: undefined } ), + { + model: {}, + view: {} + }, + 'It should initialize the state by default' + ); + } ); + + QUnit.test( '#model', function ( assert ) { + assert.expect( 1 ); + + assert.deepEqual( + mw.popups.reducers.preview( + {}, + { + type: 'BOOT', + isUserInCondition: true + } ), + { enabled: true }, + 'It should set enabled to true when the user is in the enabled condition.' + ); + } ); +}( mediaWiki ) ); + -- To view, visit https://gerrit.wikimedia.org/r/320811 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I8a2296c6846cd4b0552a485e671af1d974944195 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Popups Gerrit-Branch: mpga Gerrit-Owner: Jhobs <jhob...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits