| 1 |
import { useSelect } from '@wordpress/data'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
import IframeResizer from 'iframe-resizer-react'; |
| 4 |
import ServerSideRender from '@wordpress/server-side-render'; |
| 5 |
import ModalForm from '../../../shared/components/ModalForm'; |
| 6 |
import '../../../shared/components/EntitySelector/styles/index.scss'; |
| 7 |
|
| 8 |
/** |
| 9 |
* @unreleasaed |
| 10 |
*/ |
| 11 |
export interface BlockPreviewProps { |
| 12 |
formId: number; |
| 13 |
clientId: string; |
| 14 |
displayStyle: 'onpage' | 'modal' | 'newTab' | 'reveal'; |
| 15 |
continueButtonTitle: string; |
| 16 |
link: string; |
| 17 |
isLegacyForm: boolean; |
| 18 |
attributes: any; |
| 19 |
isSelected: boolean; |
| 20 |
className: string; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @unreleasaed |
| 25 |
*/ |
| 26 |
export default function BlockPreview({ |
| 27 |
clientId, |
| 28 |
formId, |
| 29 |
displayStyle, |
| 30 |
continueButtonTitle, |
| 31 |
link, |
| 32 |
isLegacyForm, |
| 33 |
attributes, |
| 34 |
isSelected, |
| 35 |
className, |
| 36 |
}: BlockPreviewProps) { |
| 37 |
// @ts-ignore |
| 38 |
const selectedBlock = useSelect((select) => select('core/block-editor').getSelectedBlock(), []); |
| 39 |
const isBlockSelected = selectedBlock?.clientId === clientId; |
| 40 |
|
| 41 |
const iframePointerEvents = isBlockSelected ? 'auto' : 'none'; |
| 42 |
const isModalDisplay = displayStyle === 'modal' || displayStyle === 'reveal'; |
| 43 |
const isNewTabDisplay = displayStyle === 'newTab'; |
| 44 |
|
| 45 |
if (isLegacyForm) { |
| 46 |
return ( |
| 47 |
<div className={`${className}${isSelected ? ' isSelected' : ''}`}> |
| 48 |
<ServerSideRender block="givewp/campaign-form" attributes={attributes} /> |
| 49 |
</div> |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
if (isNewTabDisplay) { |
| 54 |
return ( |
| 55 |
<a className="givewp-donation-form-link" href={link} target="_blank" rel="noopener noreferrer"> |
| 56 |
{continueButtonTitle} |
| 57 |
</a> |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
if (isModalDisplay) { |
| 62 |
return ( |
| 63 |
<ModalForm |
| 64 |
dataSrc={`/?givewp-route=donation-form-view&form-id=${formId}`} |
| 65 |
embedId="" |
| 66 |
buttonText={continueButtonTitle} |
| 67 |
isFormRedirect={false} |
| 68 |
formViewUrl="" |
| 69 |
/> |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
return ( |
| 74 |
<IframeResizer |
| 75 |
title={__('Donation Form', 'give')} |
| 76 |
src={`/?givewp-route=donation-form-view&form-id=${formId}`} |
| 77 |
checkOrigin={false} |
| 78 |
style={{ |
| 79 |
width: '1px', |
| 80 |
minWidth: '100%', |
| 81 |
border: '0', |
| 82 |
pointerEvents: iframePointerEvents, |
| 83 |
}} |
| 84 |
/> |
| 85 |
); |
| 86 |
} |
| 87 |
|