Unregister block on custom field delete
[MAILPOET-2463]
This commit is contained in:
committed by
Rostislav Wolný
parent
66d6b57175
commit
f2b04fe94c
@ -2,6 +2,7 @@ import { registerBlockType, setCategories } from '@wordpress/blocks';
|
|||||||
import { select } from '@wordpress/data';
|
import { select } from '@wordpress/data';
|
||||||
import MailPoet from 'mailpoet';
|
import MailPoet from 'mailpoet';
|
||||||
import formatCustomFieldBlockName from './format_custom_field_block_name.jsx';
|
import formatCustomFieldBlockName from './format_custom_field_block_name.jsx';
|
||||||
|
import getCustomFieldBlocksSettings from './custom_fields_blocks.jsx';
|
||||||
|
|
||||||
import * as divider from './divider/divider.jsx';
|
import * as divider from './divider/divider.jsx';
|
||||||
import * as email from './email/email.jsx';
|
import * as email from './email/email.jsx';
|
||||||
@ -11,40 +12,9 @@ import * as lastName from './last_name/last_name.jsx';
|
|||||||
import * as segmentSelect from './segment_select/segment_select.jsx';
|
import * as segmentSelect from './segment_select/segment_select.jsx';
|
||||||
import * as html from './html/html.jsx';
|
import * as html from './html/html.jsx';
|
||||||
|
|
||||||
import * as customDate from './custom_date/custom_date.jsx';
|
|
||||||
import * as customText from './custom_text/custom_text.jsx';
|
|
||||||
import * as customTextArea from './custom_textarea/custom_textarea.jsx';
|
|
||||||
import * as customRadio from './custom_radio/custom_radio.jsx';
|
|
||||||
import * as customCheckbox from './custom_checkbox/custom_checkbox.jsx';
|
|
||||||
import * as customSelect from './custom_select/custom_select.jsx';
|
|
||||||
|
|
||||||
const registerCustomFieldBlock = (customField) => {
|
const registerCustomFieldBlock = (customField) => {
|
||||||
const namesMap = {
|
const namesMap = getCustomFieldBlocksSettings(customField);
|
||||||
date: {
|
|
||||||
name: customDate.name,
|
|
||||||
settings: customDate.getSettings(customField),
|
|
||||||
},
|
|
||||||
text: {
|
|
||||||
name: customText.name,
|
|
||||||
settings: customText.getSettings(customField),
|
|
||||||
},
|
|
||||||
textarea: {
|
|
||||||
name: customTextArea.name,
|
|
||||||
settings: customTextArea.getSettings(customField),
|
|
||||||
},
|
|
||||||
radio: {
|
|
||||||
name: customRadio.name,
|
|
||||||
settings: customRadio.getSettings(customField),
|
|
||||||
},
|
|
||||||
checkbox: {
|
|
||||||
name: customCheckbox.name,
|
|
||||||
settings: customCheckbox.getSettings(customField),
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
name: customSelect.name,
|
|
||||||
settings: customSelect.getSettings(customField),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!namesMap[customField.type]) return;
|
if (!namesMap[customField.type]) return;
|
||||||
|
|
||||||
|
35
assets/js/src/form_editor/blocks/custom_fields_blocks.jsx
Normal file
35
assets/js/src/form_editor/blocks/custom_fields_blocks.jsx
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import * as customDate from './custom_date/custom_date.jsx';
|
||||||
|
import * as customText from './custom_text/custom_text.jsx';
|
||||||
|
import * as customTextArea from './custom_textarea/custom_textarea.jsx';
|
||||||
|
import * as customRadio from './custom_radio/custom_radio.jsx';
|
||||||
|
import * as customCheckbox from './custom_checkbox/custom_checkbox.jsx';
|
||||||
|
import * as customSelect from './custom_select/custom_select.jsx';
|
||||||
|
|
||||||
|
export default function getCustomFieldBlocksSettings(customField) {
|
||||||
|
return {
|
||||||
|
date: {
|
||||||
|
name: customDate.name,
|
||||||
|
settings: customDate.getSettings(customField),
|
||||||
|
},
|
||||||
|
text: {
|
||||||
|
name: customText.name,
|
||||||
|
settings: customText.getSettings(customField),
|
||||||
|
},
|
||||||
|
textarea: {
|
||||||
|
name: customTextArea.name,
|
||||||
|
settings: customTextArea.getSettings(customField),
|
||||||
|
},
|
||||||
|
radio: {
|
||||||
|
name: customRadio.name,
|
||||||
|
settings: customRadio.getSettings(customField),
|
||||||
|
},
|
||||||
|
checkbox: {
|
||||||
|
name: customCheckbox.name,
|
||||||
|
settings: customCheckbox.getSettings(customField),
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
name: customSelect.name,
|
||||||
|
settings: customSelect.getSettings(customField),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
@ -1,7 +1,10 @@
|
|||||||
import { select, dispatch } from '@wordpress/data';
|
import { select, dispatch } from '@wordpress/data';
|
||||||
import MailPoet from 'mailpoet';
|
import MailPoet from 'mailpoet';
|
||||||
import { merge } from 'lodash';
|
import { merge } from 'lodash';
|
||||||
|
import { unregisterBlockType } from '@wordpress/blocks';
|
||||||
import blocksToFormBody from './blocks_to_form_body.jsx';
|
import blocksToFormBody from './blocks_to_form_body.jsx';
|
||||||
|
import formatCustomFieldBlockName from '../blocks/format_custom_field_block_name.jsx';
|
||||||
|
import getCustomFieldBlockSettings from '../blocks/custom_fields_blocks.jsx';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
SAVE_FORM() {
|
SAVE_FORM() {
|
||||||
@ -64,6 +67,9 @@ export default {
|
|||||||
|
|
||||||
DELETE_CUSTOM_FIELD(actionData) {
|
DELETE_CUSTOM_FIELD(actionData) {
|
||||||
dispatch('mailpoet-form-editor').deleteCustomFieldStarted();
|
dispatch('mailpoet-form-editor').deleteCustomFieldStarted();
|
||||||
|
const customFields = select('mailpoet-form-editor').getAllAvailableCustomFields();
|
||||||
|
const customField = customFields.find((cf) => cf.id === actionData.customFieldId);
|
||||||
|
const namesMap = getCustomFieldBlockSettings(customField);
|
||||||
MailPoet.Ajax.post({
|
MailPoet.Ajax.post({
|
||||||
api_version: window.mailpoet_api_version,
|
api_version: window.mailpoet_api_version,
|
||||||
endpoint: 'customFields',
|
endpoint: 'customFields',
|
||||||
@ -75,6 +81,9 @@ export default {
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
dispatch('mailpoet-form-editor').deleteCustomFieldDone(actionData.customFieldId, actionData.clientId);
|
dispatch('mailpoet-form-editor').deleteCustomFieldDone(actionData.customFieldId, actionData.clientId);
|
||||||
dispatch('core/block-editor').removeBlock(actionData.clientId);
|
dispatch('core/block-editor').removeBlock(actionData.clientId);
|
||||||
|
unregisterBlockType(
|
||||||
|
formatCustomFieldBlockName(namesMap[customField.type].name, customField)
|
||||||
|
);
|
||||||
})
|
})
|
||||||
.fail((response) => {
|
.fail((response) => {
|
||||||
let errorMessage = null;
|
let errorMessage = null;
|
||||||
|
Reference in New Issue
Block a user