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 {
withRestApiHydration ,
withStoreCartApiHydration ,
} from '@woocommerce/block-hocs' ;
2020-03-05 19:54:05 +00:00
import { useStoreCart } from '@woocommerce/base-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' ;
import { CURRENT _USER _IS _ADMIN } from '@woocommerce/block-settings' ;
import { _ _experimentalCreateInterpolateElement } from 'wordpress-element' ;
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-04-27 16:06:58 +00:00
import {
getAttributesFromDataset ,
renderFrontend ,
} from '../../../utils/render-frontend.js' ;
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'
) }
text = { _ _experimentalCreateInterpolateElement (
_ _ (
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-04-27 16:06:58 +00:00
attributes : getAttributesFromDataset ( 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' ) ,
text : _ _experimentalCreateInterpolateElement (
_ _ (
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 ,
} ;
} ;
2019-12-03 14:12:46 +00:00
renderFrontend (
'.wp-block-woocommerce-checkout' ,
2020-03-12 09:24:50 +00:00
withStoreCartApiHydration ( withRestApiHydration ( CheckoutFrontend ) ) ,
2020-03-06 11:43:40 +00:00
getProps ,
getErrorBoundaryProps
2019-12-03 14:12:46 +00:00
) ;