Skip to content

Commit

Permalink
Store: Factorize some action creators
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Oct 6, 2017
1 parent 3a3908a commit 2456fd2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
24 changes: 24 additions & 0 deletions editor/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,30 @@ export function stopTyping() {
};
}

/**
* Returns an action object used in signalling that the user toggled the sidebar
*
* @return {Object} Action object
*/
export function toggleSidebar() {
return {
type: 'TOGGLE_SIDEBAR',
};
}

/**
* Returns an action object used in signalling that the user switched the active sidebar tab panel
*
* @param {String} panel The panel name
* @return {Object} Action object
*/
export function setActivePanel( panel ) {
return {
type: 'SET_ACTIVE_PANEL',
panel,
};
}

/**
* Returns an action object used in signalling that the user toggled a sidebar panel
*
Expand Down
24 changes: 9 additions & 15 deletions editor/block-settings-menu/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { IconButton } from '@wordpress/components';
* Internal dependencies
*/
import { isEditorSidebarOpened } from '../selectors';
import { selectBlock } from '../actions';
import { selectBlock, removeBlock, toggleSidebar, setActivePanel } from '../actions';

function BlockSettingsMenuContent( { onDelete, onSelect, isSidebarOpened, toggleSidebar, setActivePanel } ) {
function BlockSettingsMenuContent( { onDelete, onSelect, isSidebarOpened, onToggleSidebar, onShowInspector } ) {
const toggleInspector = () => {
onSelect();
setActivePanel();
onShowInspector();
if ( ! isSidebarOpened ) {
toggleSidebar();
onToggleSidebar();
}
};

Expand Down Expand Up @@ -48,22 +48,16 @@ export default connect(
} ),
( dispatch, ownProps ) => ( {
onDelete() {
dispatch( {
type: 'REMOVE_BLOCKS',
uids: [ ownProps.uid ],
} );
dispatch( removeBlock( ownProps.uid ) );
},
onSelect() {
dispatch( selectBlock( ownProps.uid ) );
},
setActivePanel() {
dispatch( {
type: 'SET_ACTIVE_PANEL',
panel: 'block',
} );
onShowInspector() {
dispatch( setActivePanel( 'block' ) );
},
toggleSidebar() {
dispatch( { type: 'TOGGLE_SIDEBAR' } );
onToggleSidebar() {
dispatch( toggleSidebar() );
},
} )
)( BlockSettingsMenuContent );
13 changes: 5 additions & 8 deletions editor/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import PreviewButton from './preview-button';
import ModeSwitcher from './mode-switcher';
import Inserter from '../inserter';
import { getMultiSelectedBlockUids, hasEditorUndo, hasEditorRedo, isEditorSidebarOpened } from '../selectors';
import { clearSelectedBlock } from '../actions';
import { clearSelectedBlock, toggleSidebar, removeBlocks } from '../actions';

function Header( {
multiSelectedBlockUids,
Expand All @@ -29,7 +29,7 @@ function Header( {
redo,
hasRedo,
hasUndo,
toggleSidebar,
onToggleSidebar,
isSidebarOpened,
} ) {
const count = multiSelectedBlockUids.length;
Expand Down Expand Up @@ -90,7 +90,7 @@ function Header( {
<PublishButton />
<IconButton
icon="admin-generic"
onClick={ toggleSidebar }
onClick={ onToggleSidebar }
isToggled={ isSidebarOpened }
label={ __( 'Settings' ) }
/>
Expand All @@ -109,12 +109,9 @@ export default connect(
} ),
( dispatch ) => ( {
onDeselect: () => dispatch( clearSelectedBlock() ),
onRemove: ( uids ) => dispatch( {
type: 'REMOVE_BLOCKS',
uids,
} ),
onRemove: ( uids ) => dispatch( removeBlocks( uids ) ),
undo: () => dispatch( { type: 'UNDO' } ),
redo: () => dispatch( { type: 'REDO' } ),
toggleSidebar: () => dispatch( { type: 'TOGGLE_SIDEBAR' } ),
onToggleSidebar: () => dispatch( toggleSidebar() ),
} )
)( Header );
14 changes: 5 additions & 9 deletions editor/sidebar/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { IconButton } from '@wordpress/components';
* Internal Dependencies
*/
import { getActivePanel } from '../selectors';
import { toggleSidebar, setActivePanel } from '../actions';

const SidebarHeader = ( { panel, onSetPanel, toggleSidebar } ) => {
const SidebarHeader = ( { panel, onSetPanel, onToggleSidebar } ) => {
return (
<div className="components-panel__header editor-sidebar__panel-tabs">
<button
Expand All @@ -32,7 +33,7 @@ const SidebarHeader = ( { panel, onSetPanel, toggleSidebar } ) => {
{ __( 'Block' ) }
</button>
<IconButton
onClick={ toggleSidebar }
onClick={ onToggleSidebar }
icon="no-alt"
label={ __( 'Close settings' ) }
/>
Expand All @@ -45,12 +46,7 @@ export default connect(
panel: getActivePanel( state ),
} ),
( dispatch ) => ( {
onSetPanel( panel ) {
dispatch( {
type: 'SET_ACTIVE_PANEL',
panel: panel,
} );
},
toggleSidebar: () => dispatch( { type: 'TOGGLE_SIDEBAR' } ),
onSetPanel: ( panel ) => dispatch( setActivePanel( panel ) ),
onToggleSidebar: () => dispatch( toggleSidebar() ),
} )
)( SidebarHeader );

0 comments on commit 2456fd2

Please sign in to comment.