index.tsx
105 lines
| 1 | import {createRoot, render} from '@wordpress/element'; |
| 2 | import ModalForm from './Components/ModalForm'; |
| 3 | import IframeResizer from 'iframe-resizer-react'; |
| 4 | |
| 5 | import '../editor/styles/index.scss'; |
| 6 | import isRouteInlineRedirect from '@givewp/forms/app/utilities/isRouteInlineRedirect'; |
| 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 3.4.0 add logic for inline redirects. |
| 38 | * @since 3.2.0 replace form format reveal with new tab. |
| 39 | * @since 3.0.0 |
| 40 | */ |
| 41 | function DonationFormBlockApp({formFormat, dataSrc, embedId, openFormButton, formUrl, formViewUrl}: DonationFormBlockAppProps) { |
| 42 | const isFormRedirect = isRedirect(dataSrc); |
| 43 | |
| 44 | if (formFormat === 'newTab') { |
| 45 | return ( |
| 46 | <a className={'givewp-donation-form-link'} href={formUrl} target={'_blank'} rel={'noopener noreferrer'}> |
| 47 | {openFormButton} |
| 48 | </a> |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | if (formFormat === 'modal' || formFormat === 'reveal') { |
| 53 | return <ModalForm openFormButton={openFormButton} dataSrc={dataSrc} embedId={embedId} openByDefault={isFormRedirect} isFormRedirect={isFormRedirect} formViewUrl={formViewUrl} />; |
| 54 | } |
| 55 | |
| 56 | return ( |
| 57 | <IframeResizer |
| 58 | id={embedId} |
| 59 | src={dataSrc} |
| 60 | checkOrigin={false} |
| 61 | style={{ |
| 62 | width: '1px', |
| 63 | minWidth: '100%', |
| 64 | border: '0', |
| 65 | }} |
| 66 | /> |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | const roots = document.querySelectorAll('.root-data-givewp-embed'); |
| 71 | |
| 72 | roots.forEach((root) => { |
| 73 | const dataSrc = root.getAttribute('data-src'); |
| 74 | const embedId = root.getAttribute('data-givewp-embed-id'); |
| 75 | const formFormat = root.getAttribute('data-form-format'); |
| 76 | const openFormButton = root.getAttribute('data-open-form-button'); |
| 77 | const formUrl = root.getAttribute('data-form-url'); |
| 78 | const formViewUrl = root.getAttribute('data-form-view-url'); |
| 79 | |
| 80 | if (createRoot) { |
| 81 | createRoot(root).render( |
| 82 | <DonationFormBlockApp |
| 83 | openFormButton={openFormButton} |
| 84 | formFormat={formFormat} |
| 85 | dataSrc={dataSrc} |
| 86 | embedId={embedId} |
| 87 | formUrl={formUrl} |
| 88 | formViewUrl={formViewUrl} |
| 89 | /> |
| 90 | ); |
| 91 | } else { |
| 92 | render( |
| 93 | <DonationFormBlockApp |
| 94 | openFormButton={openFormButton} |
| 95 | formFormat={formFormat} |
| 96 | dataSrc={dataSrc} |
| 97 | embedId={embedId} |
| 98 | formUrl={formUrl} |
| 99 | formViewUrl={formViewUrl} |
| 100 | />, |
| 101 | root |
| 102 | ); |
| 103 | } |
| 104 | }); |
| 105 |