Add mapping of block styles attribute to API data format
[MAILPOET-2599]
This commit is contained in:
committed by
Veljko V
parent
251c9ffd38
commit
e6ceb97b18
@@ -1,5 +1,16 @@
|
|||||||
import { has } from 'lodash';
|
import { has } from 'lodash';
|
||||||
|
|
||||||
|
const mapBlockStyles = (styles) => {
|
||||||
|
const mappedStyles = {
|
||||||
|
full_width: styles.fullWidth ? '1' : '0',
|
||||||
|
};
|
||||||
|
if (styles.inheritFromTheme) {
|
||||||
|
return mappedStyles;
|
||||||
|
}
|
||||||
|
mappedStyles.bold = styles.bold ? '1' : '0';
|
||||||
|
return mappedStyles;
|
||||||
|
};
|
||||||
|
|
||||||
const mapCustomField = (block, customFields, mappedCommonProperties) => {
|
const mapCustomField = (block, customFields, mappedCommonProperties) => {
|
||||||
const customField = customFields.find((cf) => cf.id === block.attributes.customFieldId);
|
const customField = customFields.find((cf) => cf.id === block.attributes.customFieldId);
|
||||||
if (!customField) return null;
|
if (!customField) return null;
|
||||||
@@ -13,9 +24,11 @@ const mapCustomField = (block, customFields, mappedCommonProperties) => {
|
|||||||
}
|
}
|
||||||
if (block.name.startsWith('mailpoet-form/custom-text')) {
|
if (block.name.startsWith('mailpoet-form/custom-text')) {
|
||||||
mapped.type = 'text';
|
mapped.type = 'text';
|
||||||
|
mapped.styles = mapBlockStyles(block.attributes.styles);
|
||||||
}
|
}
|
||||||
if (block.name.startsWith('mailpoet-form/custom-textarea')) {
|
if (block.name.startsWith('mailpoet-form/custom-textarea')) {
|
||||||
mapped.type = 'textarea';
|
mapped.type = 'textarea';
|
||||||
|
mapped.styles = mapBlockStyles(block.attributes.styles);
|
||||||
}
|
}
|
||||||
if (block.name.startsWith('mailpoet-form/custom-radio')) {
|
if (block.name.startsWith('mailpoet-form/custom-radio')) {
|
||||||
mapped.type = 'radio';
|
mapped.type = 'radio';
|
||||||
@@ -74,6 +87,9 @@ export const mapColorSlugToValue = (colorDefinitions, colorSlug, colorValue = nu
|
|||||||
* @param customFields - list of all custom Fields
|
* @param customFields - list of all custom Fields
|
||||||
*/
|
*/
|
||||||
export const blocksToFormBodyFactory = (colorDefinitions, customFields = []) => {
|
export const blocksToFormBodyFactory = (colorDefinitions, customFields = []) => {
|
||||||
|
if (!Array.isArray(customFields)) {
|
||||||
|
throw new Error('Mapper expects customFields to be an array.');
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @param blocks
|
* @param blocks
|
||||||
* @param parent - parent block of nested block
|
* @param parent - parent block of nested block
|
||||||
@@ -83,9 +99,6 @@ export const blocksToFormBodyFactory = (colorDefinitions, customFields = []) =>
|
|||||||
if (!Array.isArray(blocks)) {
|
if (!Array.isArray(blocks)) {
|
||||||
throw new Error('Mapper expects blocks to be an array.');
|
throw new Error('Mapper expects blocks to be an array.');
|
||||||
}
|
}
|
||||||
if (!Array.isArray(customFields)) {
|
|
||||||
throw new Error('Mapper expects customFields to be an array.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return blocks.map((block) => {
|
return blocks.map((block) => {
|
||||||
const mapped = {
|
const mapped = {
|
||||||
@@ -158,18 +171,21 @@ export const blocksToFormBodyFactory = (colorDefinitions, customFields = []) =>
|
|||||||
...mapped.params,
|
...mapped.params,
|
||||||
required: '1',
|
required: '1',
|
||||||
},
|
},
|
||||||
|
styles: mapBlockStyles(block.attributes.styles),
|
||||||
};
|
};
|
||||||
case 'mailpoet-form/first-name-input':
|
case 'mailpoet-form/first-name-input':
|
||||||
return {
|
return {
|
||||||
...mapped,
|
...mapped,
|
||||||
id: 'first_name',
|
id: 'first_name',
|
||||||
name: 'First name',
|
name: 'First name',
|
||||||
|
styles: mapBlockStyles(block.attributes.styles),
|
||||||
};
|
};
|
||||||
case 'mailpoet-form/last-name-input':
|
case 'mailpoet-form/last-name-input':
|
||||||
return {
|
return {
|
||||||
...mapped,
|
...mapped,
|
||||||
id: 'last_name',
|
id: 'last_name',
|
||||||
name: 'Last name',
|
name: 'Last name',
|
||||||
|
styles: mapBlockStyles(block.attributes.styles),
|
||||||
};
|
};
|
||||||
case 'mailpoet-form/segment-select':
|
case 'mailpoet-form/segment-select':
|
||||||
return {
|
return {
|
||||||
|
@@ -167,7 +167,10 @@ export const formBodyToBlocksFactory = (colorDefinitions, customFields = []) =>
|
|||||||
return {
|
return {
|
||||||
...mapped,
|
...mapped,
|
||||||
name: 'mailpoet-form/email-input',
|
name: 'mailpoet-form/email-input',
|
||||||
styles: backwardCompatibleBlockStyles,
|
attributes: {
|
||||||
|
...mapped.attributes,
|
||||||
|
styles: backwardCompatibleBlockStyles,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
case 'heading':
|
case 'heading':
|
||||||
if (item.params && has(item.params, 'level')) {
|
if (item.params && has(item.params, 'level')) {
|
||||||
@@ -192,13 +195,19 @@ export const formBodyToBlocksFactory = (colorDefinitions, customFields = []) =>
|
|||||||
return {
|
return {
|
||||||
...mapped,
|
...mapped,
|
||||||
name: 'mailpoet-form/first-name-input',
|
name: 'mailpoet-form/first-name-input',
|
||||||
styles: backwardCompatibleBlockStyles,
|
attributes: {
|
||||||
|
...mapped.attributes,
|
||||||
|
styles: backwardCompatibleBlockStyles,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
case 'last_name':
|
case 'last_name':
|
||||||
return {
|
return {
|
||||||
...mapped,
|
...mapped,
|
||||||
name: 'mailpoet-form/last-name-input',
|
name: 'mailpoet-form/last-name-input',
|
||||||
styles: backwardCompatibleBlockStyles,
|
attributes: {
|
||||||
|
...mapped.attributes,
|
||||||
|
styles: backwardCompatibleBlockStyles,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
case 'segments':
|
case 'segments':
|
||||||
if (
|
if (
|
||||||
|
@@ -6,6 +6,10 @@ export const emailBlock = {
|
|||||||
attributes: {
|
attributes: {
|
||||||
label: 'Email Address',
|
label: 'Email Address',
|
||||||
labelWithinInput: false,
|
labelWithinInput: false,
|
||||||
|
styles: {
|
||||||
|
fullWidth: false,
|
||||||
|
inheritFromTheme: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -45,6 +49,10 @@ export const firstNameBlock = {
|
|||||||
label: 'First Name',
|
label: 'First Name',
|
||||||
labelWithinInput: false,
|
labelWithinInput: false,
|
||||||
mandatory: false,
|
mandatory: false,
|
||||||
|
styles: {
|
||||||
|
fullWidth: false,
|
||||||
|
inheritFromTheme: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -57,6 +65,10 @@ export const lastNameBlock = {
|
|||||||
label: 'Last Name',
|
label: 'Last Name',
|
||||||
labelWithinInput: false,
|
labelWithinInput: false,
|
||||||
mandatory: false,
|
mandatory: false,
|
||||||
|
styles: {
|
||||||
|
fullWidth: false,
|
||||||
|
inheritFromTheme: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -71,6 +83,10 @@ export const customTextBlock = {
|
|||||||
mandatory: false,
|
mandatory: false,
|
||||||
validate: 'alphanum',
|
validate: 'alphanum',
|
||||||
customFieldId: 1,
|
customFieldId: 1,
|
||||||
|
styles: {
|
||||||
|
fullWidth: false,
|
||||||
|
inheritFromTheme: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -63,6 +63,42 @@ describe('Blocks to Form Body', () => {
|
|||||||
expect(input.params.label_within).to.be.equal('1');
|
expect(input.params.label_within).to.be.equal('1');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should map input block styles', () => {
|
||||||
|
const blockWithThemeStyles = {
|
||||||
|
...emailBlock,
|
||||||
|
attributes: {
|
||||||
|
...emailBlock.attributes,
|
||||||
|
styles: {
|
||||||
|
fullWidth: true,
|
||||||
|
inheritFromTheme: true,
|
||||||
|
bold: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const blockWithCustomStyles = {
|
||||||
|
...firstNameBlock,
|
||||||
|
attributes: {
|
||||||
|
...firstNameBlock.attributes,
|
||||||
|
styles: {
|
||||||
|
fullWidth: false,
|
||||||
|
inheritFromTheme: false,
|
||||||
|
bold: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const [
|
||||||
|
inputWithThemeStyles,
|
||||||
|
inputWithCustomStyles,
|
||||||
|
] = formBlocksToBody([blockWithThemeStyles, blockWithCustomStyles]);
|
||||||
|
expect(inputWithThemeStyles.styles).to.eql({
|
||||||
|
full_width: '1',
|
||||||
|
});
|
||||||
|
expect(inputWithCustomStyles.styles).to.eql({
|
||||||
|
full_width: '0',
|
||||||
|
bold: '1',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Should map last name block to input data', () => {
|
it('Should map last name block to input data', () => {
|
||||||
const [input] = formBlocksToBody([lastNameBlock]);
|
const [input] = formBlocksToBody([lastNameBlock]);
|
||||||
checkBodyInputBasics(input);
|
checkBodyInputBasics(input);
|
||||||
|
@@ -92,13 +92,14 @@ describe('Form Body To Blocks', () => {
|
|||||||
updated_at: '2019-12-10T15:05:06+00:00',
|
updated_at: '2019-12-10T15:05:06+00:00',
|
||||||
};
|
};
|
||||||
|
|
||||||
const [email, firstName, lastName, customText, customTextArea] = formBodyToBlocks([
|
const map = formBodyToBlocksFactory(colorDefinitions, [customFieldText, customFieldTextarea]);
|
||||||
|
const [email, firstName, lastName, customText, customTextArea] = map([
|
||||||
{ ...emailInput, position: '1' },
|
{ ...emailInput, position: '1' },
|
||||||
{ ...firstNameInput, position: '2' },
|
{ ...firstNameInput, position: '2' },
|
||||||
{ ...lastNameInput, position: '3' },
|
{ ...lastNameInput, position: '3' },
|
||||||
{ ...customTextInput, position: '4' },
|
{ ...customTextInput, position: '4' },
|
||||||
{ ...customTextareaInput, position: '5', id: 2 },
|
{ ...customTextareaInput, position: '5', id: 2 },
|
||||||
], [customFieldText, customFieldTextarea]);
|
]);
|
||||||
const defaultStyles = {
|
const defaultStyles = {
|
||||||
fullWidth: false,
|
fullWidth: false,
|
||||||
inheritFromTheme: true,
|
inheritFromTheme: true,
|
||||||
|
Reference in New Issue
Block a user