Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 7 additions & 18 deletions packages/block-library/src/term-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default function TermTemplateEdit( {
hideEmpty,
showNested = false,
parent = 0,
perPage = 10,
perPage,
} = {},
},
__unstableLayoutClassNames,
Expand All @@ -93,10 +93,7 @@ export default function TermTemplateEdit( {
hide_empty: hideEmpty,
order,
orderby: orderBy,
// To preview the data the closest to the frontend, we fetch the largest number of terms
// and limit them during rendering. This allows us to avoid re-fetching data when max
// terms changes.
per_page: 100,
per_page: perPage,
};

// Nested terms are returned by default from REST API as long as parent is not set.
Expand All @@ -105,20 +102,12 @@ export default function TermTemplateEdit( {
queryArgs.parent = parent || 0;
}

const { records: terms, isResolving } = useEntityRecords(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there is a mismatch internally with isResolving value and while we had the terms, isResolving was still true (needs separate investigation). That resulted in a brief loading state when it wasn't needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there is a mismatch internally with isResolving value and while we had the terms, isResolving was still true (needs separate investigation).

I've run into this same issue in the past. What about using hasResolved instead? The current PR works fine, but I think checking hasResolved is more accurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to simplify/tidy more things around this block and would rather explore this in a follow up due to the 6.9 deadlines. The root cause of useEntityRecords should be investigated first though.

const { records: terms } = useEntityRecords(
'taxonomy',
taxonomy,
queryArgs
);

const filteredTerms = useMemo( () => {
if ( ! terms ) {
return [];
}
// Limit to the number of terms defined by perPage.
return perPage === 0 ? terms : terms.slice( 0, perPage );
}, [ terms, perPage ] );

const blocks = useSelect(
( select ) => select( blockEditorStore ).getBlocks( clientId ),
[ clientId ]
Expand All @@ -128,16 +117,16 @@ export default function TermTemplateEdit( {
} );
const blockContexts = useMemo(
() =>
filteredTerms?.map( ( term ) => ( {
terms?.map( ( term ) => ( {
taxonomy,
termId: term.id,
classList: `term-${ term.id }`,
termData: term,
} ) ),
[ filteredTerms, taxonomy ]
[ terms, taxonomy ]
);

if ( isResolving ) {
if ( ! terms ) {
return (
<ul { ...blockProps }>
<li className="wp-block-term term-loading">
Expand All @@ -147,7 +136,7 @@ export default function TermTemplateEdit( {
);
}

if ( ! filteredTerms?.length ) {
if ( ! terms.length ) {
return <p { ...blockProps }> { __( 'No terms found.' ) }</p>;
}

Expand Down
10 changes: 5 additions & 5 deletions packages/block-library/src/term-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function render_block_core_term_template( $attributes, $content, $block ) {
$query = $query_block_context['termQuery'];

$query_args = array(
'taxonomy' => $query['taxonomy'] ?? 'category',
'number' => $query['perPage'] ?? 10,
'order' => $query['order'] ?? 'asc',
'orderby' => $query['orderBy'] ?? 'name',
'hide_empty' => $query['hideEmpty'] ?? true,
'taxonomy' => $query['taxonomy'],
'number' => $query['perPage'],
'order' => $query['order'],
'orderby' => $query['orderBy'],
'hide_empty' => $query['hideEmpty'],
);

// We set parent only when inheriting from the taxonomy archive context or not
Expand Down
Loading