-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[Block Library - Query Loop]: Add parents filter
#40933
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc5cffa
[Block Library - Query Loop]: Add `parents` filter
ntsekouras bd5d573
move `isPostTypeHierarchical` in inspector controls component + reset…
ntsekouras 76417f9
orderBy relevance
ntsekouras 26e3eda
address feedback
ntsekouras b36f639
feedback
ntsekouras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
133 changes: 133 additions & 0 deletions
133
packages/block-library/src/query/edit/inspector-controls/parent-control.js
This file contains hidden or 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,133 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { FormTokenField } from '@wordpress/components'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
| import { useState, useEffect, useMemo } from '@wordpress/element'; | ||
| import { useDebounce } from '@wordpress/compose'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { getEntitiesInfo, mapToIHasNameAndId } from '../../utils'; | ||
|
|
||
| const EMPTY_ARRAY = []; | ||
| const BASE_QUERY = { | ||
| order: 'asc', | ||
| _fields: 'id,title', | ||
| context: 'view', | ||
| }; | ||
|
|
||
| function ParentControl( { parents, postType, onChange } ) { | ||
| const [ search, setSearch ] = useState( '' ); | ||
| const [ value, setValue ] = useState( EMPTY_ARRAY ); | ||
| const [ suggestions, setSuggestions ] = useState( EMPTY_ARRAY ); | ||
| const debouncedSearch = useDebounce( setSearch, 250 ); | ||
| const { searchResults, searchHasResolved } = useSelect( | ||
| ( select ) => { | ||
| if ( ! search ) { | ||
| return { searchResults: EMPTY_ARRAY, searchHasResolved: true }; | ||
| } | ||
| const { getEntityRecords, hasFinishedResolution } = select( | ||
| coreStore | ||
| ); | ||
| const selectorArgs = [ | ||
| 'postType', | ||
| postType, | ||
| { | ||
| ...BASE_QUERY, | ||
| search, | ||
| orderby: 'relevance', | ||
| exclude: parents, | ||
| per_page: 20, | ||
| }, | ||
| ]; | ||
| return { | ||
| searchResults: getEntityRecords( ...selectorArgs ), | ||
| searchHasResolved: hasFinishedResolution( | ||
| 'getEntityRecords', | ||
| selectorArgs | ||
| ), | ||
| }; | ||
| }, | ||
| [ search, parents ] | ||
| ); | ||
| const currentParents = useSelect( | ||
| ( select ) => { | ||
| if ( ! parents?.length ) return EMPTY_ARRAY; | ||
| const { getEntityRecords } = select( coreStore ); | ||
| return getEntityRecords( 'postType', postType, { | ||
| ...BASE_QUERY, | ||
| include: parents, | ||
| per_page: parents.length, | ||
| } ); | ||
| }, | ||
| [ parents ] | ||
| ); | ||
| // Update the `value` state only after the selectors are resolved | ||
| // to avoid emptying the input when we're changing parents. | ||
| useEffect( () => { | ||
| if ( ! parents?.length ) { | ||
| setValue( EMPTY_ARRAY ); | ||
| } | ||
| if ( ! currentParents?.length ) return; | ||
| const currentParentsInfo = getEntitiesInfo( | ||
| mapToIHasNameAndId( currentParents, 'title.rendered' ) | ||
| ); | ||
| // Returns only the existing entity ids. This prevents the component | ||
| // from crashing in the editor, when non existing ids are provided. | ||
| const sanitizedValue = parents.reduce( ( accumulator, id ) => { | ||
| const entity = currentParentsInfo.mapById[ id ]; | ||
| if ( entity ) { | ||
| accumulator.push( { | ||
| id, | ||
| value: entity.name, | ||
| } ); | ||
| } | ||
| return accumulator; | ||
| }, [] ); | ||
| setValue( sanitizedValue ); | ||
| }, [ parents, currentParents ] ); | ||
|
|
||
| const entitiesInfo = useMemo( () => { | ||
| if ( ! searchResults?.length ) return EMPTY_ARRAY; | ||
| return getEntitiesInfo( | ||
| mapToIHasNameAndId( searchResults, 'title.rendered' ) | ||
| ); | ||
| }, [ searchResults ] ); | ||
| // Update suggestions only when the query has resolved. | ||
| useEffect( () => { | ||
| if ( ! searchHasResolved ) return; | ||
| setSuggestions( entitiesInfo.names ); | ||
| }, [ entitiesInfo.names, searchHasResolved ] ); | ||
|
|
||
| const getIdByValue = ( entitiesMappedByName, entity ) => { | ||
| const id = entity?.id || entitiesMappedByName?.[ entity ]?.id; | ||
| if ( id ) return id; | ||
| }; | ||
| const onParentChange = ( newValue ) => { | ||
| const ids = Array.from( | ||
| newValue.reduce( ( accumulator, entity ) => { | ||
| // Verify that new values point to existing entities. | ||
| const id = getIdByValue( entitiesInfo.mapByName, entity ); | ||
| if ( id ) accumulator.add( id ); | ||
| return accumulator; | ||
| }, new Set() ) | ||
| ); | ||
| setSuggestions( EMPTY_ARRAY ); | ||
| onChange( { parents: ids } ); | ||
| }; | ||
| return ( | ||
| <FormTokenField | ||
| label={ __( 'Parents' ) } | ||
| value={ value } | ||
| onInputChange={ debouncedSearch } | ||
| suggestions={ suggestions } | ||
| onChange={ onParentChange } | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export default ParentControl; | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.