Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components: Extract the discussion panel as reusable components #3312

Merged
merged 1 commit into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions editor/post-comments/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { FormToggle, withInstanceId } from '@wordpress/components';

/**
* Internal Dependencies
*/
import { getEditedPostAttribute } from '../selectors';
import { editPost } from '../actions';

function PostComments( { commentStatus = 'open', instanceId, ...props } ) {
const onToggleComments = () => props.editPost( { comment_status: commentStatus === 'open' ? 'closed' : 'open' } );

const commentsToggleId = 'allow-comments-toggle-' + instanceId;

return [
<label key="label" htmlFor={ commentsToggleId }>{ __( 'Allow Comments' ) }</label>,
<FormToggle
key="toggle"
checked={ commentStatus === 'open' }
onChange={ onToggleComments }
showHint={ false }
id={ commentsToggleId }
/>,
];
}

export default connect(
( state ) => {
return {
commentStatus: getEditedPostAttribute( state, 'comment_status' ),
};
},
{
editPost,
}
)( withInstanceId( PostComments ) );

45 changes: 45 additions & 0 deletions editor/post-pingbacks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { FormToggle, withInstanceId } from '@wordpress/components';

/**
* Internal Dependencies
*/
import { getEditedPostAttribute } from '../selectors';
import { editPost } from '../actions';

function PostPingbacks( { pingStatus = 'open', instanceId, ...props } ) {
const onTogglePingback = () => props.editPost( { ping_status: pingStatus === 'open' ? 'closed' : 'open' } );

const pingbacksToggleId = 'allow-pingbacks-toggle-' + instanceId;

return [
<label key="label" htmlFor={ pingbacksToggleId }>{ __( 'Allow Pingbacks & Trackbacks' ) }</label>,
<FormToggle
key="toggle"
checked={ pingStatus === 'open' }
onChange={ onTogglePingback }
showHint={ false }
id={ pingbacksToggleId }
/>,
];
}

export default connect(
( state ) => {
return {
pingStatus: getEditedPostAttribute( state, 'ping_status' ),
};
},
{
editPost,
}
)( withInstanceId( PostPingbacks ) );

37 changes: 9 additions & 28 deletions editor/sidebar/discussion-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,29 @@ import { connect } from 'react-redux';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { PanelBody, PanelRow, FormToggle, withInstanceId } from '@wordpress/components';
import { PanelBody, PanelRow } from '@wordpress/components';

/**
* Internal Dependencies
*/
import { getEditedPostAttribute, isEditorSidebarPanelOpened } from '../../selectors';
import { editPost, toggleSidebarPanel } from '../../actions';
import PostComments from '../../post-comments';
import PostPingbacks from '../../post-pingbacks';
import { isEditorSidebarPanelOpened } from '../../selectors';
import { toggleSidebarPanel } from '../../actions';

/**
* Module Constants
*/
const PANEL_NAME = 'discussion-panel';

function DiscussionPanel( { pingStatus = 'open', commentStatus = 'open', instanceId, isOpened, onTogglePanel, ...props } ) {
const onTogglePingback = () => props.editPost( { ping_status: pingStatus === 'open' ? 'closed' : 'open' } );
const onToggleComments = () => props.editPost( { comment_status: commentStatus === 'open' ? 'closed' : 'open' } );

const commentsToggleId = 'allow-comments-toggle-' + instanceId;
const pingbacksToggleId = 'allow-pingbacks-toggle-' + instanceId;

function DiscussionPanel( { isOpened, onTogglePanel } ) {
return (
<PanelBody title={ __( 'Discussion' ) } opened={ isOpened } onToggle={ onTogglePanel }>
<PanelRow>
<label htmlFor={ commentsToggleId }>{ __( 'Allow Comments' ) }</label>
<FormToggle
checked={ commentStatus === 'open' }
onChange={ onToggleComments }
showHint={ false }
id={ commentsToggleId }
/>
<PostComments />
</PanelRow>
<PanelRow>
<label htmlFor={ pingbacksToggleId }>{ __( 'Allow Pingbacks & Trackbacks' ) }</label>
<FormToggle
checked={ pingStatus === 'open' }
onChange={ onTogglePingback }
showHint={ false }
id={ pingbacksToggleId }
/>
<PostPingbacks />
</PanelRow>
</PanelBody>
);
Expand All @@ -54,16 +38,13 @@ function DiscussionPanel( { pingStatus = 'open', commentStatus = 'open', instanc
export default connect(
( state ) => {
return {
pingStatus: getEditedPostAttribute( state, 'ping_status' ),
commentStatus: getEditedPostAttribute( state, 'comment_status' ),
isOpened: isEditorSidebarPanelOpened( state, PANEL_NAME ),
};
},
{
editPost,
onTogglePanel() {
return toggleSidebarPanel( PANEL_NAME );
},
}
)( withInstanceId( DiscussionPanel ) );
)( DiscussionPanel );