Skip to content

Commit

Permalink
Fix the Post author selector for contributors (#26554)
Browse files Browse the repository at this point in the history
Co-authored-by: Riad Benguella <[email protected]>
  • Loading branch information
2 people authored and tellthemachines committed Nov 30, 2020
1 parent d87df93 commit 9896f96
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/designers-developers/developers/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Returns all available authors.
_Parameters_

- _state_ `Object`: Data state.
- _query_ `(Object|undefined)`: Optional object of query parameters to include with request.

_Returns_

Expand Down
1 change: 1 addition & 0 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ Returns all available authors.
_Parameters_

- _state_ `Object`: Data state.
- _query_ `(Object|undefined)`: Optional object of query parameters to include with request.

_Returns_

Expand Down
26 changes: 21 additions & 5 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,28 @@ import { ifNotResolved, getNormalizedCommaSeparable } from './utils';

/**
* Requests authors from the REST API.
*
* @param {Object|undefined} query Optional object of query parameters to
* include with request.
*/
export function* getAuthors() {
const users = yield apiFetch( {
path: '/wp/v2/users/?who=authors&per_page=-1',
} );
yield receiveUserQuery( 'authors', users );
export function* getAuthors( query ) {
const path = addQueryArgs(
'/wp/v2/users/?who=authors&per_page=100',
query
);
const users = yield apiFetch( { path } );
yield receiveUserQuery( path, users );
}

/**
* Temporary approach to resolving editor access to author queries.
*
* @param {number} id The author id.
*/
export function* __unstableGetAuthor( id ) {
const path = `/wp/v2/users?who=authors&include=${ id }`;
const users = yield apiFetch( { path } );
yield receiveUserQuery( 'author', users );
}

/**
Expand Down
25 changes: 20 additions & 5 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { set, map, find, get, filter, compact, defaultTo } from 'lodash';
*/
import { createRegistrySelector } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
Expand Down Expand Up @@ -37,18 +38,32 @@ export const isRequestingEmbedPreview = createRegistrySelector(
}
);

/**
* Returns all available authors.
*
* @param {Object} state Data state.
* @param {Object|undefined} query Optional object of query parameters to
* include with request.
* @return {Array} Authors list.
*/
export function getAuthors( state, query ) {
const path = addQueryArgs(
'/wp/v2/users/?who=authors&per_page=100',
query
);
return getUserQueryResults( state, path );
}

/**
* Returns all available authors.
*
* @param {Object} state Data state.
* @param {number} id The author id.
*
* @return {Array} Authors list.
*/
export function getAuthors( state ) {
deprecated( "select( 'core' ).getAuthors()", {
alternative: "select( 'core' ).getUsers({ who: 'authors' })",
} );
return getUserQueryResults( state, 'authors' );
export function __unstableGetAuthor( state, id ) {
return get( state, [ 'users', 'byId', id ], null );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/post-author/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function PostAuthorCheck( {
authors,
children,
} ) {
if ( ! hasAssignAuthorAction || ! authors || authors.length < 2 ) {
if ( ! hasAssignAuthorAction || ! authors || 1 >= authors.length ) {
return null;
}

Expand All @@ -40,7 +40,7 @@ export default compose( [
false
),
postType: select( 'core/editor' ).getCurrentPostType(),
authors: select( 'core' ).getUsers( { who: 'authors' } ),
authors: select( 'core' ).getAuthors(),
};
} ),
withInstanceId,
Expand Down
14 changes: 8 additions & 6 deletions packages/editor/src/components/post-author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ function PostAuthor() {

const { authorId, isLoading, authors, postAuthor } = useSelect(
( select ) => {
const { getUser, getUsers, isResolving } = select( 'core' );
const { __unstableGetAuthor, getAuthors, isResolving } = select(
'core'
);
const { getEditedPostAttribute } = select( 'core/editor' );
const author = getUser( getEditedPostAttribute( 'author' ) );
const author = __unstableGetAuthor(
getEditedPostAttribute( 'author' )
);
const query =
! fieldValue || '' === fieldValue ? {} : { search: fieldValue };
return {
authorId: getEditedPostAttribute( 'author' ),
postAuthor: author,
authors: getUsers( { who: 'authors', ...query } ),
isLoading: isResolving( 'core', 'getUsers', [
{ search: fieldValue, who: 'authors' },
] ),
authors: getAuthors( query ),
isLoading: isResolving( 'core', 'getAuthors', [ query ] ),
};
},
[ fieldValue ]
Expand Down

0 comments on commit 9896f96

Please sign in to comment.