mirror of
https://github.com/strapi/strapi.git
synced 2025-11-14 17:19:01 +00:00
Add helper to format content type data
This commit is contained in:
parent
4d3a0f98e5
commit
7fb450ebfd
@ -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,
|
||||
};
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -152,6 +152,9 @@ const data = {
|
||||
isTemporary: true,
|
||||
schema: {
|
||||
name: 'test content type',
|
||||
collectionName: 'test-content-types',
|
||||
connection: 'default',
|
||||
description: '',
|
||||
attributes: {
|
||||
name: {
|
||||
type: 'string',
|
||||
|
||||
@ -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: [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user