Skip to content

Commit

Permalink
Editor: Don't use selector shortcuts for the taxonomy queries (WordPr…
Browse files Browse the repository at this point in the history
…ess#68998)

* Editor: Don't use selector shortcuts for the taxonomy queries
* Update unit tests

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: tyxla <[email protected]>
  • Loading branch information
3 people authored and Kallyan01 committed Feb 24, 2025
1 parent 7ac1be7 commit ca74f4e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import { store as editorStore } from '../../store';
function MaybeCategoryPanel() {
const hasNoCategory = useSelect( ( select ) => {
const postType = select( editorStore ).getCurrentPostType();
const { canUser, getEntityRecord, getTaxonomy } = select( coreStore );
const categoriesTaxonomy = getTaxonomy( 'category' );
const { canUser, getEntityRecord } = select( coreStore );
const categoriesTaxonomy = getEntityRecord(
'root',
'taxonomy',
'category'
);
const defaultCategoryId = canUser( 'read', {
kind: 'root',
name: 'site',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ const TagsPanel = () => {
const MaybeTagsPanel = () => {
const { hasTags, isPostTypeSupported } = useSelect( ( select ) => {
const postType = select( editorStore ).getCurrentPostType();
const tagsTaxonomy = select( coreStore ).getTaxonomy( 'post_tag' );
const tagsTaxonomy = select( coreStore ).getEntityRecord(
'root',
'taxonomy',
'post_tag'
);
const _isPostTypeSupported = tagsTaxonomy?.types?.includes( postType );
const areTagsFetched = tagsTaxonomy !== undefined;
const tags =
Expand Down
8 changes: 5 additions & 3 deletions packages/editor/src/components/post-taxonomies/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import { store as editorStore } from '../../store';
export default function PostTaxonomiesCheck( { children } ) {
const hasTaxonomies = useSelect( ( select ) => {
const postType = select( editorStore ).getCurrentPostType();
const taxonomies = select( coreStore ).getTaxonomies( {
per_page: -1,
} );
const taxonomies = select( coreStore ).getEntityRecords(
'root',
'taxonomy',
{ per_page: -1 }
);
return taxonomies?.some( ( taxonomy ) =>
taxonomy.types.includes( postType )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ export function FlatTermSelector( { slug, __nextHasNoMarginBottom } ) {
( select ) => {
const { getCurrentPost, getEditedPostAttribute } =
select( editorStore );
const { getEntityRecords, getTaxonomy, hasFinishedResolution } =
const { getEntityRecords, getEntityRecord, hasFinishedResolution } =
select( coreStore );
const post = getCurrentPost();
const _taxonomy = getTaxonomy( slug );
const _taxonomy = getEntityRecord( 'root', 'taxonomy', slug );
const _termIds = _taxonomy
? getEditedPostAttribute( _taxonomy.rest_base )
: EMPTY_ARRAY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ export function HierarchicalTermSelector( { slug } ) {
( select ) => {
const { getCurrentPost, getEditedPostAttribute } =
select( editorStore );
const { getTaxonomy, getEntityRecords, isResolving } =
const { getEntityRecord, getEntityRecords, isResolving } =
select( coreStore );
const _taxonomy = getTaxonomy( slug );
const _taxonomy = getEntityRecord( 'root', 'taxonomy', slug );
const post = getCurrentPost();

return {
Expand Down
6 changes: 5 additions & 1 deletion packages/editor/src/components/post-taxonomies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export function PostTaxonomies( { taxonomyWrapper = identity } ) {
const { postType, taxonomies } = useSelect( ( select ) => {
return {
postType: select( editorStore ).getCurrentPostType(),
taxonomies: select( coreStore ).getTaxonomies( { per_page: -1 } ),
taxonomies: select( coreStore ).getEntityRecords(
'root',
'taxonomy',
{ per_page: -1 }
),
};
}, [] );
const visibleTaxonomies = ( taxonomies ?? [] ).filter(
Expand Down
50 changes: 32 additions & 18 deletions packages/editor/src/components/post-taxonomies/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ describe( 'PostTaxonomies', () => {
},
};

const allTaxonomies = [ genresTaxonomy, categoriesTaxonomy ];

const hidesUI = [
genresTaxonomy,
{
...categoriesTaxonomy,
types: [ 'post', 'page', 'book' ],
visibility: {
show_ui: false,
},
},
];

beforeEach( () => {
jest.spyOn( select( editorStore ), 'getCurrentPost' ).mockReturnValue( {
_links: {
Expand All @@ -70,8 +83,8 @@ describe( 'PostTaxonomies', () => {
},
} );

jest.spyOn( select( coreStore ), 'getTaxonomy' ).mockImplementation(
( slug ) => {
jest.spyOn( select( coreStore ), 'getEntityRecord' ).mockImplementation(
( kind, name, slug ) => {
switch ( slug ) {
case 'category': {
return categoriesTaxonomy;
Expand All @@ -91,8 +104,11 @@ describe( 'PostTaxonomies', () => {
select( editorStore ),
'getCurrentPostType'
).mockReturnValue( 'page' );
jest.spyOn( select( coreStore ), 'getTaxonomies' ).mockReturnValue(
taxonomies
jest.spyOn(
select( coreStore ),
'getEntityRecords'
).mockImplementation( ( kind, name ) =>
kind === 'root' && name === 'taxonomy' ? taxonomies : null
);

const { container } = render( <PostTaxonomies /> );
Expand All @@ -105,10 +121,12 @@ describe( 'PostTaxonomies', () => {
select( editorStore ),
'getCurrentPostType'
).mockReturnValue( 'book' );
jest.spyOn( select( coreStore ), 'getTaxonomies' ).mockReturnValue( [
genresTaxonomy,
categoriesTaxonomy,
] );
jest.spyOn(
select( coreStore ),
'getEntityRecords'
).mockImplementation( ( kind, name ) =>
kind === 'root' && name === 'taxonomy' ? allTaxonomies : null
);

render( <PostTaxonomies /> );

Expand All @@ -129,16 +147,12 @@ describe( 'PostTaxonomies', () => {
select( editorStore ),
'getCurrentPostType'
).mockReturnValue( 'book' );
jest.spyOn( select( coreStore ), 'getTaxonomies' ).mockReturnValue( [
genresTaxonomy,
{
...categoriesTaxonomy,
types: [ 'post', 'page', 'book' ],
visibility: {
show_ui: false,
},
},
] );
jest.spyOn(
select( coreStore ),
'getEntityRecords'
).mockImplementation( ( kind, name ) =>
kind === 'root' && name === 'taxonomy' ? hidesUI : null
);

render( <PostTaxonomies /> );

Expand Down

0 comments on commit ca74f4e

Please sign in to comment.