-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Page List Block: Simplify Page List convert to links function API #46365
Page List Block: Simplify Page List convert to links function API #46365
Conversation
|
||
const PAGE_FIELDS = [ 'id', 'title', 'link', 'type', 'parent' ]; | ||
const MAX_PAGE_COUNT = 100; | ||
|
||
export const convertSelectedBlockToNavigationLinks = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted to its own file.
convertToNavigationLinks( pages ); | ||
|
||
// Replace the Page List block with the Navigation Links. | ||
replaceBlock( clientId, navigationLinks ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new call is needed but that has the advantage of making the function simpler and more flexible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And easier to test!
@@ -196,7 +195,7 @@ export default function PageListEdit( { | |||
return ( | |||
<> | |||
<InspectorControls> | |||
{ isNavigationChild && ( | |||
{ isNavigationChild && pages?.length > 0 && ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't show this if there are no pages...right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds about right
convertToNavigationLinks( pages ); | ||
|
||
// Replace the Page List block with the Navigation Links. | ||
replaceBlock( clientId, navigationLinks ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new call is needed but that has the advantage of making the function simpler and more flexible.
|
||
// Mock createBlock to avoid creating the blocks in test environment | ||
// as convertToNavigationLinks calls this method internally. | ||
jest.mock( '@wordpress/blocks', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocking here avoids the need to curry the function.
Size Change: -37 B (0%) Total Size: 1.32 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
79c9911
to
e3045d8
Compare
What?
Simplifies the API of the function which converts Pages to Nav links and improves its adherence to SRP.
Why?
In #46352 we used the
convert
function from the Convert Modal in a second place in the Page List code. This drew attention to the overly complex API of that function and the fact that it was unnecessarily curried (presumably to aid in the writing of tests).The changes in this PR make the function easier to work with, reason about and test.
How?
This PR
Testing Instructions
Check that the "convert to links" functionality of the Page List inside Nav block works both when:
Edit
in the Block Toolbar and then clicking in the Moda;Customize
in the Inspector ControlsTesting Instructions for Keyboard
Screenshots or screencast