2023-12-05 21:35:18 +02:00
import { _ _ } from '@wordpress/i18n' ;
2023-12-06 17:31:56 +02:00
import { useState , useEffect } from '@wordpress/element' ;
2023-12-05 21:35:18 +02:00
import { InspectorControls , useBlockProps } from '@wordpress/block-editor' ;
2023-12-05 21:35:41 +02:00
import { PanelBody , SelectControl , Spinner } from '@wordpress/components' ;
2023-12-07 16:28:30 +02:00
import { useScriptParams } from "./hooks/script-params" ;
2023-12-13 08:47:21 +02:00
import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'
import PayPalMessages from "./components/PayPalMessages" ;
2023-12-05 21:35:18 +02:00
2023-12-06 17:31:56 +02:00
export default function Edit ( { attributes , clientId , setAttributes } ) {
2024-03-14 17:54:02 +04:00
const { layout , logo , position , color , size , flexColor , flexRatio , placement , id } = attributes ;
2023-12-05 21:35:18 +02:00
const isFlex = layout === 'flex' ;
2023-12-13 08:47:21 +02:00
const [ paypalScriptState , setPaypalScriptState ] = useState ( null ) ;
const [ rendered , setRendered ] = useState ( false ) ;
2023-12-05 21:35:41 +02:00
2023-12-14 10:36:15 +02:00
let amount = undefined ;
const postContent = String ( wp . data . select ( 'core/editor' ) ? . getEditedPostContent ( ) ) ;
if ( postContent . includes ( 'woocommerce/checkout' ) || postContent . includes ( 'woocommerce/cart' ) ) {
amount = 50.0 ;
}
2023-12-05 21:35:18 +02:00
const previewStyle = {
layout ,
logo : {
position ,
type : logo ,
} ,
color : flexColor ,
ratio : flexRatio ,
text : {
color ,
2024-03-14 17:54:02 +04:00
size
2023-12-05 21:35:18 +02:00
} ,
} ;
2023-12-06 17:31:56 +02:00
2023-12-20 16:44:38 +02:00
let classes = [ 'ppcp-paylater-block-preview' , 'ppcp-overlay-parent' ] ;
2024-03-12 17:23:55 +04:00
if ( PcpPayLaterBlock . vaultingEnabled || ! PcpPayLaterBlock . placementEnabled ) {
2023-12-20 16:44:38 +02:00
classes = [ 'ppcp-paylater-block-preview' , 'ppcp-paylater-unavailable' , 'block-editor-warning' ] ;
}
const props = useBlockProps ( { className : classes } ) ;
2023-12-07 16:28:30 +02:00
2023-12-13 08:47:21 +02:00
const loadingElement = < div { ... props } > < Spinner / > < / d i v > ;
2023-12-06 17:31:56 +02:00
useEffect ( ( ) => {
if ( ! id ) {
setAttributes ( { id : 'ppcp-' + clientId } ) ;
}
} , [ ] ) ;
2023-12-20 16:44:38 +02:00
if ( PcpPayLaterBlock . vaultingEnabled ) {
return < div { ... props } >
< div className = { 'block-editor-warning__contents' } >
< h3 > { _ _ ( 'PayPal Pay Later Messaging' , 'woocommerce-paypal-payments' ) } < / h 3 >
< p className = { 'block-editor-warning__message' } > { _ _ ( '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 = { 'class="block-editor-warning__actions"' } >
< span className = { 'block-editor-warning__action' } >
< a href = { PcpPayLaterBlock . settingsUrl } className = { 'components-button is-primary' } >
{ _ _ ( 'PayPal Payments Settings' , 'woocommerce-paypal-payments' ) }
< / a >
< / s p a n >
< span className = { 'block-editor-warning__action' } >
< button onClick = { ( ) => wp . data . dispatch ( 'core/block-editor' ) . removeBlock ( clientId ) } type = { 'button' } className = { 'components-button is-secondary' } >
{ _ _ ( 'Remove Block' , 'woocommerce-paypal-payments' ) }
2024-03-12 17:23:55 +04:00
< / b u t t o n >
< / s p a n >
< / d i v >
< / d i v >
< / d i v >
}
if ( ! PcpPayLaterBlock . placementEnabled ) {
return < div { ... props } >
< div className = { 'block-editor-warning__contents' } >
< h3 > { _ _ ( 'PayPal Pay Later Messaging' , 'woocommerce-paypal-payments' ) } < / h 3 >
< p className = { 'block-editor-warning__message' } > { _ _ ( 'Pay Later Messaging cannot be used while the “WooCommerce Block” messaging placement is disabled. Enable the placement in the PayPal Payments Pay Later settings to reactivate this block.' , 'woocommerce-paypal-payments' ) } < / p >
< div className = { 'class="block-editor-warning__actions"' } >
< span className = { 'block-editor-warning__action' } >
< a href = { PcpPayLaterBlock . payLaterSettingsUrl } className = { 'components-button is-primary' } >
{ _ _ ( 'PayPal Payments Settings' , 'woocommerce-paypal-payments' ) }
< / a >
< / s p a n >
< span className = { 'block-editor-warning__action' } >
< button onClick = { ( ) => wp . data . dispatch ( 'core/block-editor' ) . removeBlock ( clientId ) } type = { 'button' } className = { 'components-button is-secondary' } >
{ _ _ ( 'Remove Block' , 'woocommerce-paypal-payments' ) }
2023-12-20 16:44:38 +02:00
< / b u t t o n >
< / s p a n >
< / d i v >
< / d i v >
< / d i v >
}
2023-12-13 08:47:21 +02:00
let scriptParams = useScriptParams ( PcpPayLaterBlock . ajax . cart _script _params ) ;
2023-12-07 16:28:30 +02:00
if ( scriptParams === null ) {
2023-12-13 08:47:21 +02:00
return loadingElement ;
2023-12-07 16:28:30 +02:00
}
2023-12-13 08:47:21 +02:00
if ( scriptParams === false ) {
scriptParams = {
url _params : {
clientId : 'test' ,
}
2023-12-07 16:28:30 +02:00
}
}
2023-12-18 08:01:21 +02:00
scriptParams . url _params . components = 'messages,buttons,funding-eligibility' ;
2023-12-07 16:28:30 +02:00
2023-12-13 08:47:21 +02:00
if ( ! paypalScriptState ) {
loadPaypalScript ( scriptParams , ( ) => {
setPaypalScriptState ( 'loaded' )
} , ( ) => {
setPaypalScriptState ( 'failed' )
} ) ;
}
if ( paypalScriptState !== 'loaded' ) {
return loadingElement ;
}
2023-12-05 21:35:18 +02:00
return (
< >
< InspectorControls >
< PanelBody title = { _ _ ( 'Settings' , 'woocommerce-paypal-payments' ) } >
< SelectControl
label = { _ _ ( 'Layout' , 'woocommerce-paypal-payments' ) }
options = { [
{ label : _ _ ( 'Text' , 'woocommerce-paypal-payments' ) , value : 'text' } ,
{ label : _ _ ( 'Banner' , 'woocommerce-paypal-payments' ) , value : 'flex' } ,
] }
value = { layout }
onChange = { ( value ) => setAttributes ( { layout : value } ) }
/ >
{ ! isFlex && ( < SelectControl
label = { _ _ ( 'Logo' , 'woocommerce-paypal-payments' ) }
options = { [
2024-03-14 17:54:02 +04:00
{ label : _ _ ( 'Full logo' , 'woocommerce-paypal-payments' ) , value : 'primary' } ,
{ label : _ _ ( 'Monogram' , 'woocommerce-paypal-payments' ) , value : 'alternative' } ,
2023-12-05 21:35:18 +02:00
{ label : _ _ ( 'Inline' , 'woocommerce-paypal-payments' ) , value : 'inline' } ,
2024-03-14 17:54:02 +04:00
{ label : _ _ ( 'Message only' , 'woocommerce-paypal-payments' ) , value : 'none' } ,
2023-12-05 21:35:18 +02:00
] }
value = { logo }
onChange = { ( value ) => setAttributes ( { logo : value } ) }
/ > ) }
2023-12-06 17:33:13 +02:00
{ ! isFlex && logo === 'primary' && ( < SelectControl
2023-12-05 21:35:18 +02:00
label = { _ _ ( 'Logo Position' , 'woocommerce-paypal-payments' ) }
options = { [
{ label : _ _ ( 'Left' , 'woocommerce-paypal-payments' ) , value : 'left' } ,
{ label : _ _ ( 'Right' , 'woocommerce-paypal-payments' ) , value : 'right' } ,
{ label : _ _ ( 'Top' , 'woocommerce-paypal-payments' ) , value : 'top' } ,
] }
value = { position }
onChange = { ( value ) => setAttributes ( { position : value } ) }
/ > ) }
{ ! isFlex && ( < SelectControl
label = { _ _ ( 'Text Color' , 'woocommerce-paypal-payments' ) }
options = { [
2024-03-14 17:54:02 +04:00
{ label : _ _ ( 'Black / Blue logo' , 'woocommerce-paypal-payments' ) , value : 'black' } ,
{ label : _ _ ( 'White / White logo' , 'woocommerce-paypal-payments' ) , value : 'white' } ,
2023-12-05 21:35:18 +02:00
{ label : _ _ ( 'Monochrome' , 'woocommerce-paypal-payments' ) , value : 'monochrome' } ,
2024-03-14 17:54:02 +04:00
{ label : _ _ ( 'Black / Gray logo' , 'woocommerce-paypal-payments' ) , value : 'grayscale' } ,
2023-12-05 21:35:18 +02:00
] }
value = { color }
onChange = { ( value ) => setAttributes ( { color : value } ) }
/ > ) }
2024-03-14 17:54:02 +04:00
{ ! isFlex && ( < SelectControl
label = { _ _ ( 'Text Size' , 'woocommerce-paypal-payments' ) }
options = { [
{ label : _ _ ( 'Small' , 'woocommerce-paypal-payments' ) , value : '12' } ,
{ label : _ _ ( 'Medium' , 'woocommerce-paypal-payments' ) , value : '14' } ,
{ label : _ _ ( 'Large' , 'woocommerce-paypal-payments' ) , value : '16' } ,
] }
value = { size }
onChange = { ( value ) => setAttributes ( { size : value } ) }
/ > ) }
2023-12-05 21:35:18 +02:00
{ isFlex && ( < SelectControl
label = { _ _ ( 'Color' , 'woocommerce-paypal-payments' ) }
options = { [
{ label : _ _ ( 'Blue' , 'woocommerce-paypal-payments' ) , value : 'blue' } ,
{ label : _ _ ( 'Black' , 'woocommerce-paypal-payments' ) , value : 'black' } ,
{ label : _ _ ( 'White' , 'woocommerce-paypal-payments' ) , value : 'white' } ,
2024-03-14 17:54:02 +04:00
{ label : _ _ ( 'White (no border)' , 'woocommerce-paypal-payments' ) , value : 'white-no-border' } ,
2023-12-05 21:35:18 +02:00
] }
value = { flexColor }
onChange = { ( value ) => setAttributes ( { flexColor : value } ) }
/ > ) }
{ isFlex && ( < SelectControl
label = { _ _ ( 'Ratio' , 'woocommerce-paypal-payments' ) }
options = { [
{ label : _ _ ( '8x1' , 'woocommerce-paypal-payments' ) , value : '8x1' } ,
{ label : _ _ ( '20x1' , 'woocommerce-paypal-payments' ) , value : '20x1' } ,
] }
value = { flexRatio }
onChange = { ( value ) => setAttributes ( { flexRatio : value } ) }
/ > ) }
2023-12-07 08:15:05 +02:00
< SelectControl
label = { _ _ ( 'Placement page' , 'woocommerce-paypal-payments' ) }
help = { _ _ ( 'Used for the analytics dashboard in the merchant account.' , 'woocommerce-paypal-payments' ) }
options = { [
{ label : _ _ ( 'Detect automatically' , 'woocommerce-paypal-payments' ) , value : 'auto' } ,
{ label : _ _ ( 'Cart' , 'woocommerce-paypal-payments' ) , value : 'cart' } ,
{ label : _ _ ( 'Payment' , 'woocommerce-paypal-payments' ) , value : 'payment' } ,
{ label : _ _ ( 'Product' , 'woocommerce-paypal-payments' ) , value : 'product' } ,
{ label : _ _ ( 'Product list' , 'woocommerce-paypal-payments' ) , value : 'product-list' } ,
{ label : _ _ ( 'Home' , 'woocommerce-paypal-payments' ) , value : 'home' } ,
{ label : _ _ ( 'Category' , 'woocommerce-paypal-payments' ) , value : 'category' } ,
] }
value = { placement }
onChange = { ( value ) => setAttributes ( { placement : value } ) }
/ >
2023-12-05 21:35:18 +02:00
< / 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 >
2023-12-07 16:28:30 +02:00
< div { ... props } >
2023-12-05 21:35:18 +02:00
< div className = { 'ppcp-overlay-child' } >
2023-12-13 08:47:21 +02:00
< PayPalMessages
style = { previewStyle }
2023-12-14 10:36:15 +02:00
amount = { amount }
2023-12-13 08:47:21 +02:00
onRender = { ( ) => setRendered ( true ) }
/ >
2023-12-05 21:35:18 +02:00
< / d i v >
< div className = { 'ppcp-overlay-child ppcp-unclicable-overlay' } > { /* make the message not clickable */ }
2023-12-13 08:47:21 +02:00
{ ! rendered && ( < Spinner / > ) }
2023-12-05 21:35:18 +02:00
< / d i v >
< / d i v >
< / >
) ;
}