-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Editor: Refactor Post Author component #33695
Changes from 8 commits
3779e05
99c19ea
29cd70b
0c18516
3f06b0f
5ce07f2
36e7d2c
c505b46
04c1add
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,36 +6,40 @@ import { debounce } from 'lodash'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useMemo, useEffect } from '@wordpress/element'; | ||
import { useState, useMemo } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { ComboboxControl } from '@wordpress/components'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editorStore } from '../../store'; | ||
import { AUTHORS_QUERY } from './constants'; | ||
|
||
function PostAuthorCombobox() { | ||
const [ fieldValue, setFieldValue ] = useState(); | ||
|
||
const { authorId, isLoading, authors, postAuthor } = useSelect( | ||
( select ) => { | ||
const { __unstableGetAuthor, getAuthors, isResolving } = select( | ||
coreStore | ||
); | ||
const { getUser, getUsers, isResolving } = select( coreStore ); | ||
const { getEditedPostAttribute } = select( editorStore ); | ||
const author = __unstableGetAuthor( | ||
getEditedPostAttribute( 'author' ) | ||
); | ||
const query = | ||
! fieldValue || '' === fieldValue ? {} : { search: fieldValue }; | ||
const author = getUser( getEditedPostAttribute( 'author' ), { | ||
context: 'view', | ||
} ); | ||
const query = { ...AUTHORS_QUERY }; | ||
|
||
if ( fieldValue ) { | ||
query.search = fieldValue; | ||
} | ||
|
||
return { | ||
authorId: getEditedPostAttribute( 'author' ), | ||
postAuthor: author, | ||
authors: getAuthors( query ), | ||
isLoading: isResolving( 'core', 'getAuthors', [ query ] ), | ||
authors: getUsers( query ), | ||
isLoading: isResolving( 'core', 'getUsers', [ query ] ), | ||
}; | ||
}, | ||
[ fieldValue ] | ||
|
@@ -46,7 +50,7 @@ function PostAuthorCombobox() { | |
const fetchedAuthors = ( authors ?? [] ).map( ( author ) => { | ||
return { | ||
value: author.id, | ||
label: author.name, | ||
label: decodeEntities( author.name ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added missing entity decoding. We were already doing this for select control. |
||
}; | ||
} ); | ||
|
||
|
@@ -57,22 +61,17 @@ function PostAuthorCombobox() { | |
|
||
if ( foundAuthor < 0 && postAuthor ) { | ||
return [ | ||
{ value: postAuthor.id, label: postAuthor.name }, | ||
{ | ||
value: postAuthor.id, | ||
label: decodeEntities( postAuthor.name ), | ||
}, | ||
...fetchedAuthors, | ||
]; | ||
} | ||
|
||
return fetchedAuthors; | ||
}, [ authors, postAuthor ] ); | ||
|
||
// Initializes the post author properly | ||
// Also ensures external changes are reflected. | ||
useEffect( () => { | ||
if ( postAuthor ) { | ||
setFieldValue( postAuthor.name ); | ||
} | ||
}, [ postAuthor ] ); | ||
|
||
Comment on lines
-68
to
-75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't track down why this effect was added precisely, but I don't think it's needed anymore. It also initializes an unnecessary search query on the mount. |
||
/** | ||
* Handle author selection. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const AUTHORS_QUERY = { | ||
who: 'authors', | ||
per_page: 50, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed the "per_page" argument from 100 to 50. However, I don't think this number makes a big difference when used with Happy to revert it if needed. |
||
_fields: 'id,name', | ||
context: 'view', // Allows non-admins to perform requests. | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We weren't using this prop in
PostAuthorCheck.