2020-03-04 12:40:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useRef } from '@wordpress/element';
|
|
|
|
import { getSetting } from '@woocommerce/settings';
|
|
|
|
import { CART_STORE_KEY } from '@woocommerce/block-data';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hydrate Cart API data.
|
|
|
|
*
|
|
|
|
* Makes cart data available without an API request to wc/store/cart/.
|
|
|
|
*/
|
|
|
|
const useStoreCartApiHydration = () => {
|
|
|
|
const cartData = useRef( getSetting( 'cartData' ) );
|
|
|
|
|
|
|
|
useSelect( ( select, registry ) => {
|
|
|
|
if ( ! cartData.current ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { isResolving, hasFinishedResolution } = select( CART_STORE_KEY );
|
|
|
|
const {
|
|
|
|
receiveCart,
|
2020-03-10 15:49:26 +00:00
|
|
|
receiveError,
|
2020-03-04 12:40:03 +00:00
|
|
|
startResolution,
|
|
|
|
finishResolution,
|
|
|
|
} = registry.dispatch( CART_STORE_KEY );
|
|
|
|
|
|
|
|
if (
|
|
|
|
! isResolving( 'getCartData', [] ) &&
|
|
|
|
! hasFinishedResolution( 'getCartData', [] )
|
|
|
|
) {
|
|
|
|
startResolution( 'getCartData', [] );
|
2020-03-10 15:49:26 +00:00
|
|
|
if ( cartData.current?.code?.includes( 'error' ) ) {
|
|
|
|
receiveError( cartData.current );
|
|
|
|
} else {
|
|
|
|
receiveCart( cartData.current );
|
|
|
|
}
|
2020-03-04 12:40:03 +00:00
|
|
|
finishResolution( 'getCartData', [] );
|
|
|
|
}
|
|
|
|
}, [] );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HOC that calls the useStoreCartApiHydration hook.
|
|
|
|
*
|
|
|
|
* @param {Function} OriginalComponent Component being wrapped.
|
|
|
|
*/
|
|
|
|
const withStoreCartApiHydration = ( OriginalComponent ) => {
|
|
|
|
return ( props ) => {
|
|
|
|
useStoreCartApiHydration();
|
|
|
|
return <OriginalComponent { ...props } />;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withStoreCartApiHydration;
|