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
Refactor students page fetching data through Gutenberg and avoiding subqueries #5104
Conversation
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 to me and works well. 👍
0c9023d
to
2467160
Compare
Changing to draft again since I need to fix one other related thing. |
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.
Thanks @renatho for this refactoring, I was already planning to refactor it to support new features.
Could you please explain how adding a wordpress/data
avoid subqueries?
One of our reasons to not use wordpress/data
was because the sensei wordpress/data
version is pretty outdated (2 years ago) so are avoiding to create new features that doesn't require a strong state management until we update it.
Do you think it is really necessary to solve the problem?
I saw you are removing nock, usually mock the fetch
@wordpress/data library is not a good practice because it creates a coupling between the test and the implementation.
It already helped us to get some issues.
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.
As we discussed, the main motivation of this PR on the frontends ide is to remove the http-client.
I would suggest remove the changes related to ~@wordpress/data because we are using @wordpress/apiFetch that is also a standard way to load data from wordpress.
it avoid us to use a store and all related infrastructure to support wordpress/data
.
What do you think?
They are unrelated changes. I uptated the PR title in order to make it more clear. 😉
Just registering here in the PR, as we talked, the version we have installed is for the unit tests only. When running the site, it will import it from from the Gutenberg bundled version in WordPress core: https://developer.wordpress.org/block-editor/contributors/versions-in-wordpress/. More details in: https://www.npmjs.com/package/@wordpress/dependency-extraction-webpack-plugin
I was thinking a little about that. I think the only benefit of not using wordpress/data is not including their dependencies in the student page, but it's not a big deal since we're in wp-admin and it's used in many other pages, caching it separately. And using the useSelect there with the store, it brings many benefits, like using an implementation that is specifically for this purpose, saving some code writing and being more prepared for future changes. So I'd say that it would be nice to continue with wordpress/data, unless you have a strong reason to not use it. WDYT? |
import { useSelect } from '@wordpress/data'; | ||
import { useState, useEffect, useCallback } from '@wordpress/element'; | ||
|
||
// TODO: Write tests. |
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.
Are you going to add this test?
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.
A partial test added here: 859125c, but I couldn't make the debounce work yet. So I'll keep this part as TODO for now. 😉
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; |
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.
Suggestion, replace useSelect
to useEntityRecords
const { records, isResolving } = useEntityRecords( 'postType', 'courses' );
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.
Thank you for the suggestion! It would be nice, but this hook was exposed recently, so we can't use that yet in order to support older versions. See it in: WordPress/gutenberg@8b8886c
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.
@renatho I still think that configuring a store just to render a list inside a modal (it is not a SPA) just to make a get a bit overkill but considering it was moved to a hook if we can easy switch if we saw some issue with this approach.
I just noticed your add a TODO to write the hook test. Are you planning add this test?
The test is not complete, since we're having an issue to simulate the debounce timers.
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 and works well 👍
// TODO: Complete test running debounce to make sure it was called after the time. | ||
// jest.runAllTimers(); | ||
// expect( mockFn ).toBeCalledWith( mapSelect, deps2 ); |
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 you plan to do this before merging?
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.
Unfortunately, I don't plan to add it now. We're having some problems with dependencies in order to make it work. runAllTimers
is starting an infinite loop in the debounce
. There are some issues about that on the Internet and it was supposed to be fixed, so at some point, we need to dig into the dependencies to see what's happening.
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.
Just adding some links here for future reference about this:
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery | ||
$results = $wpdb->get_results( | ||
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholders created dinamically. |
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.
Just writing to confirm that you are using phpcs:disable
on purpose. As opposed to phpcs:ignore
this will disable the check until the end of the current file not just on the following line.
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.
Good catch! It wasn't on purpose. Fixed in: 4e602d0
After the last approval, I just added a commit fixing PHPCS comments: 4e602d0. We need this PR in the next release, so considering this change is simple and doesn't impact how the plugin work, I'm going ahead and merging this. Please, let me know if there's something else on that, so I can raise a new PR. 😉 |
Changes proposed in this Pull Request
useSelect
.useSelectWithDebounce
in order to expand theuseSelect
, to work with debounce.wp_kses
we use for the Students table, in order to support it in different envs that have customization on thewp_kses
, like removing thedata-*
.Testing instructions