From ceb88fa467b65ae2e3ce16417b36d3f67665e6f7 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 11 Feb 2025 16:39:23 +0300 Subject: [PATCH] Editor: Add loading state to the 'PageAttributesParent' component (#69062) Unlinked contributors: mapk, ItsJonQ, MichaelArestad. Co-authored-by: Mamaduka Co-authored-by: karmatosed Co-authored-by: ndiego Co-authored-by: mirka <0mirka00@git.wordpress.org> Co-authored-by: iandunn --- .../src/components/page-attributes/parent.js | 93 +++++++++++-------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/packages/editor/src/components/page-attributes/parent.js b/packages/editor/src/components/page-attributes/parent.js index bd2861766c334a..56381f448abc4c 100644 --- a/packages/editor/src/components/page-attributes/parent.js +++ b/packages/editor/src/components/page-attributes/parent.js @@ -61,47 +61,63 @@ export const getItemPriority = ( name, searchValue ) => { export function PageAttributesParent() { const { editPost } = useDispatch( editorStore ); const [ fieldValue, setFieldValue ] = useState( false ); - const { isHierarchical, parentPostId, parentPostTitle, pageItems } = - useSelect( - ( select ) => { - const { getPostType, getEntityRecords, getEntityRecord } = - select( coreStore ); - const { getCurrentPostId, getEditedPostAttribute } = - select( editorStore ); - const postTypeSlug = getEditedPostAttribute( 'type' ); - const pageId = getEditedPostAttribute( 'parent' ); - const pType = getPostType( postTypeSlug ); - const postId = getCurrentPostId(); - const postIsHierarchical = pType?.hierarchical ?? false; - const query = { - per_page: 100, - exclude: postId, - parent_exclude: postId, - orderby: 'menu_order', - order: 'asc', - _fields: 'id,title,parent', - }; + const { + isHierarchical, + parentPostId, + parentPostTitle, + pageItems, + isLoading, + } = useSelect( + ( select ) => { + const { + getPostType, + getEntityRecords, + getEntityRecord, + isResolving, + } = select( coreStore ); + const { getCurrentPostId, getEditedPostAttribute } = + select( editorStore ); + const postTypeSlug = getEditedPostAttribute( 'type' ); + const pageId = getEditedPostAttribute( 'parent' ); + const pType = getPostType( postTypeSlug ); + const postId = getCurrentPostId(); + const postIsHierarchical = pType?.hierarchical ?? false; + const query = { + per_page: 100, + exclude: postId, + parent_exclude: postId, + orderby: 'menu_order', + order: 'asc', + _fields: 'id,title,parent', + }; - // Perform a search when the field is changed. - if ( !! fieldValue ) { - query.search = fieldValue; - } + // Perform a search when the field is changed. + if ( !! fieldValue ) { + query.search = fieldValue; + } - const parentPost = pageId - ? getEntityRecord( 'postType', postTypeSlug, pageId ) - : null; + const parentPost = pageId + ? getEntityRecord( 'postType', postTypeSlug, pageId ) + : null; - return { - isHierarchical: postIsHierarchical, - parentPostId: pageId, - parentPostTitle: parentPost ? getTitle( parentPost ) : '', - pageItems: postIsHierarchical - ? getEntityRecords( 'postType', postTypeSlug, query ) - : null, - }; - }, - [ fieldValue ] - ); + return { + isHierarchical: postIsHierarchical, + parentPostId: pageId, + parentPostTitle: parentPost ? getTitle( parentPost ) : '', + pageItems: postIsHierarchical + ? getEntityRecords( 'postType', postTypeSlug, query ) + : null, + isLoading: postIsHierarchical + ? isResolving( 'getEntityRecords', [ + 'postType', + postTypeSlug, + query, + ] ) + : false, + }; + }, + [ fieldValue ] + ); const parentOptions = useMemo( () => { const getOptionsFromTree = ( tree, level = 0 ) => { @@ -187,6 +203,7 @@ export function PageAttributesParent() { onFilterValueChange={ debounce( handleKeydown, 300 ) } onChange={ handleChange } hideLabelFromVision + isLoading={ isLoading } /> ); }