-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Reuse data query in the post author components (#58760)
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
- Loading branch information
1 parent
8f8c4d7
commit 180b57a
Showing
4 changed files
with
77 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
export const BASE_QUERY = { | ||
_fields: 'id,name', | ||
context: 'view', // Allows non-admins to perform requests. | ||
}; | ||
|
||
export const AUTHORS_QUERY = { | ||
who: 'authors', | ||
per_page: 50, | ||
_fields: 'id,name', | ||
context: 'view', // Allows non-admins to perform requests. | ||
...BASE_QUERY, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
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, BASE_QUERY } from './constants'; | ||
|
||
export function useAuthorsQuery( search ) { | ||
const { authorId, authors, postAuthor } = useSelect( | ||
( select ) => { | ||
const { getUser, getUsers } = select( coreStore ); | ||
const { getEditedPostAttribute } = select( editorStore ); | ||
const _authorId = getEditedPostAttribute( 'author' ); | ||
const query = { ...AUTHORS_QUERY }; | ||
|
||
if ( search ) { | ||
query.search = search; | ||
} | ||
|
||
return { | ||
authorId: _authorId, | ||
authors: getUsers( query ), | ||
postAuthor: getUser( _authorId, BASE_QUERY ), | ||
}; | ||
}, | ||
[ search ] | ||
); | ||
|
||
const authorOptions = useMemo( () => { | ||
const fetchedAuthors = ( authors ?? [] ).map( ( author ) => { | ||
return { | ||
value: author.id, | ||
label: decodeEntities( author.name ), | ||
}; | ||
} ); | ||
|
||
// Ensure the current author is included in the dropdown list. | ||
const foundAuthor = fetchedAuthors.findIndex( | ||
( { value } ) => postAuthor?.id === value | ||
); | ||
|
||
if ( foundAuthor < 0 && postAuthor ) { | ||
return [ | ||
{ | ||
value: postAuthor.id, | ||
label: decodeEntities( postAuthor.name ), | ||
}, | ||
...fetchedAuthors, | ||
]; | ||
} | ||
|
||
return fetchedAuthors; | ||
}, [ authors, postAuthor ] ); | ||
|
||
return { authorId, authorOptions }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180b57a
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.
Flaky tests detected in 180b57a.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7812912387
📝 Reported issues:
/test/e2e/specs/interactivity/directive-bind.spec.ts