| 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 |
|
| 12 |
export interface IFormBlockAttributes { |
| 13 |
attributes: { |
| 14 |
portalId: string; |
| 15 |
formId: string; |
| 16 |
preview?: boolean; |
| 17 |
formName: string; |
| 18 |
}; |
| 19 |
} |
| 20 |
|
| 21 |
export interface IFormBlockProps extends IFormBlockAttributes { |
| 22 |
setAttributes: Function; |
| 23 |
isSelected: boolean; |
| 24 |
} |
| 25 |
|
| 26 |
export default function registerFormBlock() { |
| 27 |
const editComponent = (props: IFormBlockProps) => { |
| 28 |
if (props.attributes.preview) { |
| 29 |
return <FormGutenbergPreview />; |
| 30 |
} else if (connectionStatus === ConnectionStatus.Connected) { |
| 31 |
return <FormEdit {...props} origin="gutenberg" preview={true} />; |
| 32 |
} else { |
| 33 |
return <ErrorHandler status={401} />; |
| 34 |
} |
| 35 |
}; |
| 36 |
|
| 37 |
if (!WpBlocksApi) { |
| 38 |
return null; |
| 39 |
} |
| 40 |
|
| 41 |
WpBlocksApi.registerBlockType('leadin/hubspot-form-block', { |
| 42 |
title: __('HubSpot Form', 'leadin'), |
| 43 |
description: __('Select and embed a HubSpot form', 'leadin'), |
| 44 |
icon: SprocketIcon, |
| 45 |
category: 'leadin-blocks', |
| 46 |
attributes: { |
| 47 |
portalId: { |
| 48 |
type: 'string', |
| 49 |
default: '', |
| 50 |
} as WpBlocksApi.BlockAttribute<string>, |
| 51 |
formId: { |
| 52 |
type: 'string', |
| 53 |
} as WpBlocksApi.BlockAttribute<string>, |
| 54 |
formName: { |
| 55 |
type: 'string', |
| 56 |
} as WpBlocksApi.BlockAttribute<string>, |
| 57 |
preview: { |
| 58 |
type: 'boolean', |
| 59 |
default: false, |
| 60 |
} as WpBlocksApi.BlockAttribute<boolean>, |
| 61 |
}, |
| 62 |
example: { |
| 63 |
attributes: { |
| 64 |
preview: true, |
| 65 |
}, |
| 66 |
}, |
| 67 |
edit: editComponent, |
| 68 |
save: props => <FormBlockSave {...props} />, |
| 69 |
}); |
| 70 |
} |
| 71 |
|