Skip to content

Commit

Permalink
Chrome: Adding a post author drodown
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jul 31, 2017
1 parent 39f459f commit 619de3b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
90 changes: 90 additions & 0 deletions editor/sidebar/post-author/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
import { __ } from 'i18n';
import { PanelRow, withInstanceId } from 'components';
import { Component } from 'element';

/**
* Internal dependencies
*/
import './style.scss';
import { getEditedPostAttribute } from '../../selectors';
import { editPost } from '../../actions';

class PostAuthor extends Component {
constructor() {
super( ...arguments );
this.state = {
authors: [],
};
}

fetchAuthors() {
this.fetchAuthorsRequest = new wp.api.collections.Users().fetch( { data: {
roles: 'author,editor,administrator',
per_page: 100,
} } );
this.fetchAuthorsRequest.then( ( authors ) => {
this.setState( { authors } );
} );
}

componentDidMount() {
this.fetchAuthors();
}

componentWillUnmount() {
if ( this.fetchAuthorsRequest ) {
return this.fetchAuthorsRequest.abort();
}
}

render() {
const { onUpdateAuthor, postAuthor, instanceId } = this.props;
const { authors } = this.state;
const selectId = 'post-author-selector-' + instanceId;

if ( authors.length < 2 ) {
return null;
}

// Disable reason: A select with an onchange throws a warning

/* eslint-disable jsx-a11y/no-onchange */
return (
<PanelRow>
<label htmlFor={ selectId }>{ __( 'Author' ) }</label>
<select
id={ selectId }
value={ postAuthor }
onChange={ ( event ) => onUpdateAuthor( event.target.value ) }
className="editor-post-author__select"
>
{ authors.map( ( author ) => (
<option key={ author.id } value={ author.id }>{ author.name }</option>
) ) }
</select>
</PanelRow>
);
/* eslint-enable jsx-a11y/no-onchange */
}
}

export default connect(
( state ) => {
return {
postAuthor: getEditedPostAttribute( state, 'author' ),
};
},
{
onUpdateAuthor( author ) {
return editPost( { author } );
},
},
)( withInstanceId( PostAuthor ) );
3 changes: 3 additions & 0 deletions editor/sidebar/post-author/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.editor-post-author__select {
margin: -5px 0;
}
2 changes: 2 additions & 0 deletions editor/sidebar/post-status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import PostVisibility from '../post-visibility';
import PostTrash from '../post-trash';
import PostSchedule from '../post-schedule';
import PostSticky from '../post-sticky';
import PostAuthor from '../post-author';
import {
getEditedPostAttribute,
getSuggestedPostFormat,
Expand Down Expand Up @@ -64,6 +65,7 @@ class PostStatus extends Component {
<span>{ format }</span>
</PanelRow>
<PostSticky />
<PostAuthor />
<PostTrash />
</PanelBody>
);
Expand Down

0 comments on commit 619de3b

Please sign in to comment.