Add helper to format content type data

This commit is contained in:
soupette 2019-12-03 20:06:34 +01:00
parent 4d3a0f98e5
commit 7fb450ebfd
4 changed files with 69 additions and 10 deletions

View File

@ -1,4 +1,4 @@
import { get, isEqual, omit } from 'lodash'; import { get, has, isEqual, omit } from 'lodash';
import makeUnique from '../../../utils/makeUnique'; import makeUnique from '../../../utils/makeUnique';
const getCreatedAndModifiedComponents = (allComponents, initialComponents) => { const getCreatedAndModifiedComponents = (allComponents, initialComponents) => {
@ -41,6 +41,29 @@ const formatComponent = (component, mainDataUID, isCreatingData = false) => {
return formattedComponent; return formattedComponent;
}; };
const formatContentType = data => {
const isCreatingData = get(data, 'isTemporary', true);
const mainDataUID = get(data, 'uid', null);
const formattedAttributes = formatAttributes(
get(data, 'schema.attributes', {}),
mainDataUID,
isCreatingData,
false
);
const formattedContentType = Object.assign(
{},
omit(data.schema, 'attributes'),
{ attributes: formattedAttributes }
);
delete formattedContentType.uid;
delete formattedContentType.isTemporary;
return formattedContentType;
};
/** /**
* *
* @param {Object} attributes * @param {Object} attributes
@ -57,18 +80,28 @@ const formatAttributes = (
return Object.keys(attributes).reduce((acc, current) => { return Object.keys(attributes).reduce((acc, current) => {
const currentAttribute = get(attributes, current, {}); const currentAttribute = get(attributes, current, {});
const hasARelationWithMainDataUID = currentAttribute.target === mainDataUID; const hasARelationWithMainDataUID = currentAttribute.target === mainDataUID;
const isRelationType = has(currentAttribute, 'nature');
const currentTargetAttribute = get(
currentAttribute,
'targetAttribute',
null
);
if (!hasARelationWithMainDataUID) { if (!hasARelationWithMainDataUID) {
acc[current] = currentAttribute; if (isRelationType) {
const relationAttr = Object.assign({}, currentAttribute, {
targetAttribute: formatRelationTargetAttribute(
currentTargetAttribute
),
});
acc[current] = relationAttr;
} else {
acc[current] = currentAttribute;
}
} }
if (hasARelationWithMainDataUID) { if (hasARelationWithMainDataUID) {
const currentTargetAttribute = get(
currentAttribute,
'targetAttribute',
null
);
let target = currentTargetAttribute.target; let target = currentTargetAttribute.target;
if (isCreatingMainData) { if (isCreatingMainData) {
@ -77,8 +110,7 @@ const formatAttributes = (
const formattedRelationAttribute = Object.assign({}, currentAttribute, { const formattedRelationAttribute = Object.assign({}, currentAttribute, {
target, target,
targetAttribute: targetAttribute: formatRelationTargetAttribute(currentTargetAttribute),
currentTargetAttribute === '-' ? null : currentTargetAttribute,
}); });
acc[current] = formattedRelationAttribute; acc[current] = formattedRelationAttribute;
@ -88,6 +120,9 @@ const formatAttributes = (
}, {}); }, {});
}; };
const formatRelationTargetAttribute = targetAttribute =>
targetAttribute === '-' ? null : targetAttribute;
const getComponentsToPost = ( const getComponentsToPost = (
allComponents, allComponents,
initialComponents, initialComponents,
@ -116,4 +151,5 @@ export {
formatComponent, formatComponent,
getComponentsToPost, getComponentsToPost,
getCreatedAndModifiedComponents, getCreatedAndModifiedComponents,
formatContentType,
}; };

View File

@ -1,5 +1,6 @@
import { import {
formatComponent, formatComponent,
formatContentType,
getComponentsToPost, getComponentsToPost,
getCreatedAndModifiedComponents, getCreatedAndModifiedComponents,
} from '../cleanData'; } from '../cleanData';
@ -68,6 +69,17 @@ describe('CleanData utils', () => {
}); });
}); });
describe('FormatContentType', () => {
it('should format the content type correctly', () => {
const {
rawData: { contentType },
} = contentTypeData;
const expected = expectedData.contentType;
expect(formatContentType(contentType)).toEqual(expected);
});
});
describe('GetComponentsToPost', () => { describe('GetComponentsToPost', () => {
it('should return an array containing all the formattedComponents', () => { it('should return an array containing all the formattedComponents', () => {
const { const {

View File

@ -152,6 +152,9 @@ const data = {
isTemporary: true, isTemporary: true,
schema: { schema: {
name: 'test content type', name: 'test content type',
collectionName: 'test-content-types',
connection: 'default',
description: '',
attributes: { attributes: {
name: { name: {
type: 'string', type: 'string',

View File

@ -1,6 +1,9 @@
const expectedData = { const expectedData = {
contentType: { contentType: {
name: 'test content type', name: 'test content type',
collectionName: 'test-content-types',
connection: 'default',
description: '',
attributes: { attributes: {
name: { name: {
type: 'string', type: 'string',
@ -45,6 +48,11 @@ const expectedData = {
repeatable: true, repeatable: true,
component: 'default.metas', component: 'default.metas',
}, },
quote: {
type: 'component',
repeatable: false,
component: 'blog.quote',
},
}, },
}, },
componentsToFormat: [ componentsToFormat: [