import { __ } from '@wordpress/i18n'; import { useState, useEffect } from '@wordpress/element'; import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; import { PanelBody, SelectControl, Spinner } from '@wordpress/components'; import { useScriptParams } from "./hooks/script-params"; import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading' import PayPalMessages from "./components/PayPalMessages"; export default function Edit( { attributes, clientId, setAttributes } ) { const { layout, logo, position, color, size, flexColor, flexRatio, placement, id } = attributes; const isFlex = layout === 'flex'; const [paypalScriptState, setPaypalScriptState] = useState(null); const [rendered, setRendered] = useState(false); let amount = undefined; const postContent = String(wp.data.select('core/editor')?.getEditedPostContent()); if (postContent.includes('woocommerce/checkout') || postContent.includes('woocommerce/cart')) { amount = 50.0; } const previewStyle = { layout, logo: { position, type: logo, }, color: flexColor, ratio: flexRatio, text: { color, size }, }; let classes = ['ppcp-paylater-block-preview', 'ppcp-overlay-parent']; if (PcpPayLaterBlock.vaultingEnabled || !PcpPayLaterBlock.placementEnabled) { classes = ['ppcp-paylater-block-preview', 'ppcp-paylater-unavailable', 'block-editor-warning']; } const props = useBlockProps({className: classes}); const loadingElement =
; useEffect(() => { if (!id) { setAttributes({id: 'ppcp-' + clientId}); } }, []); if (PcpPayLaterBlock.vaultingEnabled) { return

{__('PayPal Pay Later Messaging', 'woocommerce-paypal-payments')}

{__('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')}

{__('PayPal Payments Settings', 'woocommerce-paypal-payments')}
} if (!PcpPayLaterBlock.placementEnabled) { return

{__('PayPal Pay Later Messaging', 'woocommerce-paypal-payments')}

{__('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')}

{__('PayPal Payments Settings', 'woocommerce-paypal-payments')}
} let scriptParams = useScriptParams(PcpPayLaterBlock.ajax.cart_script_params); if (scriptParams === null) { return loadingElement; } if (scriptParams === false) { scriptParams = { url_params: { clientId: 'test', } } } scriptParams.url_params.components = 'messages,buttons,funding-eligibility'; if (!paypalScriptState) { loadPaypalScript(scriptParams, () => { setPaypalScriptState('loaded') }, () => { setPaypalScriptState('failed') }); } if (paypalScriptState !== 'loaded') { return loadingElement; } return ( <> setAttributes( { layout: value } ) } /> { !isFlex && ( setAttributes({logo: value})} />)} { !isFlex && logo === 'primary' && ( setAttributes({position: value})} />)} { !isFlex && ( setAttributes({color: value})} />)} { !isFlex && ( setAttributes({size: value})} />)} { isFlex && ( setAttributes({flexColor: value})} />)} { isFlex && ( setAttributes({flexRatio: value})} />)} setAttributes( { placement: value } ) } />
setRendered(true)} />
{/* make the message not clickable */} {!rendered && ()}
); }