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

[Block Library - Query Loop]: Fix broken preview in specific category template #44294

Merged
merged 1 commit into from
Sep 20, 2022
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
37 changes: 14 additions & 23 deletions packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { Spinner } from '@wordpress/components';
import { store as coreStore, useEntityRecords } from '@wordpress/core-data';
import { store as coreStore } from '@wordpress/core-data';

const TEMPLATE = [
[ 'core/post-title' ],
Expand Down Expand Up @@ -101,19 +101,6 @@ export default function PostTemplateEdit( {
} ) {
const [ { page } ] = queryContext;
const [ activeBlockContextId, setActiveBlockContextId ] = useState();

let categorySlug = null;
if ( templateSlug?.startsWith( 'category-' ) ) {
categorySlug = templateSlug.replace( 'category-', '' );
}
const { records: categories, hasResolved: hasResolvedCategories } =
useEntityRecords( 'taxonomy', 'category', {
context: 'view',
per_page: -1,
_fields: [ 'id' ],
slug: categorySlug,
} );

const { posts, blocks } = useSelect(
( select ) => {
const { getEntityRecords, getTaxonomies } = select( coreStore );
Expand All @@ -123,12 +110,22 @@ export default function PostTemplateEdit( {
per_page: -1,
context: 'view',
} );
const templateCategory =
inherit &&
templateSlug?.startsWith( 'category-' ) &&
getEntityRecords( 'taxonomy', 'category', {
context: 'view',
per_page: 1,
Copy link
Member

Choose a reason for hiding this comment

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

Nice 🚀

_fields: [ 'id' ],
slug: templateSlug.replace( 'category-', '' ),
} );
const query = {
offset: perPage ? perPage * ( page - 1 ) + offset : 0,
order,
orderby: orderBy,
};
if ( taxQuery ) {
// There is no need to build the taxQuery if we inherit.
if ( taxQuery && ! inherit ) {
Copy link
Member

Choose a reason for hiding this comment

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

It is a general improvement and not related to this issue. Am I correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're correct, it become more obvious to me after checking this issue. We could probably skip some other args if we inherit, like search etc..

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense.

// We have to build the tax query for the REST API and use as
// keys the taxonomies `rest_base` with the `term ids` as values.
const builtTaxQuery = Object.entries( taxQuery ).reduce(
Expand Down Expand Up @@ -174,11 +171,8 @@ export default function PostTemplateEdit( {
if ( templateSlug?.startsWith( 'archive-' ) ) {
query.postType = templateSlug.replace( 'archive-', '' );
postType = query.postType;
} else if ( !! categorySlug && hasResolvedCategories ) {
query.taxQuery = {
category: categories.map( ( { id } ) => id ),
};
taxQuery = query.taxQuery;
} else if ( templateCategory ) {
query.categories = templateCategory[ 0 ]?.id;
}
}
// When we preview Query Loop blocks we should prefer the current
Expand Down Expand Up @@ -210,9 +204,6 @@ export default function PostTemplateEdit( {
parents,
restQueryArgs,
previewPostType,
categories,
categorySlug,
hasResolvedCategories,
]
);
const blockContexts = useMemo(
Expand Down