woocommerce/plugins/woocommerce-blocks/assets/js/data/collections
Thomas Roberts ebe05700ab Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938)
* Add receiveCart thunk

* Add mapCartResponseToCart helper

* Add getItemsPendingQuantityUpdate selector

* Update cart resolvers to be thunks

* Remove RECEIVE_CART action and replace with SET_CART_DATA

receiveCart will turn into a thunk.

* Add notifyQuantityChanges functions

* Remove receiveCart from action type definition, replace with setCartData

* Move apiFetchWithHeaders out of controls

This will just be a normal function since we'll be updating actions to thunks which will use this instead of a control.

* Include thunks in actions file

* Update receiveCart action to setCartData

* Update applyCoupon action to a thunk

* Update useStoreCartCoupons to get action from correct place

* Update StoreCartCoupon types

* Add types for Thunk and ThunkReturnType in mapped-types

* Change applyCoupon to a thunk

* Get applyCoupon, removeCoupon, receiveApplyingCoupon from useDispatch

This is to separate the concerns of actions vs. selectors. Previously the actions were fetched during useSelect which is not a pattern we use anywhere else in the codebase. Since we updated the MapToDispatch type, we can now get correctly typed thunks from the data store.

* Improve apiFetchWithHeaders typings

* Convert removeCoupon from generator to thunk

* Add applyCoupon and removeCoupon to CartAction type

* Remove unused old-style type-def

* Add receiveApplyingCoupon & receiveRemovingCoupon to StoreCartCoupon

* Correct issues with StoreCartCoupon type

These were not intended to reflect the actions in data store, rather the functions offered by the useStoreCartCoupons hook.

* Update applyExtensionCartUpdate to a thunk

* Update addItemToCart to thunk

* Add ResolveSelectFromMap type that works with thunks

* Add CartDispatchFromMap and CartResolveSelectFromMap types

We can add this to all data stores to get them working with thunks properly.

* Add docs and update generic name in ResolveSelectFromMap

* Add correct types for thunk resolvers in cart data store

* Update removeItemFromCart to thunk

* Update apiFetchWithHeaders to use generic

* Update selectShippingRate to thunk

* Update resolver tests to test correct thunk functionality

* Update updateCustomerData to thunk

* Update reducer test to reflect new action name

* Update comments on CartDispatchFromMap and CartResolveSelectFromMap

* Add quantity_limits to preview cart

* Make notices speak when shown

* Remove copilot comment

* Add isWithinQuantityLimits function

This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.

* Add tests for notifyQuantityChanges

* Show notice when multiple_of is updated

* Update test to test for multiple_of changes

* Remove empty export

* Remove controls from cart data store

Not needed anymore since the exported value from the shared-controls file was empty.

* Export a control and async function for apiFetchWithHeaders

This is required because async functions cannot be called from sync generators.

* Use control version of apiFetchWithHeaders in the collections store

* Improve comments and remove incorrect TypeScript

* Update assets/js/data/cart/actions.ts

Co-authored-by: Mike Jolley <mike.jolley@me.com>

* Update ResolveSelectFromMap to include selectors too

* Update TS in actions

* Use finally to remove duplicate code

* remove item pending delete/qty update after action runs in all cases

This will also reset the state when the request to remove it/change quantity errors

* Remove unnecessary type from param.

Not needed because we have TS now. The description can stay though, it is useful.

* Update snackbar wording to use active voice

* Remove old WP version check

* Set max quantity to high number instead of null

This would only happen in a niche case, and would require several TS changes to fix, so it's better to set it as a number here. 9999 should be high enough, and is the default quantity limit set below in get_product_quantity_limit

* Set code on woocommerce_rest_cart_invalid_key to 409

This is so the cart is returned in the response, so the client can update.

* Fix typo in comment and add CartSelectFromMap

* Remove unnecessary docblock

* Add getItemsPendingDelete selector

This is needed so we can show a notice for items that are unexpectedly removed from the cart. We need to know which ones are pending delete so we can skip showing the notice for them.

* Add type for notifyQuantityChanges args and change args to object

* Add notifyIfRemoved function

This will check items that have been removed and show a notice for them.

* Fix TS in receiveCart & pass itemsPendingDelete to notifyQuantiyChanges

* Update wording on removal notice

* Update types for notifyQuantityChanges args

