2019-12-03 13:57:56 +00:00
/ * *
* External dependencies
* /
2020-03-04 12:40:03 +00:00
import {
withRestApiHydration ,
withStoreCartApiHydration ,
} from '@woocommerce/block-hocs' ;
2020-03-06 11:43:40 +00:00
import { _ _ } from '@wordpress/i18n' ;
2020-02-18 23:06:37 +00:00
import { useStoreCart } from '@woocommerce/base-hooks' ;
import { RawHTML } from '@wordpress/element' ;
2020-02-25 11:36:53 +00:00
import LoadingMask from '@woocommerce/base-components/loading-mask' ;
2020-03-17 11:45:33 +00:00
import {
StoreNoticesProvider ,
ValidationContextProvider ,
2020-04-01 09:27:53 +00:00
CartProvider ,
2020-03-17 11:45:33 +00:00
} from '@woocommerce/base-context' ;
2020-03-06 11:43:40 +00:00
import { CURRENT _USER _IS _ADMIN } from '@woocommerce/block-settings' ;
import { _ _experimentalCreateInterpolateElement } from 'wordpress-element' ;
2020-03-03 10:26:02 +00:00
2019-12-03 13:57:56 +00:00
/ * *
* Internal dependencies
* /
2019-12-10 15:41:57 +00:00
import FullCart from './full-cart' ;
2020-03-13 13:41:59 +00:00
import blockAttributes from './attributes' ;
2019-12-03 13:57:56 +00:00
import renderFrontend from '../../../utils/render-frontend.js' ;
2020-04-03 13:22:56 +00:00
const reloadPage = ( ) => void window . location . reload ( true ) ;
2020-02-18 23:06:37 +00:00
/ * *
2020-03-13 13:41:59 +00:00
* Renders the frontend block within the cart provider .
2020-02-18 23:06:37 +00:00
* /
2020-03-13 13:41:59 +00:00
const Block = ( { emptyCart , attributes } ) => {
const { cartItems , cartIsLoading } = useStoreCart ( ) ;
2020-02-18 23:06:37 +00:00
2020-02-25 11:36:53 +00:00
return (
2020-03-13 13:41:59 +00:00
< >
{ ! cartIsLoading && cartItems . length === 0 ? (
2020-02-25 11:36:53 +00:00
< RawHTML > { emptyCart } < / R a w H T M L >
) : (
< LoadingMask showSpinner = { true } isLoading = { cartIsLoading } >
2020-03-17 11:45:33 +00:00
< ValidationContextProvider >
2020-04-01 09:27:53 +00:00
< CartProvider >
< FullCart attributes = { attributes } / >
< / C a r t P r o v i d e r >
2020-03-17 11:45:33 +00:00
< / V a l i d a t i o n C o n t e x t P r o v i d e r >
2020-02-25 11:36:53 +00:00
< / L o a d i n g M a s k >
) }
2020-03-13 13:41:59 +00:00
< / >
) ;
} ;
/ * *
* Wrapper component to supply API data and show empty cart view as needed .
*
* @ param { * } props
* /
const CartFrontend = ( props ) => {
return (
< StoreNoticesProvider context = "wc/cart" >
< Block { ... props } / >
2020-03-03 10:26:02 +00:00
< / S t o r e N o t i c e s P r o v i d e r >
2020-02-18 23:06:37 +00:00
) ;
} ;
2019-12-10 15:41:57 +00:00
2020-03-13 13:41:59 +00:00
const getProps = ( el ) => {
const attributes = { } ;
Object . keys ( blockAttributes ) . forEach ( ( key ) => {
if ( typeof el . dataset [ key ] !== 'undefined' ) {
2020-03-16 16:38:24 +00:00
switch ( blockAttributes [ key ] . type ) {
case 'boolean' :
attributes [ key ] = el . dataset [ key ] !== 'false' ;
break ;
case 'number' :
attributes [ key ] = parseInt ( el . dataset [ key ] , 10 ) ;
break ;
default :
attributes [ key ] = el . dataset [ key ] ;
break ;
2020-03-13 13:41:59 +00:00
}
} else {
attributes [ key ] = blockAttributes [ key ] . default ;
}
} ) ;
return {
emptyCart : el . innerHTML ,
attributes ,
} ;
} ;
2020-02-18 23:06:37 +00:00
2020-03-06 11:43:40 +00:00
const getErrorBoundaryProps = ( ) => {
return {
header : _ _ ( 'Something went wrong…' , 'woo-gutenberg-products-block' ) ,
text : _ _experimentalCreateInterpolateElement (
_ _ (
2020-04-03 13:22:56 +00:00
'The cart has encountered an unexpected error. <button>Try reloading the page</button>. If the error persists, please get in touch with us so we can assist.' ,
2020-03-06 11:43:40 +00:00
'woo-gutenberg-products-block'
) ,
{
2020-04-03 13:22:56 +00:00
button : (
< button
className = "wp-block-link-button"
onClick = { reloadPage }
/ >
2020-03-06 11:43:40 +00:00
) ,
}
) ,
showErrorMessage : CURRENT _USER _IS _ADMIN ,
} ;
} ;
2020-02-18 23:06:37 +00:00
renderFrontend (
'.wp-block-woocommerce-cart' ,
2020-03-04 12:40:03 +00:00
withStoreCartApiHydration ( withRestApiHydration ( CartFrontend ) ) ,
2020-03-06 11:43:40 +00:00
getProps ,
getErrorBoundaryProps
2020-02-18 23:06:37 +00:00
) ;