11/* global window */
22
3- import React , { Component } from 'react' ;
3+ import React , { useContext } from 'react' ;
44import PropTypes from 'prop-types' ;
55import { connect } from 'react-redux' ;
66import { bindActionCreators , compose } from 'redux' ;
@@ -18,93 +18,85 @@ import getJsonErrorMessage from 'lib/getJsonErrorMessage';
1818 * The AddElementPopover component used in the context of an ElementEditor shows the
1919 * available elements that can be added to an ElementalArea.
2020 */
21- class AddElementPopover extends Component {
22- constructor ( props ) {
23- super ( props ) ;
24-
25- this . handleToggle = this . handleToggle . bind ( this ) ;
26- AddElementPopover . contextType = ElementEditorContext ;
27- }
28-
29- /**
30- * - Call add element to area endpoint (areaID, elementType, insertAfterElementID)
31- * - Then call read blocks from area endpoint (areaID)
32- * - Then update the preview via jquery/entwine
33- */
34- getElementButtonClickHandler ( elementType ) {
35- return ( event ) => {
36- event . preventDefault ( ) ;
37- const sectionConfigKey = 'DNADesign\\Elemental\\Controllers\\ElementalAreaController' ;
38- const url = `${ Config . getSection ( sectionConfigKey ) . controllerLink } /api/create` ;
39- backend . post ( url , {
40- elementClass : elementType . class ,
41- elementalAreaID : this . props . areaId ,
42- insertAfterElementID : this . props . insertAfterElement ,
43- } , {
44- 'X-SecurityID' : Config . get ( 'SecurityID' )
45- } )
46- . then ( ( ) => {
47- const { fetchElements } = this . context ;
48- return fetchElements ( ) ;
49- } )
50- . then ( ( ) => {
51- const preview = window . jQuery ( '.cms-preview' ) ;
52- preview . entwine ( 'ss.preview' ) . _loadUrl ( preview . find ( 'iframe' ) . attr ( 'src' ) ) ;
53- } )
54- . catch ( async ( err ) => {
55- const message = await getJsonErrorMessage ( err ) ;
56- this . props . actions . toasts . error ( message ) ;
57- } ) ;
58- this . handleToggle ( ) ;
59- } ;
60- }
21+ const AddElementPopover = ( {
22+ PopoverOptionSetComponent,
23+ elementTypes,
24+ container,
25+ extraClass,
26+ isOpen,
27+ placement,
28+ target,
29+ toggle,
30+ areaId,
31+ insertAfterElement,
32+ actions,
33+ } ) => {
34+ const context = useContext ( ElementEditorContext ) ;
6135
6236 /**
6337 * Pass toggle to parent and clear the search input
6438 */
65- handleToggle ( ) {
66- const { toggle } = this . props ;
67-
39+ const handleToggle = ( ) => {
6840 toggle ( ) ;
69- }
41+ } ;
7042
7143 /**
72- * Render the add element popover
73- * @returns {DOMElement }
44+ * - Call add element to area endpoint (areaID, elementType, insertAfterElementID)
45+ * - Then call read blocks from area endpoint (areaID)
46+ * - Then update the preview via jquery/entwine
7447 */
75- render ( ) {
76- const {
77- PopoverOptionSetComponent, elementTypes,
78- container, extraClass, isOpen, placement, target
79- } = this . props ;
48+ const getElementButtonClickHandler = ( elementType ) => ( event ) => {
49+ event . preventDefault ( ) ;
50+ const sectionConfigKey = 'DNADesign\\Elemental\\Controllers\\ElementalAreaController' ;
51+ const url = `${ Config . getSection ( sectionConfigKey ) . controllerLink } /api/create` ;
52+ backend . post ( url , {
53+ elementClass : elementType . class ,
54+ elementalAreaID : areaId ,
55+ insertAfterElementID : insertAfterElement ,
56+ } , {
57+ 'X-SecurityID' : Config . get ( 'SecurityID' )
58+ } )
59+ . then ( ( ) => {
60+ const { fetchElements } = context ;
61+ return fetchElements ( ) ;
62+ } )
63+ . then ( ( ) => {
64+ const preview = window . jQuery ( '.cms-preview' ) ;
65+ preview . entwine ( 'ss.preview' ) . _loadUrl ( preview . find ( 'iframe' ) . attr ( 'src' ) ) ;
66+ } )
67+ . catch ( async ( err ) => {
68+ const message = await getJsonErrorMessage ( err ) ;
69+ actions . toasts . error ( message ) ;
70+ } ) ;
71+ handleToggle ( ) ;
72+ } ;
8073
81- const popoverClassNames = classNames (
82- 'element-editor-add-element' ,
83- extraClass
84- ) ;
74+ const popoverClassNames = classNames (
75+ 'element-editor-add-element' ,
76+ extraClass
77+ ) ;
8578
86- const buttons = elementTypes . map ( ( elementType ) => ( {
87- content : < span className = "btn__title" > { elementType . title } </ span > ,
88- key : elementType . name ,
89- className : classNames ( 'btn--icon-xl' , 'element-editor-add-element__button' ) ,
90- icon : elementType . icon . replace ( / ^ ( f o n t - i c o n - ) / g, '' ) ,
91- onClick : this . getElementButtonClickHandler ( elementType ) ,
92- } ) ) ;
79+ const buttons = elementTypes . map ( ( elementType ) => ( {
80+ content : < span className = "btn__title" > { elementType . title } </ span > ,
81+ key : elementType . name ,
82+ className : classNames ( 'btn--icon-xl' , 'element-editor-add-element__button' ) ,
83+ icon : elementType . icon . replace ( / ^ ( f o n t - i c o n - ) / g, '' ) ,
84+ onClick : getElementButtonClickHandler ( elementType ) ,
85+ } ) ) ;
9386
94- return (
95- < PopoverOptionSetComponent
96- buttons = { buttons }
97- searchPlaceholder = { i18n . _t ( 'ElementAddElementPopover.SEARCH_BLOCKS' , 'Search blocks' ) }
98- extraClass = { popoverClassNames }
99- container = { container }
100- isOpen = { isOpen }
101- placement = { placement }
102- target = { target }
103- toggle = { this . handleToggle }
104- />
105- ) ;
106- }
107- }
87+ return (
88+ < PopoverOptionSetComponent
89+ buttons = { buttons }
90+ searchPlaceholder = { i18n . _t ( 'ElementAddElementPopover.SEARCH_BLOCKS' , 'Search blocks' ) }
91+ extraClass = { popoverClassNames }
92+ container = { container }
93+ isOpen = { isOpen }
94+ placement = { placement }
95+ target = { target }
96+ toggle = { handleToggle }
97+ />
98+ ) ;
99+ } ;
108100
109101AddElementPopover . propTypes = {
110102 container : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . func , PropTypes . object ] ) ,
0 commit comments