mediawiki-extensions-Popups/resources/ext.popups/reducers/settings.js
joakin efb7b3d872 Wire up saving enabled/disabled in settings dialog
The save action has been implemented, and it is listened to by the
canonical enabled state in the previews reducer, and by the settings
reducer to perform UI changes.

The enabled state of the application has been kept in the preview
reducer as the canonical source of truth. See supporting changes for
documentation about the decision.

Actions:
* Introduce new action SETTINGS_CHANGE with the enabled status
* Trigger that action when clicking Save in the settings dialog

Reducers:
* Listen to SETTINGS_CHANGE in the preview reducer to update enabled
  status
* On the settings reducer
  * Handle the SETTINGS_CHANGE action
  * Add showHelp flag to determine if the help should be showing

Change listeners:
* Switch to compare past vs present changes in the implementation
* Handle showing and hiding the help

Supporting changes:
* On the rendered settings dialog:
  * Change #hide to actually just hide and remove legacy if statement
  * Add #toggleHelp method to show or hide the help dialog
* Add doc/adr/0003-keep-enabled-state-only-in-preview-reducer.md to
  support the decision of making the saveSettings action creator return
  a Redux.Thunk and keeping the enabled state just in the preview
  reducer.
  * Add NB to actions#saveSettings explaining and linking to the
    document

Follow commits soon:
* Persist the settings change to local storage when it changes, and
  unify with the preview change listener

Change-Id: I80dc5f29fbe6286f2e3e3b50d909894bc5041ccd
2016-12-14 14:35:59 +00:00

48 lines
1.2 KiB
JavaScript

( function ( popups, nextState ) {
/**
* Reducer for actions that modify the state of the settings
*
* @param {Object} state
* @param {Object} action
* @return {Object} state after action
*/
popups.reducers.settings = function ( state, action ) {
if ( state === undefined ) {
state = {
shouldShow: false,
showHelp: false
};
}
switch ( action.type ) {
case popups.actionTypes.SETTINGS_SHOW:
return nextState( state, {
shouldShow: true,
showHelp: false
} );
case popups.actionTypes.SETTINGS_HIDE:
return nextState( state, {
shouldShow: false,
showHelp: false
} );
case popups.actionTypes.SETTINGS_CHANGE:
return action.wasEnabled === action.enabled ?
// If the setting is the same, just hide the dialogs
nextState( state, {
shouldShow: false
} ) :
// If the settings have changed...
nextState( state, {
// If we enabled, we just hide directly, no help
// If we disabled, keep it showing and let the ui show the help.
shouldShow: !action.enabled,
showHelp: !action.enabled
} );
default:
return state;
}
};
}( mediaWiki.popups, mediaWiki.popups.nextState ) );