-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Remove all wp.api Usage from the editor package #8088
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
Conversation
| @@ -0,0 +1,320 @@ | |||
| /** | |||
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.
For more sanity and also because it's easier to use async/await this way, I extracted some effects handler into their own files.
| REQUEST_POST_UPDATE_SUCCESS: requestPostUpdateSuccess, | ||
| REQUEST_POST_UPDATE_FAILURE: requestPostUpdateFailure, | ||
| TRASH_POST: ( action, store ) => { | ||
| trashPost( action, store ); |
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 can't assign it directly because there's a warning if we return a promise from effect handlers
| const getResult = () => select( namespace )[ selectorName ].apply( null, args ); | ||
|
|
||
| const unsubscribe = subscribe( () => { | ||
| if ( hasFinished() ) { |
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 is where it's not working at the moment, I'm not certain why but hasFinished is never true.
- Try to edit a post and click "save draft", nothing happens because it's stuck here.
cc @aduth
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.
Fixed by aduth/equivalent-key-map#1
gziolo
left a comment
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 would like to better distill what has exactly changed with the effects file refactoring. I will wait until you are online before I continue. In general, I like the direction where it is heading. It will also help us to address #7397 with the removel of withAPIData usage.
| hasCreateAction: taxonomy ? get( getCurrentPost(), [ '_links', 'wp:action-create-' + taxonomy.rest_base ], false ) : false, | ||
| hasAssignAction: taxonomy ? get( getCurrentPost(), [ '_links', 'wp:action-assign-' + taxonomy.rest_base ], false ) : false, | ||
| terms: taxonomy ? select( 'core/editor' ).getEditedPostAttribute( taxonomy.rest_base ) : [], | ||
| taxonomy, |
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.
Should we pass down restBase instead of full taxonomy object? I think only this one field is used in the component. It would also align better with what we had before.
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.
Actually, this made me think, what will happen when taxonomy is falsy? We don't check against it inside the component.
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 check against hasAssignAction which is false if the taxonomy is falsy.
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 the taxonomy object is used for labels as well.
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.
Do we expect to refactor this component so that it's not calling apiFetch at all? This was a hope of mine. Wondering if it would make things easier to do this conversion first, before this one.
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.
Yes, I think we should do it, the thing is, it's not ready at the moment because we need paginated/query data in core-data. Hopefully we can get back to that once we publish those remaining packages.
| hasCreateAction: taxonomy ? get( getCurrentPost(), [ '_links', 'wp:action-create-' + taxonomy.rest_base ], false ) : false, | ||
| hasAssignAction: taxonomy ? get( getCurrentPost(), [ '_links', 'wp:action-assign-' + taxonomy.rest_base ], false ) : false, | ||
| terms: taxonomy ? select( 'core/editor' ).getEditedPostAttribute( taxonomy.rest_base ) : [], | ||
| taxonomy, |
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 echo my previous concerns regarding taxonomy and rest_base.
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 think I added a check for this specific component
| * | ||
| * @return {Promise} Selector result. | ||
| */ | ||
| export function resolveSelector( namespace, selectorName, ...args ) { |
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 utility or something equivalent will be bundled in the data module once we land the async actions work.
4d993d5 to
8fce9d6
Compare
8fce9d6 to
a5f4c9d
Compare
gziolo
left a comment
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.
One nitpick so far, I will continue tomorrow. taxonomy changes look good as a temporary solution. I double checked your comments 👍
| "dom-scroll-into-view": "1.2.1", | ||
| "element-closest": "2.0.2", | ||
| "equivalent-key-map": "0.2.0", | ||
| "equivalent-key-map": "0.2.1", |
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 can remove it from here, it is used only in data package.
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.
For some reason, I didn't understand, removing it break the unit tests (but only on travis)
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.
Notice I removed it and than added it back in the commits
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 think there went something wrong with package-lock.json. We can clean it up when all packages are there, no worries 👍
|
Not sure if relevant, but I've found a gutenberg/editor/components/provider/index.js Lines 71 to 85 in a5f4c9d
|
|
Tested for a bit and couldn't break anything! ✨
|
|
Re: gutenberg/editor/components/provider/index.js Lines 71 to 85 in a5f4c9d
It looks like Could we provide defaults, or pass through props to this component, with a fallback to globals if they exist? |
editor/store/effects/utils.js
Outdated
|
|
||
| // We need to trigger the selector (to trigger the resolver) | ||
| const result = getResult(); | ||
| if ( hasFinished() ) { |
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.
Can unsubscribe be declared after this if statement so we wouldn't have to subscribe it if is already finished?
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.
Unit test passes, so it should be good :)
editor/store/effects/test/utils.js
Outdated
| } ); | ||
|
|
||
| // Trigger resolution | ||
| const value = await resolveSelector( 'resolveStore', 'selectAll', 'check' ); |
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.
Should it be resolveStore2?
editor/store/effects/test/utils.js
Outdated
|
|
||
| describe( 'resolveSelector', () => { | ||
| it( 'Should wait for selector resolution', async () => { | ||
| registerStore( 'resolveStore', { |
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.
Would it make to extract the store definitions as follows:
const resolveStore = {
reducer: ( state = 'no', action ) => {
if ( action.type === 'resolve' ) {
return 'yes';
}
return state;
},
selectors: {
selectAll: ( state, key ) => ( key === 'check' ) ? state : 'no-key',
},
resolvers: {
selectAll: () => {
return new Promise( ( resolve ) => {
process.nextTick( () => resolve( { type: 'resolve' } ) );
} );
},
},
};and share it between those 2 tests?
editor/store/effects/test/utils.js
Outdated
| if ( action.type === 'resolve' ) { | ||
| return 'yes'; | ||
| } | ||
| if ( action.type === 'reset' ) { |
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 type is never used. Removing it doesn't break any test.
gziolo
left a comment
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 finished my review, how do you feel about addressing smaller things in this PR and opening a follow up for what @Copons discovered? This PR is already big and I bet we will have to move Provider code outside of editor to make it backward compatible. We need to ensure withAPIData works for some time before we fully deprecate it.
| dispatch( removeNotice( TRASH_POST_NOTICE_ID ) ); | ||
| try { | ||
| await apiFetch( { path: `/wp/v2/${ postType.rest_base }/${ postId }`, method: 'DELETE' } ); | ||
| } catch ( error ) { |
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 see this code missing after apiFetch when comparing with master:
const post = getCurrentPost( getState() );
// TODO: This should be an updatePost action (updating subsets of post properties),
// But right now editPost is tied with change detection.
dispatch( resetPost( { ...post, status: 'trash' } ) );Is that expected change?
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.
No it's not, I wonder how this happened :P
| // TODO: these are potentially undefined, this fix is in place | ||
| // until there is a filter to not use shared blocks if undefined | ||
| const postType = await resolveSelector( 'core', 'getPostType', 'wp_block' ); | ||
| if ( ! postType ) { |
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'm not sure how to check the return value for this selector. We should ensure it is not an empty object because this check will fail.
Just in case it's necessary, there are 2 more places where we have the same check.
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 don't think it can be an empty object, if the state is not there for some reason, it will be undefined and it will be the right post type if not. I wonder if we can't just remove the check entirely actually :P
It looks like @youknowriad found a really nice solution for that one :) |
|
I removed the remaining dependencies to |
gziolo
left a comment
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 tested locally a similar set of functionalities as @Copons did. Everything works as expected. Travis is failing with one of the e2e tests, but it's a known issue and we have open PR to fix it. I restart build. Feel free to merge as soon as it turns green.
Great job with this refactor 🥇
| APIProvider, | ||
| { | ||
| ...wpApiSettings, | ||
| ...pick( wp.api, [ |
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.
✨
| const TRASH_POST_NOTICE_ID = 'TRASH_POST_NOTICE_ID'; | ||
|
|
||
| /** | ||
| * Request Post Update Effect handler |
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.
These JSDoc descriptions really go to reinforce my worry that we don't really have a good understanding of what's intended to take place in these functions.
This PR tries to remove all wp.api usage from the "editor" package.
Notes
async/awaitresolveSelectorutility function that waits for the resolution of the selector before returning a promise with its value.wp.api.getPostTypeRoutewithselect( 'core' ).getPostType( getCurrentPostType( state ) ).rest_baseTesting instructions