mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Retrieve current script url params (client id, ...) in editor
This commit is contained in:
parent
2ecbf089f5
commit
7e8881dc9d
5 changed files with 80 additions and 8 deletions
|
@ -3,6 +3,7 @@ import { useState, useEffect } from '@wordpress/element';
|
|||
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
|
||||
import { PanelBody, SelectControl, Spinner } from '@wordpress/components';
|
||||
import { PayPalScriptProvider, PayPalMessages } from "@paypal/react-paypal-js";
|
||||
import { useScriptParams } from "./hooks/script-params";
|
||||
|
||||
export default function Edit( { attributes, clientId, setAttributes } ) {
|
||||
const { layout, logo, position, color, flexColor, flexRatio, placement, id } = attributes;
|
||||
|
@ -23,12 +24,29 @@ export default function Edit( { attributes, clientId, setAttributes } ) {
|
|||
},
|
||||
};
|
||||
|
||||
const props = useBlockProps({className: ['ppcp-paylater-block-preview', 'ppcp-overlay-parent']});
|
||||
|
||||
useEffect(() => {
|
||||
if (!id) {
|
||||
setAttributes({id: 'ppcp-' + clientId});
|
||||
}
|
||||
}, []);
|
||||
|
||||
const scriptParams = useScriptParams(PcpPayLaterBlock.ajax.cart_script_params);
|
||||
if (scriptParams === null) {
|
||||
return (<div {...props}><Spinner/></div>)
|
||||
}
|
||||
|
||||
const urlParams = scriptParams === false ? {
|
||||
clientId: 'test',
|
||||
components: 'messages',
|
||||
} : {
|
||||
...scriptParams.url_params,
|
||||
...{
|
||||
components: 'messages',
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<InspectorControls>
|
||||
|
@ -116,13 +134,10 @@ export default function Edit( { attributes, clientId, setAttributes } ) {
|
|||
/>
|
||||
</PanelBody>
|
||||
</InspectorControls>
|
||||
<div {...useBlockProps({className: ['ppcp-paylater-block-preview', 'ppcp-overlay-parent']})}>
|
||||
<div {...props}>
|
||||
<div className={'ppcp-overlay-child'}>
|
||||
<PayPalScriptProvider
|
||||
options={{
|
||||
clientId: "test",
|
||||
components: "messages",
|
||||
}}
|
||||
options={urlParams}
|
||||
>
|
||||
<PayPalMessages
|
||||
style={previewStyle}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue