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

Switch back to getAuthors #26554

Merged
merged 11 commits into from
Nov 19, 2020
19 changes: 17 additions & 2 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,29 @@ 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() {
export function* getAuthors( query ) {
const users = yield apiFetch( {
path: '/wp/v2/users/?who=authors&per_page=-1',
path: addQueryArgs( '/wp/v2/users/?who=authors&per_page=100', query ),
} );
yield receiveUserQuery( 'authors', users );
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the first argument here be "authors" to match the same entity as "getAuthors"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? I think that results in the fetched author overwriting the authors? I can test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested changing this and confirmed using the same key results in the query interfering with the getAuthors query, causing errors or unexpected results as the results from the two queries overwrite each other.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, so if I'm reading this right getAuthor(1) and getAuthor(2) are using the same query key which means if I trigger both concurrently, they'll mess with each other's result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowriad - Oh, good point. I didn't think about concurrent requests. Should we build a dynamic key here that includes the requested id? Do we have an existing component that uses this approach/a helper to create the query key?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I did add dynamic query keys for both getAuthor and getAuthors, I think it will solve both issues so I reverted the change you did with useMemo

}

/**
* Requests the current user from the REST API.
*/
Expand Down
15 changes: 12 additions & 3 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,21 @@ export const isRequestingEmbedPreview = createRegistrySelector(
* @return {Array} Authors list.
*/
export function getAuthors( state ) {
deprecated( "select( 'core' ).getAuthors()", {
alternative: "select( 'core' ).getUsers({ who: 'authors' })",
} );
return getUserQueryResults( state, 'authors' );
}

/**
* Returns all available authors.
*
* @param {Object} state Data state.
*
* @return {Array} Authors list.
*/
export function __unstableGetAuthor( state ) {
const authors = getUserQueryResults( state, 'author' );
return authors ? authors[ 0 ] : [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should probably return null if the author was not found.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will change.

}

/**
* Returns the current user.
*
Expand Down
11 changes: 9 additions & 2 deletions packages/editor/src/components/post-author/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { get } from 'lodash';
*/
import { withInstanceId, compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -18,8 +19,13 @@ export function PostAuthorCheck( {
hasAssignAuthorAction,
authors,
children,
post,
} ) {
if ( ! hasAssignAuthorAction || ! authors || authors.length < 2 ) {
const initialAuthors = useMemo( () => {
return authors;
}, [ post ] );

if ( ! hasAssignAuthorAction || ! authors || 1 >= initialAuthors.length ) {
return null;
}

Expand All @@ -40,7 +46,8 @@ export default compose( [
false
),
postType: select( 'core/editor' ).getCurrentPostType(),
authors: select( 'core' ).getUsers( { who: 'authors' } ),
authors: select( 'core' ).getAuthors(),
post,
};
} ),
withInstanceId,
Expand Down
18 changes: 12 additions & 6 deletions packages/editor/src/components/post-author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ 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 =
postAuthor ||
__unstableGetAuthor( getEditedPostAttribute( 'author' ) );
const query =
! fieldValue || '' === fieldValue ? {} : { search: fieldValue };
! fieldValue || '' === fieldValue || fieldValue === author.name
? {}
: { 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', [
{ search: fieldValue },
] ),
};
},
Expand Down