diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/cleanData.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/cleanData.js index 8a40c37745..042ce04af8 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/cleanData.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/cleanData.js @@ -1,4 +1,4 @@ -import { get, isEqual, omit } from 'lodash'; +import { get, has, isEqual, omit } from 'lodash'; import makeUnique from '../../../utils/makeUnique'; const getCreatedAndModifiedComponents = (allComponents, initialComponents) => { @@ -41,6 +41,29 @@ const formatComponent = (component, mainDataUID, isCreatingData = false) => { 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 @@ -57,18 +80,28 @@ const formatAttributes = ( return Object.keys(attributes).reduce((acc, current) => { const currentAttribute = get(attributes, current, {}); const hasARelationWithMainDataUID = currentAttribute.target === mainDataUID; + const isRelationType = has(currentAttribute, 'nature'); + const currentTargetAttribute = get( + currentAttribute, + 'targetAttribute', + null + ); if (!hasARelationWithMainDataUID) { - acc[current] = currentAttribute; + if (isRelationType) { + const relationAttr = Object.assign({}, currentAttribute, { + targetAttribute: formatRelationTargetAttribute( + currentTargetAttribute + ), + }); + + acc[current] = relationAttr; + } else { + acc[current] = currentAttribute; + } } if (hasARelationWithMainDataUID) { - const currentTargetAttribute = get( - currentAttribute, - 'targetAttribute', - null - ); - let target = currentTargetAttribute.target; if (isCreatingMainData) { @@ -77,8 +110,7 @@ const formatAttributes = ( const formattedRelationAttribute = Object.assign({}, currentAttribute, { target, - targetAttribute: - currentTargetAttribute === '-' ? null : currentTargetAttribute, + targetAttribute: formatRelationTargetAttribute(currentTargetAttribute), }); acc[current] = formattedRelationAttribute; @@ -88,6 +120,9 @@ const formatAttributes = ( }, {}); }; +const formatRelationTargetAttribute = targetAttribute => + targetAttribute === '-' ? null : targetAttribute; + const getComponentsToPost = ( allComponents, initialComponents, @@ -116,4 +151,5 @@ export { formatComponent, getComponentsToPost, getCreatedAndModifiedComponents, + formatContentType, }; diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/cleanData.test.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/cleanData.test.js index afbff43825..26d002f579 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/cleanData.test.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/cleanData.test.js @@ -1,5 +1,6 @@ import { formatComponent, + formatContentType, getComponentsToPost, getCreatedAndModifiedComponents, } 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', () => { it('should return an array containing all the formattedComponents', () => { const { diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/contentTypeData.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/contentTypeData.js index 400119a12a..d03a9db2c8 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/contentTypeData.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/contentTypeData.js @@ -152,6 +152,9 @@ const data = { isTemporary: true, schema: { name: 'test content type', + collectionName: 'test-content-types', + connection: 'default', + description: '', attributes: { name: { type: 'string', diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/expectedFormattedContentTypeData.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/expectedFormattedContentTypeData.js index 3191868002..63641c7cc8 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/expectedFormattedContentTypeData.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/DataManagerProvider/utils/tests/expectedFormattedContentTypeData.js @@ -1,6 +1,9 @@ const expectedData = { contentType: { name: 'test content type', + collectionName: 'test-content-types', + connection: 'default', + description: '', attributes: { name: { type: 'string', @@ -45,6 +48,11 @@ const expectedData = { repeatable: true, component: 'default.metas', }, + quote: { + type: 'component', + repeatable: false, + component: 'blog.quote', + }, }, }, componentsToFormat: [