mirror of
https://github.com/strapi/strapi.git
synced 2025-09-23 07:22:51 +00:00
Created helper to format attributes
This commit is contained in:
parent
0e031e8a38
commit
b1401289bd
@ -246,6 +246,8 @@ const DataManagerProvider = ({ allIcons, children }) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// console.log('mod', modifiedData);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DataManagerContext.Provider
|
<DataManagerContext.Provider
|
||||||
value={{
|
value={{
|
||||||
|
@ -0,0 +1,91 @@
|
|||||||
|
import { get, isEqual, omit } from 'lodash';
|
||||||
|
import makeUnique from '../../../utils/makeUnique';
|
||||||
|
|
||||||
|
const getCreatedAndModifiedComponents = (allComponents, initialComponents) => {
|
||||||
|
const componentUIDsToReturn = Object.keys(allComponents).filter(compoUid => {
|
||||||
|
const currentCompo = get(allComponents, compoUid, {});
|
||||||
|
const initialCompo = get(initialComponents, compoUid, {});
|
||||||
|
const hasComponentBeenCreated = get(currentCompo, ['isTemporary'], false);
|
||||||
|
const hasComponentBeenModified = !isEqual(currentCompo, initialCompo);
|
||||||
|
|
||||||
|
return hasComponentBeenCreated || hasComponentBeenModified;
|
||||||
|
});
|
||||||
|
|
||||||
|
return makeUnique(componentUIDsToReturn);
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatComponent = (component, mainDataUID, isCreatingData = false) => {
|
||||||
|
const formattedAttributes = formatAttributes(
|
||||||
|
get(component, 'schema.attributes', {}),
|
||||||
|
mainDataUID,
|
||||||
|
isCreatingData,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set tmpUID if the component has just been created
|
||||||
|
// Keep the uid if the component already exists
|
||||||
|
const compoUID = get(component, 'isTemporary', false)
|
||||||
|
? { tmpUID: component.uid }
|
||||||
|
: { uid: component.uid };
|
||||||
|
|
||||||
|
const formattedComponent = Object.assign(
|
||||||
|
{},
|
||||||
|
compoUID,
|
||||||
|
{ category: component.category },
|
||||||
|
// Omit the attributes since we want to format them
|
||||||
|
omit(component.schema, 'attributes'),
|
||||||
|
// Add the formatted attributes
|
||||||
|
{ attributes: formattedAttributes }
|
||||||
|
);
|
||||||
|
|
||||||
|
return formattedComponent;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Object} attributes
|
||||||
|
* @param {String} mainDataUID uid of the main data type
|
||||||
|
* @param {Boolean} isCreatingMainData
|
||||||
|
* @param {Boolean} isComponent
|
||||||
|
*/
|
||||||
|
const formatAttributes = (
|
||||||
|
attributes,
|
||||||
|
mainDataUID,
|
||||||
|
isCreatingMainData,
|
||||||
|
isComponent
|
||||||
|
) => {
|
||||||
|
return Object.keys(attributes).reduce((acc, current) => {
|
||||||
|
const currentAttribute = get(attributes, current, {});
|
||||||
|
const hasARelationWithMainDataUID = currentAttribute.target === mainDataUID;
|
||||||
|
|
||||||
|
if (!hasARelationWithMainDataUID) {
|
||||||
|
acc[current] = currentAttribute;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasARelationWithMainDataUID) {
|
||||||
|
const currentTargetAttribute = get(
|
||||||
|
currentAttribute,
|
||||||
|
'targetAttribute',
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
let target = currentTargetAttribute.target;
|
||||||
|
|
||||||
|
if (isCreatingMainData) {
|
||||||
|
target = isComponent ? '__contentType__' : '__self__';
|
||||||
|
}
|
||||||
|
|
||||||
|
const formattedRelationAttribute = Object.assign({}, currentAttribute, {
|
||||||
|
target,
|
||||||
|
targetAttribute:
|
||||||
|
currentTargetAttribute === '-' ? null : currentTargetAttribute,
|
||||||
|
});
|
||||||
|
|
||||||
|
acc[current] = formattedRelationAttribute;
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
};
|
||||||
|
|
||||||
|
export { formatComponent, getCreatedAndModifiedComponents };
|
@ -0,0 +1,83 @@
|
|||||||
|
import { formatComponent, getCreatedAndModifiedComponents } from '../cleanData';
|
||||||
|
import contentTypeData from './contentTypeData';
|
||||||
|
import expectedData from './expectedFormattedContentTypeData';
|
||||||
|
|
||||||
|
describe('CleanData utils', () => {
|
||||||
|
describe('GetCreatedAndModifiedComponents', () => {
|
||||||
|
it('should return an empty array if there is no component', () => {
|
||||||
|
expect(getCreatedAndModifiedComponents({}, {})).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return an array containing the uid of the modified and created components', () => {
|
||||||
|
const { componentsToFormat } = expectedData;
|
||||||
|
const {
|
||||||
|
initialComponents,
|
||||||
|
rawData: { components },
|
||||||
|
} = contentTypeData;
|
||||||
|
expect(
|
||||||
|
getCreatedAndModifiedComponents(components, initialComponents).sort()
|
||||||
|
).toEqual(componentsToFormat.sort());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('FormatComponent', () => {
|
||||||
|
describe('Formatting created component', () => {
|
||||||
|
it('should remove the uid key if the component is new', () => {
|
||||||
|
const component =
|
||||||
|
contentTypeData.rawData.components['components.main-compo'];
|
||||||
|
|
||||||
|
expect(
|
||||||
|
formatComponent(
|
||||||
|
component,
|
||||||
|
'application::test-content-type.test-content-type',
|
||||||
|
true
|
||||||
|
)
|
||||||
|
).not.toHaveProperty('uid');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add a tempUID key if the component is new', () => {
|
||||||
|
const component =
|
||||||
|
contentTypeData.rawData.components['components.main-compo'];
|
||||||
|
|
||||||
|
expect(
|
||||||
|
formatComponent(
|
||||||
|
component,
|
||||||
|
'application::test-content-type.test-content-type',
|
||||||
|
true
|
||||||
|
)
|
||||||
|
).toHaveProperty('tmpUID');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should format the component correctly', () => {
|
||||||
|
const component =
|
||||||
|
contentTypeData.rawData.components['components.main-compo'];
|
||||||
|
const expectedComponent =
|
||||||
|
expectedData.formattedComponents['components.main-compo'];
|
||||||
|
|
||||||
|
expect(
|
||||||
|
formatComponent(
|
||||||
|
component,
|
||||||
|
'application::test-content-type.test-content-type',
|
||||||
|
true
|
||||||
|
)
|
||||||
|
).toEqual(expectedComponent);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Formatting existing component', () => {
|
||||||
|
it('should format the component correctly', () => {
|
||||||
|
const component = contentTypeData.rawData.components['blog.quote'];
|
||||||
|
const expectedComponent =
|
||||||
|
expectedData.formattedComponents['blog.quote'];
|
||||||
|
|
||||||
|
expect(
|
||||||
|
formatComponent(
|
||||||
|
component,
|
||||||
|
'application::test-content-type.test-content-type',
|
||||||
|
true
|
||||||
|
)
|
||||||
|
).toEqual(expectedComponent);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -196,6 +196,11 @@ const data = {
|
|||||||
repeatable: true,
|
repeatable: true,
|
||||||
component: 'default.metas',
|
component: 'default.metas',
|
||||||
},
|
},
|
||||||
|
quote: {
|
||||||
|
type: 'component',
|
||||||
|
repeatable: false,
|
||||||
|
component: 'blog.quote',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -54,13 +54,74 @@ const expectedData = {
|
|||||||
],
|
],
|
||||||
|
|
||||||
components: [
|
components: [
|
||||||
expectedData.formattedComponents['components.main-compo'],
|
{
|
||||||
expectedData.formattedComponents['default.nested-compo'],
|
tmpUID: 'components.main-compo',
|
||||||
expectedData.formattedComponents['blog.quote'],
|
name: 'mainCompo',
|
||||||
|
icon: 'ad',
|
||||||
|
category: 'components',
|
||||||
|
attributes: {
|
||||||
|
name: {
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
testContentType: {
|
||||||
|
dominant: null,
|
||||||
|
columnName: null,
|
||||||
|
nature: 'oneWay',
|
||||||
|
targetAttribute: null,
|
||||||
|
target: '__contentType__',
|
||||||
|
unique: false,
|
||||||
|
targetColumnName: null,
|
||||||
|
required: false,
|
||||||
|
},
|
||||||
|
subCompoField: {
|
||||||
|
type: 'component',
|
||||||
|
repeatable: false,
|
||||||
|
component: 'default.nested-compo',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tmpUID: 'default.nested-compo',
|
||||||
|
name: 'nestedCompo',
|
||||||
|
icon: 'address-book',
|
||||||
|
category: 'default',
|
||||||
|
attributes: {
|
||||||
|
name: {
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
email: {
|
||||||
|
type: 'email',
|
||||||
|
default: null,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uid: 'blog.quote',
|
||||||
|
category: 'blog',
|
||||||
|
name: 'quote',
|
||||||
|
description: '',
|
||||||
|
icon: 'anchor',
|
||||||
|
connection: 'default',
|
||||||
|
collectionName: 'components_quotes',
|
||||||
|
attributes: {
|
||||||
|
quote: {
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
model: 'user',
|
||||||
|
plugin: 'users-permissions',
|
||||||
|
},
|
||||||
|
link_to_biography: {
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
formattedComponents: {
|
formattedComponents: {
|
||||||
'components.main-compo': {
|
'components.main-compo': {
|
||||||
tmpUId: 'components.main-compo',
|
tmpUID: 'components.main-compo',
|
||||||
name: 'mainCompo',
|
name: 'mainCompo',
|
||||||
icon: 'ad',
|
icon: 'ad',
|
||||||
category: 'components',
|
category: 'components',
|
||||||
@ -86,7 +147,7 @@ const expectedData = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
'default.nested-compo': {
|
'default.nested-compo': {
|
||||||
tmpUId: 'default.nested-compo',
|
tmpUID: 'default.nested-compo',
|
||||||
name: 'nestedCompo',
|
name: 'nestedCompo',
|
||||||
icon: 'address-book',
|
icon: 'address-book',
|
||||||
category: 'default',
|
category: 'default',
|
||||||
@ -130,7 +191,7 @@ export default expectedData;
|
|||||||
|
|
||||||
// components: [
|
// components: [
|
||||||
// {
|
// {
|
||||||
// tmpUId: 'components.main-compo',
|
// tmpUID: 'components.main-compo',
|
||||||
// name: 'mainCompo',
|
// name: 'mainCompo',
|
||||||
// icon: 'ad',
|
// icon: 'ad',
|
||||||
// category: 'components',
|
// category: 'components',
|
||||||
@ -156,7 +217,7 @@ export default expectedData;
|
|||||||
// },
|
// },
|
||||||
// },
|
// },
|
||||||
// {
|
// {
|
||||||
// tmpUId: 'default.nested-compo',
|
// tmpUID: 'default.nested-compo',
|
||||||
// name: 'nestedCompo',
|
// name: 'nestedCompo',
|
||||||
// icon: 'address-book',
|
// icon: 'address-book',
|
||||||
// category: 'default',
|
// category: 'default',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user