Update preview logic, render select dialog conditionally
This commit is contained in:
parent
4b9095782d
commit
85973c58ed
|
@ -2,27 +2,16 @@
|
||||||
* WordPress dependencies
|
* WordPress dependencies
|
||||||
*/
|
*/
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Button, Modal, FocusableIframe } from '@wordpress/components';
|
import { Modal, FocusableIframe } from '@wordpress/components';
|
||||||
|
|
||||||
import { baseUrl } from '@/globals';
|
import { baseUrl } from '@/globals';
|
||||||
|
|
||||||
type DialogProps = {
|
type DialogProps = {
|
||||||
|
setOpen: Function;
|
||||||
onSelectForm: Function;
|
onSelectForm: Function;
|
||||||
};
|
};
|
||||||
|
|
||||||
type DialogState = {
|
class SelectDialog extends React.Component<DialogProps> {
|
||||||
isOpen: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
class SelectDialog extends React.Component<DialogProps, DialogState> {
|
|
||||||
constructor( props: any ) {
|
|
||||||
super( props );
|
|
||||||
|
|
||||||
this.state = {
|
|
||||||
isOpen: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
window.addEventListener( 'message', this.handlePostMessage, false );
|
window.addEventListener( 'message', this.handlePostMessage, false );
|
||||||
}
|
}
|
||||||
|
@ -34,21 +23,16 @@ class SelectDialog extends React.Component<DialogProps, DialogState> {
|
||||||
handlePostMessage = ( event: MessageEvent ) => {
|
handlePostMessage = ( event: MessageEvent ) => {
|
||||||
if ( event.origin === baseUrl && event.data.type === 'cog-form-selected' ) {
|
if ( event.origin === baseUrl && event.data.type === 'cog-form-selected' ) {
|
||||||
this.props.onSelectForm( event.data );
|
this.props.onSelectForm( event.data );
|
||||||
this.setState({ isOpen: false }); // Close Dialog
|
this.props.setOpen( false );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>
|
|
||||||
<Button isPrimary={true} onClick={ () => this.setState( { isOpen: true } ) }>
|
|
||||||
Choose a form
|
|
||||||
</Button>
|
|
||||||
{ this.state.isOpen && (
|
|
||||||
<Modal
|
<Modal
|
||||||
title="Cognito Forms"
|
title="Cognito Forms"
|
||||||
className="cognito-modal"
|
className="cognito-modal"
|
||||||
onRequestClose={ () => this.setState( { isOpen: false } ) }
|
onRequestClose={ () => this.props.setOpen( false ) }
|
||||||
shouldCloseOnClickOutside={ false }
|
shouldCloseOnClickOutside={ false }
|
||||||
>
|
>
|
||||||
<FocusableIframe
|
<FocusableIframe
|
||||||
|
@ -56,8 +40,6 @@ class SelectDialog extends React.Component<DialogProps, DialogState> {
|
||||||
src={ `${ baseUrl }/integrations/cms` }
|
src={ `${ baseUrl }/integrations/cms` }
|
||||||
></FocusableIframe>
|
></FocusableIframe>
|
||||||
</Modal>
|
</Modal>
|
||||||
) }
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
79
src/edit.tsx
79
src/edit.tsx
|
@ -1,5 +1,4 @@
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { __ } from '@wordpress/i18n';
|
|
||||||
|
|
||||||
import { BlockEditProps } from '@wordpress/blocks';
|
import { BlockEditProps } from '@wordpress/blocks';
|
||||||
import {
|
import {
|
||||||
|
@ -9,17 +8,23 @@ import {
|
||||||
PanelBody,
|
PanelBody,
|
||||||
PanelRow,
|
PanelRow,
|
||||||
ExternalLink,
|
ExternalLink,
|
||||||
|
Button,
|
||||||
|
Icon,
|
||||||
} from '@wordpress/components';
|
} from '@wordpress/components';
|
||||||
import { edit } from '@wordpress/icons';
|
import { edit, external } from '@wordpress/icons';
|
||||||
|
import { useState } from '@wordpress/element';
|
||||||
import { BlockControls, InspectorControls } from '@wordpress/block-editor';
|
import { BlockControls, InspectorControls } from '@wordpress/block-editor';
|
||||||
|
|
||||||
|
import { BlockAttributes, EmbedMode } from '@/types';
|
||||||
|
import { baseUrl } from './globals';
|
||||||
|
|
||||||
import { ReactComponent as CogIcon } from '@/assets/cogicon.svg';
|
import { ReactComponent as CogIcon } from '@/assets/cogicon.svg';
|
||||||
import SelectDialog from '@/components/select-dialog';
|
import SelectDialog from '@/components/select-dialog';
|
||||||
import PreviewForm from '@/components/preview-form';
|
import PreviewForm from '@/components/preview-form';
|
||||||
import { BlockAttributes, EmbedMode } from '@/types';
|
|
||||||
|
|
||||||
const Edit: React.FC<BlockEditProps<BlockAttributes>> = ( { attributes, setAttributes, className } ) => {
|
const Edit: React.FC<BlockEditProps<BlockAttributes>> = ( { attributes, setAttributes, className } ) => {
|
||||||
|
const [ selectDialogOpen, setSelectDialogOpen ] = useState( false );
|
||||||
|
|
||||||
const handleForm = ( form: { [key: string]: any } ) => {
|
const handleForm = ( form: { [key: string]: any } ) => {
|
||||||
setAttributes( {
|
setAttributes( {
|
||||||
formId: form.formId,
|
formId: form.formId,
|
||||||
|
@ -30,49 +35,21 @@ const Edit: React.FC<BlockEditProps<BlockAttributes>> = ( { attributes, setAttri
|
||||||
} );
|
} );
|
||||||
};
|
};
|
||||||
|
|
||||||
const resetForm = () => {
|
|
||||||
// Create a new object which sets all attributes to null
|
|
||||||
const nullAttributes = Object.keys( attributes ).reduce(
|
|
||||||
( accumulator, current ) => {
|
|
||||||
// @ts-ignore
|
|
||||||
accumulator[ current ] = null;
|
|
||||||
return accumulator;
|
|
||||||
}, {} );
|
|
||||||
|
|
||||||
setAttributes( nullAttributes );
|
|
||||||
};
|
|
||||||
|
|
||||||
if ( ! attributes.formId ) {
|
|
||||||
return (
|
return (
|
||||||
|
<div className={ className }>
|
||||||
|
{ attributes.formId ?
|
||||||
|
// A form has already been selected, so render the form preview.
|
||||||
<div>
|
<div>
|
||||||
<Placeholder
|
|
||||||
icon={ CogIcon }
|
|
||||||
label="Cognito Forms"
|
|
||||||
className={ className }
|
|
||||||
instructions={ __(
|
|
||||||
'Click the button below to choose a form to embed.'
|
|
||||||
) }
|
|
||||||
>
|
|
||||||
<SelectDialog onSelectForm={ handleForm }></SelectDialog>
|
|
||||||
</Placeholder>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
{
|
|
||||||
<BlockControls>
|
<BlockControls>
|
||||||
<ToolbarButton
|
<ToolbarButton
|
||||||
icon={ edit }
|
icon={ edit }
|
||||||
label="Edit"
|
label="Edit"
|
||||||
onClick={ () => resetForm() }
|
onClick={ () => setSelectDialogOpen( true ) }
|
||||||
/>
|
/>
|
||||||
</BlockControls>
|
</BlockControls>
|
||||||
}
|
|
||||||
|
|
||||||
<InspectorControls key="setting">
|
<InspectorControls key="setting">
|
||||||
<PanelBody title="Embed Settings">
|
<PanelBody title="Form Settings">
|
||||||
<PanelRow>
|
<PanelRow>
|
||||||
<RadioControl
|
<RadioControl
|
||||||
label="Embed Mode"
|
label="Embed Mode"
|
||||||
|
@ -96,6 +73,34 @@ const Edit: React.FC<BlockEditProps<BlockAttributes>> = ( { attributes, setAttri
|
||||||
embedCode={ attributes.iframeEmbedCode }
|
embedCode={ attributes.iframeEmbedCode }
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
:
|
||||||
|
// No form has been selected yet, so render the placeholder
|
||||||
|
<div>
|
||||||
|
<Placeholder
|
||||||
|
icon={ CogIcon }
|
||||||
|
label="Cognito Forms"
|
||||||
|
instructions="Choose an existing form to embed or create a new one."
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<Button isPrimary onClick={ () => setSelectDialogOpen( true ) }>
|
||||||
|
Select Form
|
||||||
|
</Button>
|
||||||
|
<Button isTertiary href={ `${ baseUrl }/forms/new` } target="_blank">
|
||||||
|
New Form <Icon icon={ external } size={ 16 }></Icon>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</Placeholder>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
{ selectDialogOpen &&
|
||||||
|
// Selectively render the select dialog
|
||||||
|
<SelectDialog
|
||||||
|
setOpen={ ( val: boolean ) => setSelectDialogOpen( val ) }
|
||||||
|
onSelectForm={ handleForm }
|
||||||
|
></SelectDialog>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -7,18 +7,12 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Remove modal padding
|
// Remove modal padding
|
||||||
.cognito-modal > .components-modal__content {
|
.wp-block-cognito-forms-cognito-embed {
|
||||||
padding: initial;
|
.components-placeholder__label svg {
|
||||||
|
height: 24px;
|
||||||
|
width: 24px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.cognito-modal > .components-modal__content > .components-modal__header {
|
|
||||||
margin: initial;
|
|
||||||
}
|
|
||||||
|
|
||||||
// .cog-wp-embed__preview > iframe {
|
|
||||||
// height: 500px;
|
|
||||||
// }
|
|
||||||
|
|
||||||
.form-preview {
|
.form-preview {
|
||||||
min-height: 200px;
|
min-height: 200px;
|
||||||
|
|
||||||
|
@ -55,3 +49,14 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.cognito-modal {
|
||||||
|
.components-modal__content {
|
||||||
|
padding: initial;
|
||||||
|
}
|
||||||
|
|
||||||
|
.components-modal__content .components-modal__header {
|
||||||
|
margin: initial;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue