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, flexColor, flexRatio, placement, id } = attributes; const isFlex = layout === 'flex'; const [paypalScriptState, setPaypalScriptState] = useState(null); const [rendered, setRendered] = useState(false); const previewStyle = { layout, logo: { position, type: logo, }, color: flexColor, ratio: flexRatio, text: { color, }, }; const props = useBlockProps({className: ['ppcp-paylater-block-preview', 'ppcp-overlay-parent']}); const loadingElement =
; useEffect(() => { if (!id) { setAttributes({id: 'ppcp-' + clientId}); } }, []); let scriptParams = useScriptParams(PcpPayLaterBlock.ajax.cart_script_params); if (scriptParams === null) { return loadingElement; } if (scriptParams === false) { scriptParams = { url_params: { clientId: 'test', 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({flexColor: value})} />)} { isFlex && ( setAttributes({flexRatio: value})} />)} setAttributes( { placement: value } ) } />
setRendered(true)} />
{/* make the message not clickable */} {!rendered && ()}
); }