2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-05 19:54:05 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-03-05 19:54:05 +00:00
|
|
|
import { useStoreCart } from './use-store-cart';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a custom hook that is wired up to the `wc/store/collections` data
|
|
|
|
* store for the `wc/store/cart/shipping-rates` route. Given a query object, this
|
|
|
|
* will ensure a component is kept up to date with the shipping rates matching that
|
|
|
|
* query in the store state.
|
|
|
|
*
|
|
|
|
* @return {Object} This hook will return an object with three properties:
|
|
|
|
* - shippingRates An array of shipping rate objects.
|
|
|
|
* - shippingRatesLoading A boolean indicating whether the shipping
|
|
|
|
* rates are still loading or not.
|
2020-03-05 19:54:05 +00:00
|
|
|
* - updateShipping An action dispatcher to update the shipping address.
|
2020-02-14 03:43:13 +00:00
|
|
|
*/
|
2020-03-05 19:54:05 +00:00
|
|
|
export const useShippingRates = () => {
|
|
|
|
const { shippingRates } = useStoreCart();
|
|
|
|
const results = useSelect( ( select, { dispatch } ) => {
|
|
|
|
const store = select( storeKey );
|
|
|
|
const shippingRatesLoading = store.areShippingRatesLoading();
|
|
|
|
const { updateShippingAddress } = dispatch( storeKey );
|
2020-02-14 03:43:13 +00:00
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
return {
|
|
|
|
shippingRatesLoading,
|
|
|
|
updateShippingAddress,
|
|
|
|
};
|
|
|
|
}, [] );
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
shippingRates,
|
2020-03-05 19:54:05 +00:00
|
|
|
...results,
|
2020-02-14 03:43:13 +00:00
|
|
|
};
|
|
|
|
};
|