* Update tests to reflect new wording and args being an object

* Check item is truth before running comparison of keys

* Update tests for unexpectedly and expectedly removed items

* Ignore print_r to satisfy phpcs

* Update PHP tests to reflect correct response code when deleting items

* Remove unnecessary controls and dispatch events directly from thunk

Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-12-16 08:06:37 -08:00
..
test Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938) 2022-12-16 08:06:37 -08:00
README.md Improve the structure of the WooCommerce Blocks Handbook (https://github.com/woocommerce/woocommerce-blocks/pull/6429) 2022-06-09 17:52:19 +02:00
action-types.js Implement last modified header for the products endpoint to allow for cache busting (https://github.com/woocommerce/woocommerce-blocks/pull/1647) 2020-02-18 11:37:39 +00:00
actions.js Update @woocmmerce/eslint-plugin to 2.0.0 (https://github.com/woocommerce/woocommerce-blocks/pull/6203) 2022-04-08 14:47:19 +01:00
constants.js Remove v1 from Store Keys (https://github.com/woocommerce/woocommerce-blocks/pull/5987) 2022-03-04 14:03:52 +00:00
index.js Update from deprecated registerStore to register for data stores (https://github.com/woocommerce/woocommerce-blocks/pull/7310) 2022-11-10 15:37:01 +00:00
reducers.js Update @woocmmerce/eslint-plugin to 2.0.0 (https://github.com/woocommerce/woocommerce-blocks/pull/6203) 2022-04-08 14:47:19 +01:00
resolvers.js Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938) 2022-12-16 08:06:37 -08:00
selectors.js Implement last modified header for the products endpoint to allow for cache busting (https://github.com/woocommerce/woocommerce-blocks/pull/1647) 2020-02-18 11:37:39 +00:00

README.md

Collections Store

Table of Contents

To utilize this store you will import the COLLECTIONS_STORE_KEY in any module referencing it. Assuming @woocommerce/block-data is registered as an external pointing to wc.wcBlocksData you can import the key via:

import { COLLECTIONS_STORE_KEY } from '@woocommerce/block-data';

Actions

receiveCollection( namespace, resourceName, queryString, ids = [], items = [], replace = false )

This will return an action object for the given arguments used in dispatching the collection results to the store.

Note: You should rarely have to dispatch this action directly as it is used by the resolver for the getCollection selector.

argument type description
namespace string The route namespace for the collection (eg. /wc/blocks)
resourceName string The resource name for the collection (eg. products/attributes)
queryString string An additional query string to add to the request for the collection. Note, collections are cached by the query string. (eg. '?order=ASC')
ids array If the collection route has placeholders for ids, you provide them via this argument in the order of how the placeholders appear in the route
response Object An object containing a items property with the collection items from the response (array), and a headers property that is matches the window.Headers interface containing the headers from the response.
replace boolean Whether or not to replace any existing items in the store for the given indexes (namespace, resourceName, queryString) if there are already values in the store

Selectors

getCollection( namespace, resourceName, query = null, ids=[] )

This selector will return the collection for the given arguments. It has a sibling resolver, so if the selector has never been resolved, the resolver will make a request to the server for the collection and dispatch results to the store.

argument type description
namespace string The route namespace for the collection (eg. /wc/blocks)
resourceName string The resource name for the collection (eg. products/attributes)
query Object The query arguments for the collection. Eg. { order: 'ASC', sortBy: Price }
ids Array If the collection route has placeholders for ids you provide the values for those placeholders in this array (in order).

getCollectionHeader( namespace, resourceName, header, query = null, ids = [])

This selector will return a header from the collection response using the given arguments. It has a sibling resolver that will resolve getCollection using the arguments if that has never been resolved.

If the collection has headers but not a matching header for the given header argument, then undefined will be returned.

If the collection does not have any matching headers for the given arguments, then null is returned.

argument type description
namespace string The route namespace for the collection (eg. /wc/blocks)
resourceName string The resource name for the collection (eg. products/attributes)
header string The header key for the header.
query Object The query arguments for the collection. Eg. { order: 'ASC', sortBy: Price }
ids Array If the collection route has placeholders for ids you provide the values for those placeholders in this array (in order).

We're hiring! Come work with us!

🐞 Found a mistake, or have a suggestion? Leave feedback about this document here.