2019-12-03 14:12:46 +00:00
/ * *
* External dependencies
* /
2020-03-06 11:43:40 +00:00
import { _ _ } from '@wordpress/i18n' ;
2019-12-03 14:12:46 +00:00
import { withRestApiHydration } from '@woocommerce/block-hocs' ;
2020-03-05 19:54:05 +00:00
import { useStoreCart } from '@woocommerce/base-hooks' ;
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-10 15:49:26 +00:00
const { cartErrors , shippingRates } = useStoreCart ( ) ;
if ( cartErrors && cartErrors . length > 0 ) {
throw new Error ( cartErrors [ 0 ] . message ) ;
}
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 }
>
< Block { ... props } shippingRates = { shippingRates } / >
< / 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' ) {
if (
el . dataset [ key ] === 'true' ||
el . dataset [ key ] === 'false'
) {
attributes [ key ] = el . dataset [ key ] !== 'false' ;
} else {
attributes [ key ] = el . dataset [ key ] ;
}
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-05 19:54:05 +00:00
withRestApiHydration ( CheckoutFrontend ) ,
2020-03-06 11:43:40 +00:00
getProps ,
getErrorBoundaryProps
2019-12-03 14:12:46 +00:00
) ;