2024-04-08 23:35:53 +02:00
import { _ _ } from '@wordpress/i18n' ;
import { useState , useEffect } from '@wordpress/element' ;
import { InspectorControls , useBlockProps } from '@wordpress/block-editor' ;
import { PanelBody , Spinner } from '@wordpress/components' ;
import { PayPalScriptProvider , PayPalMessages } from '@paypal/react-paypal-js' ;
import { useScriptParams } from '../../../../ppcp-paylater-block/resources/js/hooks/script-params' ;
export default function Edit ( { attributes , clientId , setAttributes } ) {
2024-04-17 02:06:41 +02:00
const { id , ppcpId } = attributes ;
2024-04-08 23:35:53 +02:00
const [ loaded , setLoaded ] = useState ( false ) ;
let amount ;
const postContent = String ( wp . data . select ( 'core/editor' ) ? . getEditedPostContent ( ) ) ;
if ( postContent . includes ( 'woocommerce/checkout' ) || postContent . includes ( 'woocommerce/cart' ) ) {
amount = 50.0 ;
}
const cartConfig = PcpCartPayLaterBlock . config . cart ;
// Dynamically setting previewStyle based on the layout attribute
let previewStyle = { } ;
if ( cartConfig . layout === 'flex' ) {
previewStyle = {
layout : cartConfig . layout ,
color : cartConfig . color ,
ratio : cartConfig . ratio ,
} ;
} else {
previewStyle = {
layout : cartConfig . layout ,
logo : {
position : cartConfig [ 'logo-position' ] ,
type : cartConfig [ 'logo-type' ] ,
} ,
text : {
color : cartConfig [ 'text-color' ] ,
size : cartConfig [ 'text-size' ] ,
} ,
} ;
}
let classes = [ 'ppcp-paylater-block-preview' , 'ppcp-overlay-parent' ] ;
if ( PcpCartPayLaterBlock . vaultingEnabled || ! PcpCartPayLaterBlock . placementEnabled ) {
classes = [ ... classes , 'ppcp-paylater-unavailable' , 'block-editor-warning' ] ;
}
const props = useBlockProps ( { className : classes . join ( ' ' ) } ) ;
useEffect ( ( ) => {
2024-04-17 02:06:41 +02:00
if ( ! ppcpId ) {
setAttributes ( { ppcpId : ` ppcp- ${ clientId } ` } ) ;
2024-04-08 23:35:53 +02:00
}
2024-04-17 02:06:41 +02:00
} , [ ppcpId , clientId ] ) ;
2024-04-08 23:35:53 +02:00
if ( PcpCartPayLaterBlock . vaultingEnabled ) {
return (
< div { ... props } >
< div className = 'block-editor-warning__contents' >
< p className = 'block-editor-warning__message' >
{ _ _ ( 'Cart - Pay Later Messaging cannot be used while PayPal Vaulting is active. Disable PayPal Vaulting in the PayPal Payment settings to reactivate this block' , 'woocommerce-paypal-payments' ) }
< / p >
< div className = 'block-editor-warning__actions' >
< span className = 'block-editor-warning__action' >
< a href = { PcpCartPayLaterBlock . settingsUrl } >
< button type = 'button' className = 'components-button is-primary' >
{ _ _ ( 'PayPal Payments Settings' , 'woocommerce-paypal-payments' ) }
< / b u t t o n >
< / a >
< / s p a n >
< / d i v >
< / d i v >
< / d i v >
) ;
}
if ( ! PcpCartPayLaterBlock . placementEnabled ) {
return (
< div { ... props } >
< div className = 'block-editor-warning__contents' >
< p className = 'block-editor-warning__message' >
{ _ _ ( 'Cart - Pay Later Messaging cannot be used while the “Cart” messaging placement is disabled. Enable the placement in the PayPal Payments Pay Later settings to reactivate this block.' , 'woocommerce-paypal-payments' ) }
< / p >
< div className = 'block-editor-warning__actions' >
< span className = 'block-editor-warning__action' >
< a href = { PcpCartPayLaterBlock . payLaterSettingsUrl } >
< button type = 'button' className = 'components-button is-primary' >
{ _ _ ( 'PayPal Payments Settings' , 'woocommerce-paypal-payments' ) }
< / b u t t o n >
< / a >
< / s p a n >
< / d i v >
< / d i v >
< / d i v >
) ;
}
const scriptParams = useScriptParams ( PcpCartPayLaterBlock . ajax . cart _script _params ) ;
if ( scriptParams === null ) {
return < div { ... props } > < Spinner / > < / d i v > ;
}
const urlParams = {
... scriptParams . url _params ,
components : 'messages' ,
dataNamespace : 'ppcp-block-editor-cart-paylater-message' ,
} ;
return (
< >
< InspectorControls >
< PanelBody title = { _ _ ( 'Customize your messaging' , 'woocommerce-paypal-payments' ) } >
< p >
{ _ _ ( 'Choose the layout and color of your messaging in the PayPal Payments Pay Later settings for the “Cart” messaging placement.' , 'woocommerce-paypal-payments' ) }
< / p >
< a href = { PcpCartPayLaterBlock . payLaterSettingsUrl } >
< button type = 'button' className = 'components-button is-primary' >
{ _ _ ( 'PayPal Payments Settings' , 'woocommerce-paypal-payments' ) }
< / b u t t o n >
< / a >
< / P a n e l B o d y >
< / I n s p e c t o r C o n t r o l s >
< div { ... props } >
< div className = 'ppcp-overlay-child' >
< PayPalScriptProvider options = { urlParams } >
< PayPalMessages
style = { previewStyle }
onRender = { ( ) => setLoaded ( true ) }
amount = { amount }
/ >
< / P a y P a l S c r i p t P r o v i d e r >
< / d i v >
< div className = 'ppcp-overlay-child ppcp-unclicable-overlay' > { /* make the message not clickable */ }
{ ! loaded && < Spinner / > }
< / d i v >
< / d i v >
< / >
) ;
}