Skip to content

Commit

Permalink
Edit Post: Add store handling for pinned plugin items
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed May 7, 2018
1 parent 4806350 commit 74f4a15
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const PluginSidebarMoreMenuItem = ( { children, icon, isPinned, isSelected, onCl
<Fragment>
{ isPinned && icon && (
<PinnedPlugins>
<IconButton
{ <IconButton
icon={ icon }
label={ children }
onClick={ onClick }
isToggled={ isSelected }
aria-expanded={ isSelected }
/>
/> }
</PinnedPlugins>
) }
<PluginsMoreMenuGroup>
Expand Down
16 changes: 15 additions & 1 deletion edit-post/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function toggleGeneralSidebarEditorPanel( panel ) {
/**
* Returns an action object used to toggle a feature flag.
*
* @param {string} feature Featurre name.
* @param {string} feature Feature name.
*
* @return {Object} Action object.
*/
Expand All @@ -91,6 +91,20 @@ export function switchEditorMode( mode ) {
};
}

/**
* Returns an action object used to toggle a plugin name flag.
*
* @param {string} pluginName Plugin name.
*
* @return {Object} Action object.
*/
export function togglePinnedPluginItem( pluginName ) {
return {
type: 'TOGGLE_PINNED_PLUGIN_ITEM',
pluginName,
};
}

/**
* Returns an action object used to check the state of meta boxes at a location.
*
Expand Down
1 change: 1 addition & 0 deletions edit-post/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const PREFERENCES_DEFAULTS = {
features: {
fixedToolbar: false,
},
pinnedPluginItems: {},
};
9 changes: 9 additions & 0 deletions edit-post/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export const preferences = combineReducers( {

return state;
},
pinnedPluginItems( state = PREFERENCES_DEFAULTS.pinnedPluginItems, action ) {
if ( action.type === 'TOGGLE_PINNED_PLUGIN_ITEM' ) {
return {
...state,
[ action.pluginName ]: ! state[ action.pluginName ],
};
}
return state;
},
} );

export function panel( state = 'document', action ) {
Expand Down
22 changes: 12 additions & 10 deletions edit-post/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ export function isFeatureActive( state, feature ) {
return !! state.preferences.features[ feature ];
}

/**
* Returns true if the the plugin item is pinned to the header.
*
* @param {Object} state Global application state.
* @param {string} pluginName Plugin item name.
* @return {boolean} Whether the plugin item is pinned.
*/
export function isPluginItemPinned( state, pluginName ) {
const pinnedPluginItems = getPreference( state, 'pinnedPluginItems', {} );
return Boolean( pinnedPluginItems[ pluginName ] );
}

/**
* Returns the state of legacy meta boxes.
*
Expand Down Expand Up @@ -154,13 +166,3 @@ export const hasMetaBoxes = createSelector(
export function isSavingMetaBoxes( state ) {
return state.isSavingMetaBoxes;
}

/**
* Returns true if the the plugin item is pinned to the header.
*
* @param {Object} state Global application state.
* @return {boolean} Whether the plugin item is pinned.
*/
export function isPluginItemPinned( state ) {
return Boolean( state );
}
12 changes: 12 additions & 0 deletions edit-post/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
closePublishSidebar,
togglePublishSidebar,
toggleFeature,
togglePinnedPluginItem,
requestMetaBoxUpdates,
initializeMetaBoxState,
} from '../actions';
Expand Down Expand Up @@ -76,6 +77,17 @@ describe( 'actions', () => {
} );
} );

describe( 'togglePinnedPluginItem', () => {
it( 'should return TOGGLE_PINNED_PLUGIN_ITEM action', () => {
const pluginName = 'foo/bar';

expect( togglePinnedPluginItem( pluginName ) ).toEqual( {
type: 'TOGGLE_PINNED_PLUGIN_ITEM',
pluginName,
} );
} );
} );

describe( 'requestMetaBoxUpdates', () => {
it( 'should return the REQUEST_META_BOX_UPDATES action', () => {
expect( requestMetaBoxUpdates() ).toEqual( {
Expand Down
33 changes: 33 additions & 0 deletions edit-post/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe( 'state', () => {
editorMode: 'visual',
panels: { 'post-status': true },
features: { fixedToolbar: false },
pinnedPluginItems: {},
} );
} );

Expand Down Expand Up @@ -50,6 +51,7 @@ describe( 'state', () => {
editorMode: 'visual',
panels: { 'post-status': true },
features: { fixedToolbar: false },
pinnedPluginItems: {},
} );
} );

Expand Down Expand Up @@ -113,6 +115,37 @@ describe( 'state', () => {

expect( state.features ).toEqual( { chicken: false } );
} );

describe( 'pinnedPluginItems', () => {
const initialState = deepFreeze( {
pinnedPluginItems: {
'foo/enabled': true,
},
} );

it( 'should enable a pinned plugin flag when the value does not exist', () => {
const state = preferences( initialState, {
type: 'TOGGLE_PINNED_PLUGIN_ITEM',
pluginName: 'foo/does-not-exist',
} );

expect( state.pinnedPluginItems ).toEqual( {
'foo/enabled': true,
'foo/does-not-exist': true,
} );
} );

it( 'should disable a pinned plugin flag when it is enabled', () => {
const state = preferences( initialState, {
type: 'TOGGLE_PINNED_PLUGIN_ITEM',
pluginName: 'foo/enabled',
} );

expect( state.pinnedPluginItems ).toEqual( {
'foo/enabled': false,
} );
} );
} );
} );

describe( 'isSavingMetaBoxes', () => {
Expand Down
20 changes: 20 additions & 0 deletions edit-post/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isEditorSidebarPanelOpened,
isFeatureActive,
isPluginSidebarOpened,
isPluginItemPinned,
getMetaBoxes,
hasMetaBoxes,
isSavingMetaBoxes,
Expand Down Expand Up @@ -186,6 +187,25 @@ describe( 'selectors', () => {
expect( isFeatureActive( state, 'chicken' ) ).toBe( false );
} );
} );

describe( 'isPluginItemPinned', () => {
const state = {
preferences: {
pinnedPluginItems: {
'foo/bar': true,
},
},
};

it( 'should return false if plugin item is not pinned', () => {
expect( isPluginItemPinned( state, 'foo/unknown' ) ).toBe( false );
} );

it( 'should return true if plugin item item is pinned', () => {
expect( isPluginItemPinned( state, 'foo/bar' ) ).toBe( true );
} );
} );

describe( 'hasMetaBoxes', () => {
it( 'should return true if there are active meta boxes', () => {
const state = {
Expand Down

0 comments on commit 74f4a15

Please sign in to comment.