2020-04-08 15:03:39 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-10-25 14:46:34 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useStoreCart } from '@woocommerce/base-context/hooks';
|
2021-10-25 14:46:34 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
2020-04-08 15:03:39 +00:00
|
|
|
import LoadingMask from '@woocommerce/base-components/loading-mask';
|
2022-04-08 12:11:50 +00:00
|
|
|
import {
|
|
|
|
ValidationContextProvider,
|
|
|
|
StoreNoticesContainer,
|
2022-05-25 21:00:47 +00:00
|
|
|
SnackbarNoticesContainer,
|
2022-04-08 12:11:50 +00:00
|
|
|
} from '@woocommerce/base-context';
|
2021-10-25 14:46:34 +00:00
|
|
|
import { CURRENT_USER_IS_ADMIN } from '@woocommerce/settings';
|
|
|
|
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary';
|
|
|
|
import { translateJQueryEventToNative } from '@woocommerce/base-utils';
|
2020-07-14 19:46:44 +00:00
|
|
|
import withScrollToTop from '@woocommerce/base-hocs/with-scroll-to-top';
|
2021-10-25 14:46:34 +00:00
|
|
|
import {
|
|
|
|
StoreNoticesProvider,
|
|
|
|
CartProvider,
|
|
|
|
} from '@woocommerce/base-context/providers';
|
|
|
|
import { SlotFillProvider } from '@woocommerce/blocks-checkout';
|
2020-04-08 15:03:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-10-25 14:46:34 +00:00
|
|
|
import { CartBlockContext } from './context';
|
|
|
|
import './style.scss';
|
2020-04-08 15:03:39 +00:00
|
|
|
|
2021-10-25 14:46:34 +00:00
|
|
|
const reloadPage = () => void window.location.reload( true );
|
2020-08-07 17:33:32 +00:00
|
|
|
|
2021-10-25 14:46:34 +00:00
|
|
|
const Cart = ( { children, attributes } ) => {
|
|
|
|
const { cartIsLoading } = useStoreCart();
|
|
|
|
const { hasDarkControls } = attributes;
|
2020-04-08 15:03:39 +00:00
|
|
|
|
2021-10-25 14:46:34 +00:00
|
|
|
return (
|
|
|
|
<LoadingMask showSpinner={ true } isLoading={ cartIsLoading }>
|
|
|
|
<CartBlockContext.Provider
|
|
|
|
value={ {
|
|
|
|
hasDarkControls,
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<ValidationContextProvider>
|
|
|
|
{ children }
|
|
|
|
</ValidationContextProvider>
|
|
|
|
</CartBlockContext.Provider>
|
|
|
|
</LoadingMask>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ScrollOnError = ( { scrollToTop } ) => {
|
2020-07-14 19:46:44 +00:00
|
|
|
useEffect( () => {
|
|
|
|
// Make it so we can read jQuery events triggered by WC Core elements.
|
|
|
|
const removeJQueryAddedToCartEvent = translateJQueryEventToNative(
|
|
|
|
'added_to_cart',
|
|
|
|
'wc-blocks_added_to_cart'
|
|
|
|
);
|
|
|
|
|
|
|
|
document.body.addEventListener(
|
|
|
|
'wc-blocks_added_to_cart',
|
2021-10-25 16:05:01 +00:00
|
|
|
scrollToTop
|
2020-07-14 19:46:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
removeJQueryAddedToCartEvent();
|
|
|
|
|
|
|
|
document.body.removeEventListener(
|
|
|
|
'wc-blocks_added_to_cart',
|
2021-10-25 16:05:01 +00:00
|
|
|
scrollToTop
|
2020-07-14 19:46:44 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
}, [ scrollToTop ] );
|
|
|
|
|
2021-10-25 14:46:34 +00:00
|
|
|
return null;
|
2020-04-08 15:03:39 +00:00
|
|
|
};
|
2021-10-25 14:46:34 +00:00
|
|
|
const Block = ( { attributes, children, scrollToTop } ) => (
|
|
|
|
<BlockErrorBoundary
|
|
|
|
header={ __( 'Something went wrong…', 'woo-gutenberg-products-block' ) }
|
|
|
|
text={ __(
|
|
|
|
'The cart has encountered an unexpected error. If the error persists, please get in touch with us for help.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
button={
|
|
|
|
<button className="wc-block-button" onClick={ reloadPage }>
|
|
|
|
{ __( 'Reload the page', 'woo-gutenberg-products-block' ) }
|
|
|
|
</button>
|
|
|
|
}
|
|
|
|
showErrorMessage={ CURRENT_USER_IS_ADMIN }
|
|
|
|
>
|
2022-05-25 21:00:47 +00:00
|
|
|
<SnackbarNoticesContainer context="wc/cart" />
|
|
|
|
<StoreNoticesProvider>
|
|
|
|
<StoreNoticesContainer context="wc/cart" />
|
|
|
|
<SlotFillProvider>
|
|
|
|
<CartProvider>
|
|
|
|
<Cart attributes={ attributes }>{ children }</Cart>
|
|
|
|
<ScrollOnError scrollToTop={ scrollToTop } />
|
|
|
|
</CartProvider>
|
|
|
|
</SlotFillProvider>
|
|
|
|
</StoreNoticesProvider>
|
2021-10-25 14:46:34 +00:00
|
|
|
</BlockErrorBoundary>
|
|
|
|
);
|
2020-07-14 19:46:44 +00:00
|
|
|
export default withScrollToTop( Block );
|