Skip to content

Conversation

@louwie17
Copy link
Contributor

@louwie17 louwie17 commented May 11, 2022

All Submissions:

Changes proposed in this Pull Request:

Adds a new products data store that makes use of the products API -> https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products
For now this only allows for retrieving/search for products and getting the product count.

Partially addresses #32524

How to test the changes in this Pull Request:

  1. Load this branch, create a couple products and go to WooCommerce > Home
  2. Open your console and run wp.data.select('wc/admin/products').getProducts({ status: 'publish'}, undefined);
  3. It should return undefined at first
  4. Run it again, assuming the request finished, and it should return all the published products. You can try the above again with other items in the query, like _fields: ['id'] for example, which should only return the ids, any query param defined here should work: https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products
  5. Now do the same for the getProductsTotalCount function, by running: wp.data.select('wc/admin/products').getProductsTotalCount({ status: 'publish'}, undefined);, this might run the correct number right away if you recently ran getProducts with the same query params, otherwise it should return undefined intiially.
  6. If you have the Redux Devtools extension, you can look for the wc/admin/products store and make sure the state is updated correctly and the actions are triggered correctly.
  7. Run wp.data.select('wc/admin/items').getItems('categories', { }); in your console to see if the items store still works correctly, and wp.data.select('wc/admin/items').getItemsTotalCount('categories', { });.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?
  • Have you created a changelog file for each project being changed, ie pnpm nx changelog <project>?

FOR PR REVIEWER ONLY:

  • I have reviewed that everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities. I made sure Linting is not ignored or disabled.

@louwie17 louwie17 requested a review from a team May 11, 2022 15:17
@github-actions github-actions bot added the package: @woocommerce/data issues related to @woocommerce/data label May 11, 2022
@louwie17 louwie17 force-pushed the update/32524_order_product_count_api_requests branch from 61a4430 to d68a8da Compare May 17, 2022 12:20
@botwoo
Copy link
Collaborator

botwoo commented May 17, 2022

📊 Test reports for this pull request have been published and are accessible through the following links:

Latest commit referenced in the reports: Make sure we always use id for receiving products and update reducer … defc559
This comment will automatically be updated with the latest referenced commit when you push new changes to this pull request.


Visit the WooCommerce Test Reports homepage to view all published reports. See the FAQs page if you're having problems accessing them.

Copy link
Contributor

@joshuatf joshuatf left a comment

Choose a reason for hiding this comment

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

Great work, @louwie17!

Overall this looks very solid. Left a few minor comments, but let me know if you have any questions.

status: 'any' | 'draft' | 'pending' | 'private' | 'publish' | 'future';
type: 'simple' | 'grouped' | 'external' | 'variable';
sku: string;
featured: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this supposed to be a boolean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup it should be, thanks

ExtensionList,
ProfileItemsState,
Product,
OnboardingProductType,
Copy link
Contributor

Choose a reason for hiding this comment

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

Good call on this rename!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, Product seemed like a bad name.

@@ -0,0 +1,8 @@
export enum TYPES {
SET_PRODUCT = 'SET_PRODUCT',
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that makes sense, I debated on that, but didn't want to write all the extra actions haha
But it does seem like the best pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I end up just sticking with SUCCESS and ERROR, as we don't really need REQUEST given we have the resolutions for that, and can use isResolutionFinished.


type ProductGetResponse = Product[] | ( { data: Product[] } & Response );

function* request( query: Partial< ProductQuery > ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks very similar to the items data store request method. Any opportunities to dry up some of this logic or are they too unique?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They should be near identical aside from the types, but I can make use of a generic for that.

shipping_class: string;
attribute: string;
attribute_term: string;
tax_class: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be an enum, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

on_sale: boolean;
min_price: string;
max_price: string;
stock_status: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Also an enum I believe.

Copy link
Contributor Author

@louwie17 louwie17 May 20, 2022

Choose a reason for hiding this comment

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

Yeah, I believe you are right, I think just instock or outofstock

* @param {Object} query Query for product totals count.
* @return {string} Resource name for product totals.
*/
export function getTotalProductCountResourceName(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice method!

@louwie17 louwie17 force-pushed the update/32524_order_product_count_api_requests branch from d68a8da to 9e316ab Compare May 20, 2022 13:11
@louwie17
Copy link
Contributor Author

@joshuatf thanks for the review, I have addressed all your feedback in 9e316ab, if you have time for another review?

@louwie17 louwie17 requested a review from joshuatf May 20, 2022 13:12
@louwie17
Copy link
Contributor Author

@joshuatf I did add one extra step to the testing instructions for double checking the items store.

Copy link
Contributor

@joshuatf joshuatf left a comment

Choose a reason for hiding this comment

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

Hey @louwie17, looks really great. Sorry, I missed a couple small items my first pass. I left a couple comments, but let me know if you have any questions.

return items;
} catch ( error ) {
yield getProductsError( query, error );
return error;
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to throw error here so that users can catch on getProducts().

getProducts(...).catch(...).

yield getProductsTotalCountSuccess( query, totalCount );
return totalCount;
} catch ( error ) {
yield getProductsTotalCountError( query, error );
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment about throwing here.

},
},
productsCount: {
'total-guyisms:{}': 2,
Copy link
Contributor

Choose a reason for hiding this comment

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

😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lol, this was deff not me haha I copy pasted most of these tests and updated them, I somehow never noticed this.

shipping_class: string;
attribute: string;
attribute_term: string;
tax_class: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

date_created_gmt: string;
date_modified: string;
date_modified_gmt: string;
type: 'simple' | 'grouped' | 'external' | 'variable';
Copy link
Contributor

Choose a reason for hiding this comment

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

These can also be extended by hooks/filters on the backend.

@louwie17 louwie17 force-pushed the update/32524_order_product_count_api_requests branch from 9e316ab to c35f99d Compare May 24, 2022 18:02
@louwie17 louwie17 requested a review from joshuatf May 24, 2022 18:02
@louwie17
Copy link
Contributor Author

@joshuatf thanks again for you review and suggestions, I addressed the PR feedback if you have time for a re-review?

joshuatf
joshuatf previously approved these changes May 25, 2022
Copy link
Contributor

@joshuatf joshuatf left a comment

Choose a reason for hiding this comment

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

Tested various parameters and fetching of totals and all worked as expected. Also double checked to categories for any regresions.

This is a fantastic addition to our data stores. Thank you for all your hard work on this, @louwie17! :shipit:

@louwie17 louwie17 force-pushed the update/32524_order_product_count_api_requests branch from c35f99d to defc559 Compare May 25, 2022 18:11
@louwie17
Copy link
Contributor Author

@joshuatf I just updated this branch with the fixes for the things we discussed in Slack.
Made sure we always add id when retrieving all the products, and updated the reducer to not overwrite and delete fields that are already present (but only overwrite the new one's).

Copy link
Contributor

@joshuatf joshuatf left a comment

Choose a reason for hiding this comment

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

Thanks for fixing those up, @louwie17! Looks good and I'm able to retrieve individual fields without passing id now.

@louwie17 louwie17 merged commit 434fab9 into trunk May 25, 2022
@louwie17 louwie17 deleted the update/32524_order_product_count_api_requests branch May 25, 2022 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: @woocommerce/data issues related to @woocommerce/data

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants