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-13 20:05:45 +00:00
import { StoreNoticesProvider } 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' ;
2019-12-03 14:12:46 +00:00
import renderFrontend from '../../../utils/render-frontend.js' ;
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-03-12 09:41:35 +00:00
const {
cartCoupons ,
2020-03-13 15:49:33 +00:00
cartItems ,
2020-03-12 09:41:35 +00:00
cartTotals ,
shippingRates ,
} = useStoreCart ( ) ;
2020-03-10 15:49:26 +00:00
2020-03-06 11:43:40 +00:00
return (
< BlockErrorBoundary
header = { _ _ (
'Something went wrong…' ,
'woo-gutenberg-products-block'
) }
text = { _ _experimentalCreateInterpolateElement (
_ _ (
'The checkout has encountered an unexpected error. <a>Try reloading the page</a>. If the error persists, please get in touch with us so we can assist.' ,
'woo-gutenberg-products-block'
) ,
{
a : (
// eslint-disable-next-line jsx-a11y/anchor-has-content
< a href = "." / >
) ,
}
) }
showErrorMessage = { CURRENT _USER _IS _ADMIN }
>
2020-03-13 20:05:45 +00:00
< StoreNoticesProvider context = "wc/checkout" >
< Block
{ ... props }
cartCoupons = { cartCoupons }
cartItems = { cartItems }
cartTotals = { cartTotals }
shippingRates = { shippingRates }
/ >
< / S t o r e N o t i c e s P r o v i d e r >
2020-03-06 11:43:40 +00:00
< / B l o c k E r r o r B o u n d a r y >
) ;
2020-03-05 19:54:05 +00:00
} ;
2020-03-04 15:13:38 +00:00
const getProps = ( el ) => {
2020-03-05 13:06:47 +00:00
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-05 13:06:47 +00:00
}
2020-03-09 14:23:16 +00:00
} else {
attributes [ key ] = blockAttributes [ key ] . default ;
2020-03-05 13:06:47 +00:00
}
} ) ;
2019-12-03 14:12:46 +00:00
return {
2020-03-05 13:06:47 +00:00
attributes ,
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 (
_ _ (
'The checkout has encountered an unexpected error. <a>Try reloading the page</a>. If the error persists, please get in touch with us so we can assist.' ,
'woo-gutenberg-products-block'
) ,
{
a : (
// eslint-disable-next-line jsx-a11y/anchor-has-content, jsx-a11y/anchor-is-valid
< a href = "javascript:window.location.reload(true)" / >
) ,
}
) ,
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
) ;