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

Editor: Add loading state to the 'PageAttributesParent' component #69062

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 55 additions & 38 deletions packages/editor/src/components/page-attributes/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) => {
Expand Down Expand Up @@ -187,6 +203,7 @@ export function PageAttributesParent() {
onFilterValueChange={ debounce( handleKeydown, 300 ) }
onChange={ handleChange }
hideLabelFromVision
isLoading={ isLoading }
/>
);
}
Expand Down
Loading