| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; |
| 3 |
import { PanelBody, SelectControl, Placeholder, Spinner } from '@wordpress/components'; |
| 4 |
import { useSelect } from '@wordpress/data'; |
| 5 |
import { store as coreStore } from '@wordpress/core-data'; |
| 6 |
import ServerSideRender from '@wordpress/server-side-render'; |
| 7 |
|
| 8 |
export default function Edit( { attributes, setAttributes } ) { |
| 9 |
const { campaignId } = attributes; |
| 10 |
const blockProps = useBlockProps(); |
| 11 |
|
| 12 |
const campaigns = useSelect( ( select ) => { |
| 13 |
return select( coreStore ).getEntityRecords( 'postType', 'bp_campaign', { |
| 14 |
per_page: 100, |
| 15 |
status: 'publish,draft', |
| 16 |
_fields: 'id,title', |
| 17 |
} ); |
| 18 |
}, [] ); |
| 19 |
|
| 20 |
const campaignOptions = campaigns |
| 21 |
? [ |
| 22 |
{ value: 0, label: __( '— Select a campaign —', 'better-payment' ) }, |
| 23 |
...campaigns.map( ( c ) => ( { value: c.id, label: c.title?.rendered || c.title?.raw || String( c.id ) } ) ), |
| 24 |
] |
| 25 |
: [ { value: 0, label: __( 'Loading campaigns…', 'better-payment' ) } ]; |
| 26 |
|
| 27 |
return ( |
| 28 |
<> |
| 29 |
<InspectorControls> |
| 30 |
<PanelBody title={ __( 'Campaign', 'better-payment' ) }> |
| 31 |
<SelectControl |
| 32 |
label={ __( 'Select Campaign', 'better-payment' ) } |
| 33 |
value={ campaignId } |
| 34 |
options={ campaignOptions } |
| 35 |
onChange={ ( val ) => setAttributes( { campaignId: parseInt( val, 10 ) } ) } |
| 36 |
/> |
| 37 |
{ campaignId > 0 && ( |
| 38 |
<p> |
| 39 |
<a |
| 40 |
href={ `${ window.betterPaymentAdmin?.adminUrl || '' }admin.php?page=bp-campaign-builder&campaign_id=${ campaignId }` } |
| 41 |
target="_blank" |
| 42 |
rel="noreferrer" |
| 43 |
> |
| 44 |
{ __( 'Edit in Campaign Builder →', 'better-payment' ) } |
| 45 |
</a> |
| 46 |
</p> |
| 47 |
) } |
| 48 |
</PanelBody> |
| 49 |
</InspectorControls> |
| 50 |
|
| 51 |
<div { ...blockProps }> |
| 52 |
{ ! campaigns && <Spinner /> } |
| 53 |
{ campaigns && ! campaignId && ( |
| 54 |
<Placeholder |
| 55 |
icon="heart" |
| 56 |
label={ __( 'Campaign Display', 'better-payment' ) } |
| 57 |
instructions={ __( 'Select a campaign from the block settings panel.', 'better-payment' ) } |
| 58 |
/> |
| 59 |
) } |
| 60 |
{ campaignId > 0 && ( |
| 61 |
<ServerSideRender |
| 62 |
block="better-payment/campaign-display" |
| 63 |
attributes={ attributes } |
| 64 |
/> |
| 65 |
) } |
| 66 |
</div> |
| 67 |
</> |
| 68 |
); |
| 69 |
} |
| 70 |
|