index.tsx
59 lines
| 1 | import {useState} from 'react'; |
| 2 | import {__} from '@wordpress/i18n'; |
| 3 | import styles from './CreateEventModal.module.scss'; |
| 4 | import EventFormModal from '../EventFormModal'; |
| 5 | |
| 6 | /** |
| 7 | * Auto open modal if the URL has the query parameter id as new |
| 8 | * |
| 9 | * @since 3.6.0 |
| 10 | */ |
| 11 | const autoOpenModal = () => { |
| 12 | const queryParams = new URLSearchParams(window.location.search); |
| 13 | const newParam = queryParams.get('new'); |
| 14 | |
| 15 | return newParam === 'event'; |
| 16 | }; |
| 17 | |
| 18 | /** |
| 19 | * Create Event Modal component |
| 20 | * |
| 21 | * @since 3.6.0 |
| 22 | */ |
| 23 | export default function CreateEventModal() { |
| 24 | const [isOpen, setOpen] = useState<boolean>(autoOpenModal()); |
| 25 | const openModal = () => setOpen(true); |
| 26 | const closeModal = (response: ResponseProps = {}) => { |
| 27 | setOpen(false); |
| 28 | |
| 29 | if (response?.id) { |
| 30 | window.location.href = |
| 31 | window.GiveEventTickets.adminUrl + |
| 32 | 'edit.php?post_type=give_forms&page=give-event-tickets&id=' + |
| 33 | response?.id; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | const apiSettings = window.GiveEventTickets; |
| 38 | // Remove the /list-table from the apiRoot. This is a hack to make the API work while we don't refactor other list tables. |
| 39 | apiSettings.apiRoot = apiSettings.apiRoot.replace('/list-table', ''); |
| 40 | |
| 41 | return ( |
| 42 | <> |
| 43 | <a className={`button button-primary ${styles.createEventButton}`} onClick={openModal}> |
| 44 | {__('Create event', 'give')} |
| 45 | </a> |
| 46 | <EventFormModal |
| 47 | isOpen={isOpen} |
| 48 | handleClose={closeModal} |
| 49 | title={__('Create your event', 'give')} |
| 50 | apiSettings={apiSettings} |
| 51 | /> |
| 52 | </> |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | type ResponseProps = { |
| 57 | id?: string; |
| 58 | }; |
| 59 |