|
| 1 | +/* global window */ |
| 2 | +import i18n from 'i18n'; |
| 3 | +import jQuery from 'jquery'; |
| 4 | +import React from 'react'; |
| 5 | +import { createRoot } from 'react-dom/client'; |
| 6 | +import { loadComponent } from 'lib/Injector'; |
| 7 | + |
| 8 | +const FormBuilderModal = loadComponent('FormBuilderModal'); |
| 9 | + |
| 10 | +jQuery.entwine('ss', ($) => { |
| 11 | + /** |
| 12 | + * Kick off an "add to campaign" dialog from the CMS actions. |
| 13 | + */ |
| 14 | + $( |
| 15 | + '.cms-content-actions .add-to-campaign-action,' + |
| 16 | + '#add-to-campaign__action' |
| 17 | + ).entwine({ |
| 18 | + onclick() { |
| 19 | + let dialog = $('#add-to-campaign__dialog-wrapper'); |
| 20 | + |
| 21 | + if (!dialog.length) { |
| 22 | + dialog = $('<div id="add-to-campaign__dialog-wrapper" />'); |
| 23 | + $('body').append(dialog); |
| 24 | + } |
| 25 | + |
| 26 | + dialog.open(); |
| 27 | + |
| 28 | + return false; |
| 29 | + }, |
| 30 | + }); |
| 31 | + |
| 32 | + // This is required because the React version of e.preventDefault() doesn't work |
| 33 | + // this is to prevent PJAX request to occur when clicking a link the modal |
| 34 | + $('.add-to-campaign-modal .add-to-campaign-modal__nav-link').entwine({ |
| 35 | + onclick: (e) => { |
| 36 | + e.preventDefault(); |
| 37 | + const $link = $(e.target); |
| 38 | + window.location = $link.attr('href'); |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + /** |
| 43 | + * Uses reactstrap in order to replicate the bootstrap styling and JavaScript behaviour. |
| 44 | + * The "add to campaign" dialog is used in a similar fashion in AssetAdmin. |
| 45 | + */ |
| 46 | + $('#add-to-campaign__dialog-wrapper').entwine({ |
| 47 | + ReactRoot: null, |
| 48 | + |
| 49 | + onunmatch() { |
| 50 | + // solves errors given by ReactDOM "no matched root found" error. |
| 51 | + this._clearModal(); |
| 52 | + }, |
| 53 | + |
| 54 | + open() { |
| 55 | + this._renderModal(true); |
| 56 | + }, |
| 57 | + |
| 58 | + close() { |
| 59 | + this._renderModal(false); |
| 60 | + }, |
| 61 | + |
| 62 | + _renderModal(isOpen) { |
| 63 | + const handleHide = () => this.close(); |
| 64 | + const handleSubmit = (...args) => this._handleSubmitModal(...args); |
| 65 | + const id = $('form.cms-edit-form :input[name=ID]').val(); |
| 66 | + const sectionConfigKey = 'SilverStripe\\CMS\\Controllers\\CMSPageEditController'; |
| 67 | + const store = window.ss.store; |
| 68 | + const sectionConfig = store.getState().config.sections |
| 69 | + .find((section) => section.name === sectionConfigKey); |
| 70 | + const modalSchemaUrl = `${sectionConfig.form.AddToCampaignForm.schemaUrl}/${id}`; |
| 71 | + const title = i18n._t('Admin.ADD_TO_CAMPAIGN', 'Add to campaign'); |
| 72 | + |
| 73 | + let root = this.getReactRoot(); |
| 74 | + if (!root) { |
| 75 | + root = createRoot(this[0]); |
| 76 | + } |
| 77 | + root.render( |
| 78 | + <FormBuilderModal |
| 79 | + title={title} |
| 80 | + isOpen={isOpen} |
| 81 | + onSubmit={handleSubmit} |
| 82 | + onClosed={handleHide} |
| 83 | + schemaUrl={modalSchemaUrl} |
| 84 | + bodyClassName="modal__dialog" |
| 85 | + className="add-to-campaign-modal" |
| 86 | + responseClassBad="modal__response modal__response--error" |
| 87 | + responseClassGood="modal__response modal__response--good" |
| 88 | + identifier="Admin.AddToCampaign" |
| 89 | + /> |
| 90 | + ); |
| 91 | + this.setReactRoot(root); |
| 92 | + }, |
| 93 | + |
| 94 | + _clearModal() { |
| 95 | + const root = this.getReactRoot(); |
| 96 | + if (root) { |
| 97 | + root.unmount(); |
| 98 | + this.setReactRoot(null); |
| 99 | + } |
| 100 | + // this.empty(); |
| 101 | + }, |
| 102 | + |
| 103 | + _handleSubmitModal(data, action, submitFn) { |
| 104 | + return submitFn(); |
| 105 | + }, |
| 106 | + |
| 107 | + }); |
| 108 | +}); |
0 commit comments