diff --git a/editor/sidebar/post-author/index.js b/editor/sidebar/post-author/index.js index c62ba0a70f90e7..43f3c126637ef9 100644 --- a/editor/sidebar/post-author/index.js +++ b/editor/sidebar/post-author/index.js @@ -2,12 +2,13 @@ * External dependencies */ import { connect } from 'react-redux'; +import { filter, flowRight } from 'lodash'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { PanelRow, withInstanceId } from '@wordpress/components'; +import { PanelRow, withAPIData, withInstanceId } from '@wordpress/components'; import { Component } from '@wordpress/element'; /** @@ -17,40 +18,40 @@ import './style.scss'; import { getEditedPostAttribute } from '../../selectors'; import { editPost } from '../../actions'; -class PostAuthor extends Component { +export class PostAuthor extends Component { constructor() { super( ...arguments ); - this.state = { - authors: [], - }; - } - fetchAuthors() { - this.fetchAuthorsRequest = new wp.api.collections.Users().fetch( { data: { - per_page: 100, - } } ); - this.fetchAuthorsRequest.then( ( authors ) => { - this.setState( { authors } ); - } ); + this.setAuthorId = this.setAuthorId.bind( this ); } - componentDidMount() { - this.fetchAuthors(); + setAuthorId( event ) { + const { onUpdateAuthor } = this.props; + const { value } = event.target; + onUpdateAuthor( Number( value ) ); } - componentWillUnmount() { - this.fetchAuthorsRequest.abort(); + getAuthors() { + // While User Levels are officially deprecated, the behavior of the + // existing users dropdown on `who=authors` tests `user_level != 0` + // + // See: https://github.com/WordPress/WordPress/blob/a193916/wp-includes/class-wp-user-query.php#L322-L327 + // See: https://codex.wordpress.org/Roles_and_Capabilities#User_Levels + const { users } = this.props; + return filter( users.data, ( user ) => { + return user.capabilities.level_1; + } ); } render() { - const { onUpdateAuthor, postAuthor, instanceId } = this.props; - const { authors } = this.state; - const selectId = 'post-author-selector-' + instanceId; - + const authors = this.getAuthors(); if ( authors.length < 2 ) { return null; } + const { postAuthor, instanceId } = this.props; + const selectId = 'post-author-selector-' + instanceId; + // Disable reason: A select with an onchange throws a warning /* eslint-disable jsx-a11y/no-onchange */ @@ -60,7 +61,7 @@ class PostAuthor extends Component {