Improve typings
This commit is contained in:
parent
42a2dced76
commit
748af6a14f
|
@ -9,17 +9,17 @@ import { Spinner } from '@wordpress/components';
|
||||||
import { baseUrl } from '@/globals';
|
import { baseUrl } from '@/globals';
|
||||||
import { ReactComponent as CogIcon } from '@/assets/cogicon.svg';
|
import { ReactComponent as CogIcon } from '@/assets/cogicon.svg';
|
||||||
|
|
||||||
type PreviewProps = {
|
interface IPreviewProps {
|
||||||
embedCode: string;
|
embedCode: string;
|
||||||
};
|
}
|
||||||
|
|
||||||
type PreviewState = {
|
interface IPreviewState {
|
||||||
uniqueId: string;
|
uniqueId: string;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
};
|
}
|
||||||
|
|
||||||
class PreviewForm extends React.Component<PreviewProps, PreviewState> {
|
class PreviewForm extends React.Component<IPreviewProps, IPreviewState> {
|
||||||
constructor( props: any ) {
|
constructor( props: IPreviewProps ) {
|
||||||
super( props );
|
super( props );
|
||||||
|
|
||||||
// Generate a unique identifier, so that the embed script can be identified
|
// Generate a unique identifier, so that the embed script can be identified
|
||||||
|
@ -39,7 +39,7 @@ class PreviewForm extends React.Component<PreviewProps, PreviewState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Force the embed script to load, which makes Iframes in the editor resize properly.
|
// Force the embed script to load, which makes Iframes in the editor resize properly.
|
||||||
loadEmbedScript( callback?: Function ) {
|
loadEmbedScript( callback?: () => void ) {
|
||||||
const existingScript = document.getElementById( `cog-embed-script-${ this.state.uniqueId }` );
|
const existingScript = document.getElementById( `cog-embed-script-${ this.state.uniqueId }` );
|
||||||
|
|
||||||
if ( ! existingScript ) {
|
if ( ! existingScript ) {
|
||||||
|
|
|
@ -5,16 +5,14 @@ import * as React from 'react';
|
||||||
import { Modal, FocusableIframe } from '@wordpress/components';
|
import { Modal, FocusableIframe } from '@wordpress/components';
|
||||||
|
|
||||||
import { baseUrl } from '@/globals';
|
import { baseUrl } from '@/globals';
|
||||||
|
import { IForm } from '@/types';
|
||||||
|
|
||||||
interface IDialogProps {
|
interface IDialogProps {
|
||||||
setOpen: Function;
|
setOpen: ( val: boolean ) => void;
|
||||||
onSelectForm: Function;
|
onSelectForm: ( form: IForm ) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IDialogState {
|
class SelectDialog extends React.Component<IDialogProps> {
|
||||||
}
|
|
||||||
|
|
||||||
class SelectDialog extends React.Component<IDialogProps, IDialogState> {
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
window.addEventListener( 'message', this.handlePostMessage, false );
|
window.addEventListener( 'message', this.handlePostMessage, false );
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,18 +15,19 @@ import { edit, external } from '@wordpress/icons';
|
||||||
import { useState } from '@wordpress/element';
|
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 { IBlockAttributes, EmbedMode, IForm } from '@/types';
|
||||||
import { baseUrl } from './globals';
|
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';
|
||||||
|
|
||||||
const Edit: React.FC<BlockEditProps<BlockAttributes>> = ( { attributes, setAttributes, className } ) => {
|
const Edit: React.FC<BlockEditProps<IBlockAttributes>> = ( { attributes, setAttributes, className } ) => {
|
||||||
const [ selectDialogOpen, setSelectDialogOpen ] = useState( false );
|
const [ selectDialogOpen, setSelectDialogOpen ] = useState( false );
|
||||||
|
|
||||||
const handleForm = ( form: { [key: string]: any } ) => {
|
const handleForm = ( form: IForm ) => {
|
||||||
setAttributes( {
|
setAttributes( {
|
||||||
|
orgId: form.orgId,
|
||||||
formId: form.formId,
|
formId: form.formId,
|
||||||
seamlessEmbedCode: form.embedCodes.Seamless,
|
seamlessEmbedCode: form.embedCodes.Seamless,
|
||||||
iframeEmbedCode: form.embedCodes.IFrame,
|
iframeEmbedCode: form.embedCodes.IFrame,
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
import { registerBlockType } from '@wordpress/blocks';
|
import { registerBlockType } from '@wordpress/blocks';
|
||||||
|
|
||||||
import { BlockAttributes } from './types';
|
import { IBlockAttributes } from './types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
|
@ -14,7 +14,7 @@ import Save from './save';
|
||||||
|
|
||||||
import { ReactComponent as CogIcon } from './assets/cogicon.svg';
|
import { ReactComponent as CogIcon } from './assets/cogicon.svg';
|
||||||
|
|
||||||
registerBlockType<BlockAttributes>( 'cognito-forms/cognito-embed', {
|
registerBlockType<IBlockAttributes>( 'cognito-forms/cognito-embed', {
|
||||||
title: 'Cognito Forms',
|
title: 'Cognito Forms',
|
||||||
icon: CogIcon,
|
icon: CogIcon,
|
||||||
description: 'Easily build powerful forms.',
|
description: 'Easily build powerful forms.',
|
||||||
|
@ -24,6 +24,9 @@ registerBlockType<BlockAttributes>( 'cognito-forms/cognito-embed', {
|
||||||
html: false,
|
html: false,
|
||||||
},
|
},
|
||||||
attributes: {
|
attributes: {
|
||||||
|
orgId: {
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
formId: {
|
formId: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
},
|
},
|
||||||
|
|
|
@ -2,9 +2,9 @@ import * as React from 'react';
|
||||||
|
|
||||||
import { BlockSaveProps } from '@wordpress/blocks';
|
import { BlockSaveProps } from '@wordpress/blocks';
|
||||||
|
|
||||||
import { EmbedMode, BlockAttributes } from './types';
|
import { EmbedMode, IBlockAttributes } from './types';
|
||||||
|
|
||||||
const Save: React.FC<BlockSaveProps<BlockAttributes>> = ( { attributes: { formId, embedMode, seamlessEmbedCode, iframeEmbedCode } } ) => {
|
const Save: React.FC<BlockSaveProps<IBlockAttributes>> = ( { attributes: { formId, embedMode, seamlessEmbedCode, iframeEmbedCode } } ) => {
|
||||||
let embedCode = '';
|
let embedCode = '';
|
||||||
|
|
||||||
switch ( embedMode ) {
|
switch ( embedMode ) {
|
||||||
|
|
13
src/types.ts
13
src/types.ts
|
@ -3,10 +3,21 @@ export enum EmbedMode {
|
||||||
IFrame,
|
IFrame,
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BlockAttributes {
|
export interface IBlockAttributes {
|
||||||
|
orgId: string;
|
||||||
formId: string;
|
formId: string;
|
||||||
iframeEmbedCode: string;
|
iframeEmbedCode: string;
|
||||||
seamlessEmbedCode: string;
|
seamlessEmbedCode: string;
|
||||||
ampEmbedCode: string;
|
ampEmbedCode: string;
|
||||||
embedMode: EmbedMode;
|
embedMode: EmbedMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IForm {
|
||||||
|
orgId: string;
|
||||||
|
formId: string;
|
||||||
|
embedCodes: {
|
||||||
|
Seamless: string;
|
||||||
|
IFrame: string;
|
||||||
|
Amp: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue