Ensure email and submit are always present even when deleted with columns

[MAILPOET-2609]
This commit is contained in:
Rostislav Wolny
2020-02-19 14:17:22 +01:00
committed by Pavel Dohnal
parent 6b718dfb2c
commit e5f84f9333

View File

@@ -17,6 +17,21 @@ const formatApiErrorMessage = (response) => {
return errorMessage; return errorMessage;
}; };
const findBlock = (blocks, name) => (
blocks.reduce((result, block) => {
if (result) {
return result;
}
if (block.name === name) {
return block;
}
if (Array.isArray(block.innerBlocks) && block.innerBlocks.length) {
return findBlock(block.innerBlocks, name);
}
return null;
}, null)
);
export default { export default {
SAVE_FORM() { SAVE_FORM() {
if (select('mailpoet-form-editor').getIsFormSaving()) { if (select('mailpoet-form-editor').getIsFormSaving()) {
@@ -136,8 +151,8 @@ export default {
BLOCKS_CHANGED_IN_BLOCK_EDITOR(actionData) { BLOCKS_CHANGED_IN_BLOCK_EDITOR(actionData) {
const newBlocks = actionData.blocks; const newBlocks = actionData.blocks;
// Check if both required inputs are present // Check if both required inputs are present
const emailInput = newBlocks.find((block) => block.name === 'mailpoet-form/email-input'); const emailInput = findBlock(newBlocks, 'mailpoet-form/email-input');
const submitInput = newBlocks.find((block) => block.name === 'mailpoet-form/submit-button'); const submitInput = findBlock(newBlocks, 'mailpoet-form/submit-button');
if (emailInput && submitInput) { if (emailInput && submitInput) {
dispatch('mailpoet-form-editor').changeFormBlocks(newBlocks); dispatch('mailpoet-form-editor').changeFormBlocks(newBlocks);
return; return;
@@ -147,14 +162,14 @@ export default {
const currentBlocks = select('mailpoet-form-editor').getFormBlocks(); const currentBlocks = select('mailpoet-form-editor').getFormBlocks();
const fixedBlocks = [...newBlocks]; const fixedBlocks = [...newBlocks];
if (!emailInput) { if (!emailInput) {
let currentEmailInput = currentBlocks.find((block) => block.name === 'mailpoet-form/email-input'); let currentEmailInput = findBlock(currentBlocks, 'mailpoet-form/email-input');
if (!currentEmailInput) { if (!currentEmailInput) {
currentEmailInput = createBlock('mailpoet-form/email-input'); currentEmailInput = createBlock('mailpoet-form/email-input');
} }
fixedBlocks.unshift(currentEmailInput); fixedBlocks.unshift(currentEmailInput);
} }
if (!submitInput) { if (!submitInput) {
let currentSubmit = currentBlocks.find((block) => block.name === 'mailpoet-form/submit-button'); let currentSubmit = findBlock(currentBlocks, 'mailpoet-form/submit-button');
if (!currentSubmit) { if (!currentSubmit) {
currentSubmit = createBlock('mailpoet-form/submit-button'); currentSubmit = createBlock('mailpoet-form/submit-button');
} }