index.tsx
78 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 | |
| 7 | type DonationFormBlockAppProps = { |
| 8 | formFormat: 'fullForm' | 'newTab' | 'modal' | string; |
| 9 | dataSrc: string; |
| 10 | embedId: string; |
| 11 | openFormButton: string; |
| 12 | formUrl: string; |
| 13 | }; |
| 14 | |
| 15 | /** |
| 16 | * @since 3.2.0 replace form format reveal with new tab. |
| 17 | * @since 3.0.0 |
| 18 | */ |
| 19 | function DonationFormBlockApp({formFormat, dataSrc, embedId, openFormButton, formUrl}: DonationFormBlockAppProps) { |
| 20 | if (formFormat === 'newTab') { |
| 21 | return ( |
| 22 | <a className={'givewp-donation-form-link'} href={formUrl} target={'_blank'} rel={'noopener noreferrer'}> |
| 23 | {openFormButton} |
| 24 | </a> |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | if (formFormat === 'modal' || formFormat === 'reveal') { |
| 29 | return <ModalForm openFormButton={openFormButton} dataSrc={dataSrc} embedId={embedId} />; |
| 30 | } |
| 31 | |
| 32 | return ( |
| 33 | <IframeResizer |
| 34 | id={embedId} |
| 35 | src={dataSrc} |
| 36 | checkOrigin={false} |
| 37 | style={{ |
| 38 | width: '1px', |
| 39 | minWidth: '100%', |
| 40 | border: '0', |
| 41 | }} |
| 42 | /> |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | const roots = document.querySelectorAll('.root-data-givewp-embed'); |
| 47 | |
| 48 | roots.forEach((root) => { |
| 49 | const dataSrc = root.getAttribute('data-src'); |
| 50 | const embedId = root.getAttribute('data-givewp-embed-id'); |
| 51 | const formFormat = root.getAttribute('data-form-format'); |
| 52 | const openFormButton = root.getAttribute('data-open-form-button'); |
| 53 | const formUrl = root.getAttribute('data-form-url'); |
| 54 | |
| 55 | if (createRoot) { |
| 56 | createRoot(root).render( |
| 57 | <DonationFormBlockApp |
| 58 | openFormButton={openFormButton} |
| 59 | formFormat={formFormat} |
| 60 | dataSrc={dataSrc} |
| 61 | embedId={embedId} |
| 62 | formUrl={formUrl} |
| 63 | /> |
| 64 | ); |
| 65 | } else { |
| 66 | render( |
| 67 | <DonationFormBlockApp |
| 68 | openFormButton={openFormButton} |
| 69 | formFormat={formFormat} |
| 70 | dataSrc={dataSrc} |
| 71 | embedId={embedId} |
| 72 | formUrl={formUrl} |
| 73 | />, |
| 74 | root |
| 75 | ); |
| 76 | } |
| 77 | }); |
| 78 |