2019-12-03 14:12:46 +00:00
/ * *
* External dependencies
* /
2020-03-06 11:43:40 +00:00
import { _ _ } from '@wordpress/i18n' ;
2020-03-12 09:24:50 +00:00
import {
withStoreCartApiHydration ,
2021-04-22 11:37:27 +00:00
withRestApiHydration ,
2020-03-12 09:24:50 +00:00
} from '@woocommerce/block-hocs' ;
2021-04-08 12:31:12 +00:00
import { useStoreCart } from '@woocommerce/base-context/hooks' ;
2020-03-23 11:22:00 +00:00
import {
StoreNoticesProvider ,
ValidationContextProvider ,
} from '@woocommerce/base-context' ;
2020-03-06 11:43:40 +00:00
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary' ;
2021-04-22 11:37:27 +00:00
import { CURRENT _USER _IS _ADMIN } from '@woocommerce/settings' ;
2020-09-26 13:51:21 +00:00
import { createInterpolateElement } from 'wordpress-element' ;
2020-05-27 10:11:30 +00:00
import {
renderFrontend ,
2020-06-05 12:18:16 +00:00
getValidBlockAttributes ,
2020-05-27 10:11:30 +00:00
} from '@woocommerce/base-utils' ;
2019-12-03 14:12:46 +00:00
/ * *
* Internal dependencies
* /
import Block from './block.js' ;
2020-03-05 13:06:47 +00:00
import blockAttributes from './attributes' ;
2020-03-26 13:31:09 +00:00
import EmptyCart from './empty-cart/index.js' ;
2019-12-03 14:12:46 +00:00
2020-04-03 13:22:56 +00:00
const reloadPage = ( ) => void window . location . reload ( true ) ;
2020-03-05 19:54:05 +00:00
/ * *
2020-03-06 11:43:40 +00:00
* Wrapper component for the checkout block .
2020-03-05 19:54:05 +00:00
*
2020-03-06 11:43:40 +00:00
* @ param { Object } props Props for the block .
2020-03-05 19:54:05 +00:00
* /
2020-03-06 11:43:40 +00:00
const CheckoutFrontend = ( props ) => {
2020-04-07 15:41:22 +00:00
const { cartItems , cartIsLoading } = useStoreCart ( ) ;
2020-03-10 15:49:26 +00:00
2020-03-06 11:43:40 +00:00
return (
2020-03-19 15:50:36 +00:00
< >
{ ! cartIsLoading && cartItems . length === 0 ? (
< EmptyCart / >
) : (
< BlockErrorBoundary
header = { _ _ (
'Something went wrong…' ,
'woo-gutenberg-products-block'
) }
2020-09-26 13:51:21 +00:00
text = { createInterpolateElement (
2020-03-19 15:50:36 +00:00
_ _ (
2020-04-03 13:22:56 +00:00
'The checkout 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-19 15:50:36 +00:00
'woo-gutenberg-products-block'
) ,
{
2020-04-03 13:22:56 +00:00
button : (
< button
2020-05-08 07:02:18 +00:00
className = "wc-block-link-button"
2020-04-03 13:22:56 +00:00
onClick = { reloadPage }
/ >
2020-03-19 15:50:36 +00:00
) ,
}
) }
showErrorMessage = { CURRENT _USER _IS _ADMIN }
>
< StoreNoticesProvider context = "wc/checkout" >
2020-03-23 11:22:00 +00:00
< ValidationContextProvider >
2020-04-07 15:41:22 +00:00
< Block { ... props } / >
2020-03-23 11:22:00 +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-03-19 15:50:36 +00:00
< / S t o r e N o t i c e s P r o v i d e r >
< / B l o c k E r r o r B o u n d a r y >
2020-03-06 11:43:40 +00:00
) }
2020-03-19 15:50:36 +00:00
< / >
2020-03-06 11:43:40 +00:00
) ;
2020-03-05 19:54:05 +00:00
} ;
2020-03-04 15:13:38 +00:00
const getProps = ( el ) => {
2019-12-03 14:12:46 +00:00
return {
2020-06-05 12:18:16 +00:00
attributes : getValidBlockAttributes ( blockAttributes , el . dataset ) ,
2019-12-03 14:12:46 +00:00
} ;
} ;
2020-03-06 11:43:40 +00:00
const getErrorBoundaryProps = ( ) => {
return {
header : _ _ ( 'Something went wrong…' , 'woo-gutenberg-products-block' ) ,
2020-09-26 13:51:21 +00:00
text : createInterpolateElement (
2020-03-06 11:43:40 +00:00
_ _ (
2020-04-03 13:22:56 +00:00
'The checkout 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
2020-05-08 07:02:18 +00:00
className = "wc-block-link-button"
2020-04-03 13:22:56 +00:00
onClick = { reloadPage }
/ >
2020-03-06 11:43:40 +00:00
) ,
}
) ,
showErrorMessage : CURRENT _USER _IS _ADMIN ,
} ;
} ;
2020-05-27 10:11:30 +00:00
renderFrontend ( {
selector : '.wp-block-woocommerce-checkout' ,
Block : withStoreCartApiHydration (
withRestApiHydration ( CheckoutFrontend )
) ,
2020-03-06 11:43:40 +00:00
getProps ,
2020-05-27 10:11:30 +00:00
getErrorBoundaryProps ,
} ) ;