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';
|
2021-03-24 13:28:11 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { LAST_CART_UPDATE_TIMESTAMP_KEY } from '../data/cart/constants';
|
2020-03-04 12:40:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hydrate Cart API data.
|
|
|
|
*
|
|
|
|
* Makes cart data available without an API request to wc/store/cart/.
|
|
|
|
*/
|
|
|
|
const useStoreCartApiHydration = () => {
|
|
|
|
const cartData = useRef( getSetting( 'cartData' ) );
|
2021-03-24 13:28:11 +00:00
|
|
|
const { setIsCartDataStale } = useDispatch( CART_STORE_KEY );
|
2020-03-04 12:40:03 +00:00
|
|
|
|
|
|
|
useSelect( ( select, registry ) => {
|
|
|
|
if ( ! cartData.current ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-24 13:28:11 +00:00
|
|
|
const { isResolving, hasFinishedResolution, isCartDataStale } = select(
|
|
|
|
CART_STORE_KEY
|
|
|
|
);
|
2020-03-04 12:40:03 +00:00
|
|
|
|
2021-03-24 13:28:11 +00:00
|
|
|
/**
|
|
|
|
* This should only execute once. When the code further down the file executes
|
|
|
|
* then the condition directly below this comment should never evaluate to true
|
|
|
|
* on subsequent executions. Because of this localStorage.getItem won't be
|
|
|
|
* called multiple times.
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
! isCartDataStale() &&
|
|
|
|
! isResolving( 'getCartData' ) &&
|
|
|
|
! hasFinishedResolution( 'getCartData', [] )
|
|
|
|
) {
|
|
|
|
const lastCartUpdateRaw = window.localStorage.getItem(
|
|
|
|
LAST_CART_UPDATE_TIMESTAMP_KEY
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( lastCartUpdateRaw ) {
|
|
|
|
const lastCartUpdate = parseFloat( lastCartUpdateRaw );
|
|
|
|
const cartGeneratedTimestamp = parseFloat(
|
|
|
|
cartData.current.generated_timestamp
|
|
|
|
);
|
|
|
|
|
|
|
|
const needsUpdateFromAPI =
|
|
|
|
! isNaN( cartGeneratedTimestamp ) &&
|
|
|
|
! isNaN( lastCartUpdate ) &&
|
|
|
|
lastCartUpdate > cartGeneratedTimestamp;
|
|
|
|
|
|
|
|
if ( needsUpdateFromAPI ) {
|
|
|
|
setIsCartDataStale();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 12:40:03 +00:00
|
|
|
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 (
|
2021-03-24 13:28:11 +00:00
|
|
|
! isCartDataStale() &&
|
2020-03-04 12:40:03 +00:00
|
|
|
! 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;
|