-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Edit Post]: Migrate store actions to thunks #36551
Conversation
Size Change: +28 B (0%) Total Size: 1.15 MB
ℹ️ View Unchanged
|
6bb92fc
to
590f5e6
Compare
registry | ||
.dispatch( interfaceStore ) | ||
[ isPinned ? 'unpinItem' : 'pinItem' ]( 'core/edit-post', pluginName ); |
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.
I had to stop here for a moment to consider all the logic. Perhaps it would read smoother without the ternary operator?
registry | |
.dispatch( interfaceStore ) | |
[ isPinned ? 'unpinItem' : 'pinItem' ]( 'core/edit-post', pluginName ); | |
const { unpinItem, pinItem} = registry.dispatch( interfaceStore ); | |
if ( isPinned ) { | |
unpinItem( 'core/edit-post', pluginName ); | |
} else { | |
pinItem( 'core/edit-post', pluginName ); | |
} |
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.
Ideally (out of scope for this PR) there would be a togglePinnedItem
action in the interface
store. The pinItem
/unpinItem
actions are used at two places, and both of them really want to toggle.
@ntsekouras There was a similar problem in gutenberg/packages/edit-navigation/src/store/test/resolvers.js Lines 40 to 45 in 8b1d86e
It still required mocking the API response though:
Would that make it easier here? If not, I think leaning on e2e is reasonable. |
2c89e7f
to
74bcc65
Compare
registry | ||
.dispatch( interfaceStore ) | ||
[ isPinned ? 'unpinItem' : 'pinItem' ]( 'core/edit-post', pluginName ); |
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.
Ideally (out of scope for this PR) there would be a togglePinnedItem
action in the interface
store. The pinItem
/unpinItem
actions are used at two places, and both of them really want to toggle.
|
||
if ( ! isEditorReady ) { | ||
return; | ||
} | ||
|
||
const postType = yield controls.select( editorStore, 'getCurrentPostType' ); | ||
|
||
// Only initialize once. | ||
if ( metaBoxesInitialized ) { | ||
return; | ||
} |
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.
Interestingly, the "is editor ready" and "are metaboxes initialized" checks are already performed by the <MetaBoxes>
component before calling initializeMetaBoxes
. One of the checks is redundant.
subscribe( () => { | ||
const isSavingPost = select( editorStore ).isSavingPost(); | ||
const isAutosavingPost = select( editorStore ).isAutosavingPost(); | ||
registry.subscribe( async () => { |
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 subscription doesn't really belong to a Redux action. It's something that would be much more easily implemented in the <MetaBoxes>
component. The component would then (elegantly) do three things:
function MetaBoxes() {
// initialize metaboxes
useEffect( () => {
if ( shouldInit ) { initializeMetaBoxes(); }
}, [ shouldInit ] );
// update metaboxes (new)
useEffect( () => {
if ( shouldUpdate ) { requestMetaBoxUpdates(); }
}, [ shouldUpdate ] );
// render metaboxes
return <>{ metaBoxes.map( box => ... ) }</>;
}
} catch { | ||
yield controls.dispatch( editPostStore, 'metaBoxUpdatesFailure' ); | ||
dispatch.metaBoxUpdatesFailure(); |
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.
Interesting that both these actions do the same thing: update the isSavingMetaBoxes
flag. They could be merged into one. We are ignoring the errors.
Also, do they need to be a part of the public API? There is no use case for that. Just like REQUEST_META_BOX_UPDATES
, they could be internal/private.
registry | ||
.select( interfaceStore ) | ||
.isFeatureActive( editPostStore.name, 'welcomeGuide' ) | ||
).toBeFalsy(); |
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.
isFeatureActive
always returns a true boolean, not just a falsy value. A stronger .toBe( false )
expectation is in place.
74bcc65
to
37abf43
Compare
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.
Looks good. I posted a few comments, but they are more like refactoring ideas rather than blockers.
There was an incorrect update of package-lock.json
and I added a commit that fixes that.
I also rebased the PR onto the latest trunk
. Let's see what the CI checks say now.
The e2es are failing at:
The PR is changing exactly this metabox-saving code, so apparently at some point the migration went wrong. |
eccc9c0
to
ba9d323
Compare
Thanks for the reviews here! Also thank you @jsnajdr for the extra work with rebasing and fixes. I'm now looking at the comments and will try to find out about the metaboxes test failures. As I'm not really familiar with metaboxes, I could use some help here if you find something obvious, but either way I'll try to solve them. I can see that some comments about refactoring some things in metaboxes make sense, but I'd love if we could just refactor to use thunks here and handle these in a follow up. |
@ntsekouras I did some debugging and it seems that the e2e test is inherently fragile and flaky. Here's approximately what it does: await savePost();
reloadPage();
verifySavedInfoHasBeenLoaded(); I.e., it reloads the page immediately after subscribe( async () => {
if ( prevIsSaving === true && nextIsSaving === false ) {
await dispatch( requestMetaBoxUpdates() );
}
} ); Is the metabox save request performed before the |
Thanks for helping @jsnajdr!! I'm still looking into the failures and it might be something related with the |
I also tried the patch with the |
The e2e tests are now green after the |
This PR migrates the
edit-post
data store to thunks and updates unit tests.Notes
There are some tests missing right now, but when I started writing them a lot of mocking for
core
store internals were needed and I'm not sure if it's the right approach here. Maybe some e2e tests would be better for these cases?Namely the untested actions are
__unstableCreateTemplate, initializeMetaBoxes and requestMetaBoxUpdates
.