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 categories - ensure that categories are unique by slug. #62954

Merged
merged 3 commits into from
Aug 2, 2024

Conversation

Chrico
Copy link
Contributor

@Chrico Chrico commented Jun 28, 2024

Fixes #50061

What?

Block categories can be registered through backend by hooking into the "block_categories_all"-filter or through frontend by calling wp.blocks.setCategories(). Both require an array of WPBlockCategory elements. This change aims for making the categories: WPBlockCategory[] with unique entries by slug to avoid rendering in Block Inserter all Blocks duplicated.

See also: #50061


Testing Instructions

Go to "New Post" (or "Edit Post"), open DevTools and insert following:

const cateogries = wp.blocks.getCategories();
wp.blocks.setCategories( [
    ... cateogries, 
    ... [ {'slug':'theme', 'title': 'My title'}, {'slug':'theme', 'title': 'Meeeh'} ] 
);

Open afterwards the "Block Inserter". You will see now "Theme", "My Title" and "Meeeh" with exactly 3 times the same Blocks. With this fix we ensure, that only 1 Group is shown with the Blocks.


Open Questions

Right now, the last entry "wins". Should we show a message ( console.info()?) when an entry is overwritten by a matching slug?

Block categories can be registered through backend by hooking into the "block_categories_all"-filter or through frontend by calling wp.blocks.setCategories(). Both require an array of WPBlockCategory elements. This change aims for making the categories: WPBlockCategory[] with unique entries by slug to avoid rendering in Block Inserter all Blocks duplicated.

See also: WordPress#50061
Copy link

github-actions bot commented Jun 28, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Chrico <chrico@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Jun 28, 2024
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @Chrico! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@shail-mehta shail-mehta added [Feature] Block API API that allows to express the block paradigm. [Type] Bug An existing feature does not function as intended labels Jun 28, 2024
@talldan
Copy link
Contributor

talldan commented Aug 1, 2024

Thanks for the contribution @Chrico! Would you be able to add a unit test as well that tests the uniqueness?

This is where the tests are currently located:

describe( 'categories', () => {
it( 'should return the default categories as default state', () => {
expect( categories( undefined, {} ) ).toEqual( DEFAULT_CATEGORIES );
} );
it( 'should override categories', () => {
const original = deepFreeze( [
{ slug: 'chicken', title: 'Chicken' },
] );
const state = categories( original, {
type: 'SET_CATEGORIES',
categories: [ { slug: 'wings', title: 'Wings' } ],
} );
expect( state ).toEqual( [ { slug: 'wings', title: 'Wings' } ] );
} );
it( 'should add the category icon', () => {
const original = deepFreeze( [
{
slug: 'chicken',
title: 'Chicken',
},
] );
const state = categories( original, {
type: 'UPDATE_CATEGORY',
slug: 'chicken',
category: {
icon: 'new-icon',
},
} );
expect( state ).toEqual( [
{
slug: 'chicken',
title: 'Chicken',
icon: 'new-icon',
},
] );
} );
it( 'should update the category icon', () => {
const original = deepFreeze( [
{
slug: 'chicken',
title: 'Chicken',
icon: 'old-icon',
},
{
slug: 'wings',
title: 'Wings',
icon: 'old-icon',
},
] );
const state = categories( original, {
type: 'UPDATE_CATEGORY',
slug: 'chicken',
category: {
icon: 'new-icon',
},
} );
expect( state ).toEqual( [
{
slug: 'chicken',
title: 'Chicken',
icon: 'new-icon',
},
{
slug: 'wings',
title: 'Wings',
icon: 'old-icon',
},
] );
} );
it( 'should update multiple category properties', () => {
const original = deepFreeze( [
{
slug: 'chicken',
title: 'Chicken',
icon: 'old-icon',
},
{
slug: 'wings',
title: 'Wings',
icon: 'old-icon',
},
] );
const state = categories( original, {
type: 'UPDATE_CATEGORY',
slug: 'wings',
category: {
title: 'New Wings',
chicken: 'ribs',
},
} );
expect( state ).toEqual( [
{
slug: 'chicken',
title: 'Chicken',
icon: 'old-icon',
},
{
slug: 'wings',
title: 'New Wings',
chicken: 'ribs',
icon: 'old-icon',
},
] );
} );

@Chrico
Copy link
Contributor Author

Chrico commented Aug 1, 2024

Thanks for the contribution @Chrico! Would you be able to add a unit test as well that tests the uniqueness?

Here we go: a72049a ;-)

Copy link
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

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

Code looks good and this is testing well, thank you for the contribution

@talldan talldan merged commit c6d7b2f into WordPress:trunk Aug 2, 2024
60 checks passed
@talldan talldan changed the title blocks/store/reducer // ensure that categories are unique by slug. Block categories - ensure that categories are unique by slug. Aug 2, 2024
@github-actions github-actions bot added this to the Gutenberg 19.0 milestone Aug 2, 2024
@Chrico
Copy link
Contributor Author

Chrico commented Aug 2, 2024

Thanks...but I also missed to write the changelog...This is a breaking fix change (in theory), right? 👀

@talldan
Copy link
Contributor

talldan commented Aug 2, 2024

If you want to make a PR for that, I'd be happy to merge it too, you can add an entry to the 'Unreleased' section in the blocks package. I'd personally consider it a bug fix, I'm not sure I'd consider it a breaking change. The package's interface is still the same, it's only the internal behavior that's very slightly different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Block categories are not unique
3 participants