Skip to content

Commit

Permalink
Refactor PostAuthorSelect as functional component.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsilverstein committed Dec 30, 2020
1 parent 420e0a5 commit 5ebf486
Showing 1 changed file with 28 additions and 44 deletions.
72 changes: 28 additions & 44 deletions packages/editor/src/components/post-author/post-author-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,39 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/compose';
import { Component } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import { SelectControl } from '@wordpress/components';

export class PostAuthorSelect extends Component {
constructor() {
super( ...arguments );

this.setAuthorId = this.setAuthorId.bind( this );
}

setAuthorId( value ) {
const { onUpdateAuthor } = this.props;
onUpdateAuthor( Number( value ) );
}

render() {
const { postAuthor, authors } = this.props;
const authorOptions = authors.map( ( author ) => ( {
label: decodeEntities( author.name ),
value: author.id,
} ) );

return (
<SelectControl
className="post-author-selector"
label={ __( 'Author' ) }
options={ authorOptions }
onChange={ this.setAuthorId }
value={ postAuthor }
/>
);
}
}

export default compose( [
withSelect( ( select ) => {
function PostAuthorSelect() {
const { editPost } = useDispatch( 'core/editor' );
const { postAuthor, authors } = useSelect( ( select ) => {
const authorsFromAPI = select( 'core' ).getAuthors();
return {
postAuthor: select( 'core/editor' ).getEditedPostAttribute(
'author'
),
authors: select( 'core' ).getAuthors(),
authors: authorsFromAPI.map( ( author ) => ( {
label: decodeEntities( author.name ),
value: author.id,
} ) ),
};
} ),
withDispatch( ( dispatch ) => ( {
onUpdateAuthor( author ) {
dispatch( 'core/editor' ).editPost( { author } );
},
} ) ),
] )( PostAuthorSelect );
} );

const setAuthorId = ( value ) => {
const author = Number( value );
editPost( { author } );
};

return (
<SelectControl
className="post-author-selector"
label={ __( 'Author' ) }
options={ authors }
onChange={ setAuthorId }
value={ postAuthor }
/>
);
}

export default PostAuthorSelect;

0 comments on commit 5ebf486

Please sign in to comment.