| 1 |
import React from 'react'; |
| 2 |
import * as WpBlocksApi from '@wordpress/blocks'; |
| 3 |
import SprocketIcon from '../Common/SprocketIcon'; |
| 4 |
import FormBlockSave from './FormBlockSave'; |
| 5 |
import { connectionStatus } from '../../constants/leadinConfig'; |
| 6 |
import FormGutenbergPreview from './FormGutenbergPreview'; |
| 7 |
import ErrorHandler from '../../shared/Common/ErrorHandler'; |
| 8 |
import FormEdit from '../../shared/Form/FormEdit'; |
| 9 |
import ConnectionStatus from '../../shared/enums/connectionStatus'; |
| 10 |
import { __ } from '@wordpress/i18n'; |
| 11 |
import { isFullSiteEditor } from '../../utils/withMetaData'; |
| 12 |
|
| 13 |
export interface IFormBlockAttributes { |
| 14 |
attributes: { |
| 15 |
portalId: string; |
| 16 |
formId: string; |
| 17 |
preview?: boolean; |
| 18 |
formName: string; |
| 19 |
}; |
| 20 |
} |
| 21 |
|
| 22 |
export interface IFormBlockProps extends IFormBlockAttributes { |
| 23 |
setAttributes: Function; |
| 24 |
isSelected: boolean; |
| 25 |
context?: any; |
| 26 |
} |
| 27 |
|
| 28 |
export default function registerFormBlock() { |
| 29 |
const editComponent = (props: IFormBlockProps) => { |
| 30 |
if (props.attributes.preview) { |
| 31 |
return <FormGutenbergPreview />; |
| 32 |
} else if (connectionStatus === ConnectionStatus.Connected) { |
| 33 |
return <FormEdit {...props} origin="gutenberg" preview={true} />; |
| 34 |
} else { |
| 35 |
return <ErrorHandler status={401} />; |
| 36 |
} |
| 37 |
}; |
| 38 |
|
| 39 |
// We do not support the full site editor: https://issues.hubspotcentral.com/browse/WP-1033 |
| 40 |
if (!WpBlocksApi || isFullSiteEditor()) { |
| 41 |
return null; |
| 42 |
} |
| 43 |
|
| 44 |
WpBlocksApi.registerBlockType('leadin/hubspot-form-block', { |
| 45 |
title: __('HubSpot Form', 'leadin'), |
| 46 |
description: __('Select and embed a HubSpot form', 'leadin'), |
| 47 |
icon: SprocketIcon, |
| 48 |
category: 'leadin-blocks', |
| 49 |
attributes: { |
| 50 |
portalId: { |
| 51 |
type: 'string', |
| 52 |
default: '', |
| 53 |
} as WpBlocksApi.BlockAttribute<string>, |
| 54 |
formId: { |
| 55 |
type: 'string', |
| 56 |
} as WpBlocksApi.BlockAttribute<string>, |
| 57 |
formName: { |
| 58 |
type: 'string', |
| 59 |
} as WpBlocksApi.BlockAttribute<string>, |
| 60 |
preview: { |
| 61 |
type: 'boolean', |
| 62 |
default: false, |
| 63 |
} as WpBlocksApi.BlockAttribute<boolean>, |
| 64 |
}, |
| 65 |
example: { |
| 66 |
attributes: { |
| 67 |
preview: true, |
| 68 |
}, |
| 69 |
}, |
| 70 |
edit: editComponent, |
| 71 |
save: props => <FormBlockSave {...props} />, |
| 72 |
}); |
| 73 |
} |
| 74 |
|