woocommerce/plugins/woocommerce-blocks/assets/js/data/collections
Mike Jolley ca8efd7c3e Add API fetch error handling (https://github.com/woocommerce/woocommerce-blocks/pull/1687)
* initial experiment for fetch error handling

* throw error from api response error (this allows for catching it in the stream)

* Add ERROR action type

* Controls: resolve with an error response object rather than throw exception

* remove try catch from resolver; just yield correct object based on presence of any errors

* Make the use collection hook return an error.

* Small tidy up in use-store-products

* Throw exception from Use Collection - useState is needed for the exception to work inside a hook (see comments)

* Show error code from API in error message

* Update assets/js/base/components/block-error-boundary/block-error.js

Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com>

* Update assets/js/base/components/block-error-boundary/style.scss

Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com>

* Remove comment

* Handle api error in boundry

* Use reject in promise

* Return error message by default which may be undefined (this is ok)

* Update mocks so tests pass again

Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-01-31 13:01:34 +00:00
..
test Update NPM dependencies and script dependency handling (https://github.com/woocommerce/woocommerce-blocks/pull/1337) 2019-12-10 12:17:46 -05:00
README.md Rename modelName to resourceName everywhere (https://github.com/woocommerce/woocommerce-blocks/pull/1173) 2019-11-14 11:10:50 -05:00
action-types.js Add API fetch error handling (https://github.com/woocommerce/woocommerce-blocks/pull/1687) 2020-01-31 13:01:34 +00:00
actions.js Add API fetch error handling (https://github.com/woocommerce/woocommerce-blocks/pull/1687) 2020-01-31 13:01:34 +00:00
constants.js All Products with Inner Blocks (https://github.com/woocommerce/woocommerce-blocks/pull/1032) 2019-10-28 09:53:09 -04:00
controls.js All Products with Inner Blocks (https://github.com/woocommerce/woocommerce-blocks/pull/1032) 2019-10-28 09:53:09 -04:00
index.js All Products with Inner Blocks (https://github.com/woocommerce/woocommerce-blocks/pull/1032) 2019-10-28 09:53:09 -04:00
reducers.js Add API fetch error handling (https://github.com/woocommerce/woocommerce-blocks/pull/1687) 2020-01-31 13:01:34 +00:00
resolvers.js Add API fetch error handling (https://github.com/woocommerce/woocommerce-blocks/pull/1687) 2020-01-31 13:01:34 +00:00
selectors.js Add API fetch error handling (https://github.com/woocommerce/woocommerce-blocks/pull/1687) 2020-01-31 13:01:34 +00:00

README.md

Collections Store.

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).