index.tsx
103 lines
| 1 | import {__} from '@wordpress/i18n'; |
| 2 | import IframeResizer from 'iframe-resizer-react'; |
| 3 | import isRouteInlineRedirect from '@givewp/forms/app/utilities/isRouteInlineRedirect'; |
| 4 | import ModalForm from '@givewp/src/Campaigns/Blocks/shared/components/ModalForm'; |
| 5 | import '../editor/styles/index.scss'; |
| 6 | import renderDonationForm from './renderDonationForm'; |
| 7 | |
| 8 | /** |
| 9 | * @since 3.2.1 Revert the display style value of "fullForm" to "onpage". |
| 10 | * @since 3.1.2 |
| 11 | */ |
| 12 | type DonationFormBlockAppProps = { |
| 13 | formFormat: 'onpage' | 'newTab' | 'modal' | string; |
| 14 | dataSrc: string; |
| 15 | embedId: string; |
| 16 | openFormButton: string; |
| 17 | formUrl: string; |
| 18 | formViewUrl: string; |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * @since 3.4.0 |
| 23 | */ |
| 24 | const inlineRedirectRoutes = ['donation-confirmation-receipt-view']; |
| 25 | |
| 26 | /** |
| 27 | * @since 3.4.0 |
| 28 | */ |
| 29 | const isRedirect = (url: string) => { |
| 30 | const redirectUrl = new URL(url); |
| 31 | const redirectUrlParams = new URLSearchParams(redirectUrl.search); |
| 32 | |
| 33 | return isRouteInlineRedirect(redirectUrlParams, inlineRedirectRoutes); |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * @since 4.3.0 replace ModalForm with Campaigns ModalForm. |
| 38 | * @since 3.4.0 add logic for inline redirects. |
| 39 | * @since 3.2.0 replace form format reveal with new tab. |
| 40 | * @since 3.0.0 |
| 41 | */ |
| 42 | export default function DonationFormBlockApp({ |
| 43 | formFormat, |
| 44 | dataSrc, |
| 45 | embedId, |
| 46 | openFormButton, |
| 47 | formUrl, |
| 48 | formViewUrl, |
| 49 | }: DonationFormBlockAppProps) { |
| 50 | const isFormRedirect = isRedirect(dataSrc); |
| 51 | |
| 52 | if (formFormat === 'newTab') { |
| 53 | return ( |
| 54 | <a |
| 55 | className={'givewp-donation-form-link'} |
| 56 | href={formUrl} |
| 57 | target={'_blank'} |
| 58 | rel={'noopener noreferrer'} |
| 59 | aria-label={`${openFormButton} ${__('Opens in a new tab', 'give')}`} |
| 60 | > |
| 61 | {openFormButton} |
| 62 | </a> |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | if (formFormat === 'modal' || formFormat === 'reveal') { |
| 67 | return ( |
| 68 | <ModalForm |
| 69 | buttonText={openFormButton} |
| 70 | dataSrc={dataSrc} |
| 71 | embedId={embedId} |
| 72 | isFormRedirect={isFormRedirect} |
| 73 | formViewUrl={formViewUrl} |
| 74 | /> |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | return ( |
| 79 | <IframeResizer |
| 80 | title={__('Donation Form', 'give')} |
| 81 | id={embedId} |
| 82 | src={dataSrc} |
| 83 | checkOrigin={false} |
| 84 | heightCalculationMethod={'taggedElement'} |
| 85 | style={{ |
| 86 | width: '1px', |
| 87 | minWidth: '100%', |
| 88 | border: '0', |
| 89 | }} |
| 90 | /> |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | const roots = document.querySelectorAll('.root-data-givewp-embed'); |
| 95 | |
| 96 | /** |
| 97 | * @since 4.7.0 update to use renderDonationForm |
| 98 | * @since 3.22.0 Add locale support |
| 99 | */ |
| 100 | roots?.forEach((root) => { |
| 101 | renderDonationForm(root); |
| 102 | }); |
| 103 |