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