Init change modifiedData shape

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2021-07-12 14:07:30 +02:00
parent 08df9f7119
commit 79e0c85643
14 changed files with 474 additions and 139 deletions

View File

@ -9,7 +9,7 @@ import { get } from 'lodash';
import PropTypes from 'prop-types';
import List from '../List';
import useDataManager from '../../hooks/useDataManager';
import convertAttrObjToArray from '../../utils/convertAttrObjToArray';
// import convertAttrObjToArray from '../../utils/convertAttrObjToArray';
import Td from '../Td';
function ComponentList({
@ -26,7 +26,7 @@ function ComponentList({
const {
schema: { name: componentName, attributes },
} = get(modifiedData, ['components', component], {
schema: { attributes: {} },
schema: { attributes: [] },
});
return (
@ -35,7 +35,8 @@ function ComponentList({
<List
customRowComponent={customRowComponent}
dzName={dzName}
items={convertAttrObjToArray(attributes)}
// items={convertAttrObjToArray(attributes)}
items={attributes}
targetUid={component}
mainTypeName={mainTypeName}
firstLoopComponentName={firstLoopComponentName || componentName}

View File

@ -22,9 +22,10 @@ import makeUnique from '../../utils/makeUnique';
import pluginId from '../../pluginId';
import FormModal from '../FormModal';
import createDataObject from './utils/createDataObject';
import createModifiedDataSchema, {
orderAllDataAttributesWithImmutable,
} from './utils/createModifiedDataSchema';
// import createModifiedDataSchema, {
// orderAllDataAttributesWithImmutable,
// } from './utils/createModifiedDataSchema';
import createModifiedDataSchema from './utils/createModifiedDataSchema';
import retrieveSpecificInfoFromComponents from './utils/retrieveSpecificInfoFromComponents';
import retrieveComponentsFromSchema from './utils/retrieveComponentsFromSchema';
import retrieveNestedComponents from './utils/retrieveNestedComponents';
@ -54,6 +55,7 @@ import {
UPDATE_SCHEMA,
} from './constants';
import makeSelectDataManagerProvider from './selectors';
import formatSchemas from './utils/formatSchemas';
const DataManagerProvider = ({
allIcons,
@ -116,18 +118,22 @@ const DataManagerProvider = ({
);
const components = createDataObject(componentsArray);
const formattedComponents = formatSchemas(components);
const contentTypes = createDataObject(contentTypesArray);
const orderedComponents = orderAllDataAttributesWithImmutable({
components,
});
const orderedContenTypes = orderAllDataAttributesWithImmutable({
components: contentTypes,
});
const formattedContentTypes = formatSchemas(contentTypes);
// const orderedComponents = orderAllDataAttributesWithImmutable({
// components,
// });
// const orderedContenTypes = orderAllDataAttributesWithImmutable({
// components: contentTypes,
// });
dispatch({
type: GET_DATA_SUCCEEDED,
components: orderedComponents.get('components'),
contentTypes: orderedContenTypes.get('components'),
// components: orderedComponents.get('components'),
// contentTypes: orderedContenTypes.get('components'),
components: formattedComponents,
contentTypes: formattedContentTypes,
reservedNames,
});
} catch (err) {
@ -356,6 +362,7 @@ const DataManagerProvider = ({
}
};
// TODO
const getAllComponentsThatHaveAComponentInTheirAttributes = () => {
// We need to create an object with all the non modified compos
// plus the ones that are created on the fly
@ -373,7 +380,9 @@ const DataManagerProvider = ({
return makeUnique(composWithCompos);
};
console.log(getAllComponentsThatHaveAComponentInTheirAttributes());
// TODO
const getAllNestedComponents = () => {
const appNestedCompo = retrieveNestedComponents(components);
const editingDataNestedCompos = retrieveNestedComponents(modifiedData.components || {});
@ -381,6 +390,8 @@ const DataManagerProvider = ({
return makeUnique([...editingDataNestedCompos, ...appNestedCompo]);
};
console.log({ g: getAllNestedComponents() });
const removeComponentFromDynamicZone = (dzName, componentToRemoveIndex) => {
dispatch({
type: REMOVE_COMPONENT_FROM_DYNAMIC_ZONE,
@ -392,13 +403,14 @@ const DataManagerProvider = ({
const setModifiedData = () => {
const currentSchemas = isInContentTypeView ? contentTypes : components;
const schemaToSet = get(currentSchemas, currentUid, {
schema: { attributes: {} },
schema: { attributes: [] },
});
const retrievedComponents = retrieveComponentsFromSchema(
schemaToSet.schema.attributes,
components
);
const newSchemaToSet = createModifiedDataSchema(
schemaToSet,
retrievedComponents,
@ -406,15 +418,16 @@ const DataManagerProvider = ({
isInContentTypeView
);
const dataShape = orderAllDataAttributesWithImmutable(newSchemaToSet, isInContentTypeView);
// const dataShape = orderAllDataAttributesWithImmutable(newSchemaToSet, isInContentTypeView);
const hasJustCreatedSchema =
get(schemaToSet, 'isTemporary', false) &&
size(get(schemaToSet, 'schema.attributes', {})) === 0;
size(get(schemaToSet, 'schema.attributes', [])) === 0;
dispatch({
type: SET_MODIFIED_DATA,
schemaToSet: dataShape,
// schemaToSet: dataShape,
schemaToSet: newSchemaToSet,
hasJustCreatedSchema,
});
};

View File

@ -1,12 +1,14 @@
import { fromJS } from 'immutable';
import { get } from 'lodash';
import reducer, { initialState } from '../reducer';
import reducer, { initialState as immutableInitialState } from '../reducer';
import testData from './data';
import * as actions from '../constants';
const initialState = immutableInitialState.toJS();
describe('CTB | components | DataManagerProvider | reducer | basics actions ', () => {
it('Should return the initial state', () => {
expect(reducer(initialState, { type: 'TEST' })).toEqual(initialState);
expect(reducer(immutableInitialState, { type: 'TEST' })).toEqual(immutableInitialState);
});
describe('ADD_CREATED_COMPONENT_TO_DYNAMIC_ZONE', () => {
@ -48,7 +50,7 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
},
},
});
const state = initialState
const state = immutableInitialState
.setIn(['components'], components)
.setIn(['modifiedData', 'components'], components)
@ -177,7 +179,7 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
['schema', 'attributes', 'dz'],
fromJS({ type: 'dynamiczone', components: ['default.openingtimes'] })
);
const state = initialState
const state = immutableInitialState
.setIn(['components'], fromJS(testData.components))
.setIn(['modifiedData', 'components', componentUID], fromJS(component))
.setIn(['modifiedData', 'contentType'], contentType);
@ -208,7 +210,7 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
['schema', 'attributes', 'dz'],
fromJS({ type: 'dynamiczone', components: ['default.openingtimes'] })
);
const state = initialState
const state = immutableInitialState
.setIn(['components'], fromJS(testData.components))
.setIn(['modifiedData', 'components', componentUID], fromJS(component))
.setIn(
@ -243,7 +245,7 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
shouldAddComponentToData: false,
};
const state = initialState
const state = immutableInitialState
.setIn(['components', fromJS(testData.components)])
.setIn(['initialComponents', fromJS(testData.components)]);
@ -282,7 +284,7 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
},
};
const state = initialState
const state = immutableInitialState
.setIn(['components', fromJS(testData.components)])
.setIn(['initialComponents', fromJS(testData.components)]);
@ -301,7 +303,7 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
collectionName: 'test',
name: 'test',
};
const expected = initialState.setIn(
const expected = immutableInitialState.setIn(
['contentTypes', uid],
fromJS({
uid,
@ -314,19 +316,21 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
})
);
expect(reducer(initialState, { type: actions.CREATE_SCHEMA, uid, data })).toEqual(expected);
expect(reducer(immutableInitialState, { type: actions.CREATE_SCHEMA, uid, data })).toEqual(
expected
);
});
});
describe('DELETE_NOT_SAVED_TYPE', () => {
it('Should reset the components and and contentTypes object', () => {
const state = initialState
const state = immutableInitialState
.setIn(['components'], fromJS({ foo: {}, bar: {} }))
.setIn(['initialComponents'], fromJS({ foo: {} }))
.setIn(['contentTypes'], fromJS({ baz: {}, bat: {} }))
.setIn(['initialContentTypes'], fromJS({ baz: {} }));
const expected = initialState
const expected = immutableInitialState
.setIn(['components'], fromJS({ foo: {} }))
.setIn(['initialComponents'], fromJS({ foo: {} }))
.setIn(['contentTypes'], fromJS({ baz: {} }))
@ -343,7 +347,8 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
uid: 'default.test',
category: 'default',
schema: {
attributes: {},
// attributes: {},
attributes: [],
},
},
};
@ -351,7 +356,8 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
'application::test.test': {
uid: 'application::test.test',
schema: {
attributes: {},
// attributes: {},
attributes: [],
},
},
};
@ -359,21 +365,25 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
models: ['admin', 'ctb'],
attributes: ['attributes', 'length'],
};
const expected = initialState
.set('components', fromJS(components))
.set('contentTypes', fromJS(contentTypes))
.set('reservedNames', fromJS(reservedNames))
.set('initialContentTypes', fromJS(contentTypes))
.set('initialComponents', fromJS(components))
.set('isLoading', false);
const state = { ...initialState };
const expected = {
...initialState,
components,
contentTypes,
initialComponents: components,
initialContentTypes: contentTypes,
reservedNames,
isLoading: false,
};
expect(
reducer(initialState, {
reducer(fromJS(state), {
type: actions.GET_DATA_SUCCEEDED,
components,
contentTypes,
reservedNames,
})
}).toJS()
).toEqual(expected);
});
});
@ -381,10 +391,10 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
describe('RELOAD_PLUGIN', () => {
it('Should return the initial state constant', () => {
expect(
reducer(initialState.setIn(['components', 'foo'], {}), {
reducer(immutableInitialState.setIn(['components', 'foo'], {}), {
type: actions.RELOAD_PLUGIN,
})
).toEqual(initialState);
).toEqual(immutableInitialState);
});
});
@ -745,13 +755,13 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
uid: 'test',
},
});
const expected = initialState
const expected = immutableInitialState
.set('modifiedData', schemaToSet)
.set('initialData', schemaToSet)
.set('isLoadingForDataToBeSet', false);
expect(
reducer(initialState, {
reducer(immutableInitialState, {
type: actions.SET_MODIFIED_DATA,
schemaToSet,
hasJustCreatedSchema: true,
@ -766,13 +776,13 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
uid: 'test',
},
});
const expected = initialState
const expected = immutableInitialState
.set('modifiedData', schemaToSet)
.set('initialData', schemaToSet)
.set('isLoadingForDataToBeSet', false);
expect(
reducer(initialState, {
reducer(immutableInitialState, {
type: actions.SET_MODIFIED_DATA,
schemaToSet,
hasJustCreatedSchema: false,

View File

@ -1,4 +1,4 @@
import { fromJS, OrderedMap } from 'immutable';
// import { fromJS, OrderedMap } from 'immutable';
import get from 'lodash/get';
const createModifiedDataSchema = (
@ -23,52 +23,52 @@ const createModifiedDataSchema = (
return schema;
};
const orderAllDataAttributesWithImmutable = (allDataSchema, isInContentTypeView) => {
const attributesPath = ['schema', 'attributes'];
const componentsSchema = allDataSchema.components;
const componentsWithImmutableSchema = Object.keys(componentsSchema).reduce((acc, current) => {
const currentSchema = get(componentsSchema, [current], {});
// const orderAllDataAttributesWithImmutable = (allDataSchema, isInContentTypeView) => {
// const attributesPath = ['schema', 'attributes'];
// const componentsSchema = allDataSchema.components;
// const componentsWithImmutableSchema = Object.keys(componentsSchema).reduce((acc, current) => {
// const currentSchema = get(componentsSchema, [current], {});
const currentAttributes = get(currentSchema, attributesPath, {});
// const currentAttributes = get(currentSchema, attributesPath, {});
const fromJSAttributes = Object.keys(currentAttributes).reduce((acc, current) => {
acc[current] = fromJS(currentAttributes[current]);
// const fromJSAttributes = Object.keys(currentAttributes).reduce((acc, current) => {
// acc[current] = fromJS(currentAttributes[current]);
return acc;
}, {});
// return acc;
// }, {});
// TODO refacto
const currentImmutableSchemas = fromJS(currentSchema).setIn(
['schema', 'attributes'],
fromJS(OrderedMap(fromJSAttributes))
);
// // TODO refacto
// const currentImmutableSchemas = fromJS(currentSchema).setIn(
// ['schema', 'attributes'],
// fromJS(OrderedMap(fromJSAttributes))
// );
acc[current] = fromJS(currentImmutableSchemas);
// acc[current] = fromJS(currentImmutableSchemas);
return acc;
}, {});
const keyName = isInContentTypeView ? 'contentType' : 'component';
const mainSchema = get(allDataSchema, [keyName], {});
const mainImmutableSchema = fromJS(mainSchema).setIn(
attributesPath,
fromJS(
OrderedMap(
Object.keys(get(mainSchema, attributesPath, {})).reduce((acc, current) => {
acc[current] = fromJS(get(mainSchema, [...attributesPath, current], {}));
// return acc;
// }, {});
// const keyName = isInContentTypeView ? 'contentType' : 'component';
// const mainSchema = get(allDataSchema, [keyName], {});
// const mainImmutableSchema = fromJS(mainSchema).setIn(
// attributesPath,
// fromJS(
// OrderedMap(
// Object.keys(get(mainSchema, attributesPath, {})).reduce((acc, current) => {
// acc[current] = fromJS(get(mainSchema, [...attributesPath, current], {}));
return acc;
}, {})
)
)
);
// return acc;
// }, {})
// )
// )
// );
const immutableData = fromJS({
components: componentsWithImmutableSchema,
[keyName]: mainImmutableSchema,
});
// const immutableData = fromJS({
// components: componentsWithImmutableSchema,
// [keyName]: mainImmutableSchema,
// });
return immutableData;
};
// return immutableData;
// };
export default createModifiedDataSchema;
export { orderAllDataAttributesWithImmutable };
// export { orderAllDataAttributesWithImmutable };

View File

@ -0,0 +1,23 @@
const formatSchemas = schemas => {
return Object.keys(schemas).reduce((acc, current) => {
const schema = schemas[current].schema;
acc[current] = {
...schemas[current],
schema: { ...schema, attributes: toAttributesArray(schema.attributes) },
};
return acc;
}, {});
};
const toAttributesArray = attributes => {
return Object.keys(attributes).reduce((acc, current) => {
acc.push({ ...attributes[current], name: current });
return acc;
}, []);
};
export default formatSchemas;
export { toAttributesArray };

View File

@ -1,19 +1,19 @@
import { get } from 'lodash';
import get from 'lodash/get';
import makeUnique from '../../../utils/makeUnique';
const retrieveComponentsFromSchema = (attributes, allComponentsData) => {
const allComponents = Object.keys(attributes).reduce((acc, current) => {
const type = get(attributes, [current, 'type'], '');
const allComponents = attributes.reduce((acc, current) => {
const type = current.type;
if (type === 'component') {
const currentComponentName = attributes[current].component;
const currentComponentName = current.component;
// Push the existing compo
acc.push(currentComponentName);
const currentComponentAttributes = get(
allComponentsData,
[currentComponentName, 'schema', 'attributes'],
{}
[]
);
// Retrieve the nested ones
@ -21,9 +21,9 @@ const retrieveComponentsFromSchema = (attributes, allComponentsData) => {
}
if (type === 'dynamiczone') {
const dynamicZoneComponents = attributes[current].components;
const dynamicZoneComponents = current.components;
const componentsFromDZComponents = dynamicZoneComponents.reduce((acc2, currentUid) => {
const compoAttrs = get(allComponentsData, [currentUid, 'schema', 'attributes'], {});
const compoAttrs = get(allComponentsData, [currentUid, 'schema', 'attributes'], []);
return [...acc2, ...retrieveComponentsFromSchema(compoAttrs, allComponentsData)];
}, []);

View File

@ -1,4 +1,4 @@
import { get } from 'lodash';
import get from 'lodash/get';
import makeUnique from '../../../utils/makeUnique';
const retrieveComponentsThatHaveComponents = allComponents => {
@ -17,10 +17,10 @@ const retrieveComponentsThatHaveComponents = allComponents => {
};
const doesComponentHaveAComponentField = component => {
const attributes = get(component, ['schema', 'attributes'], {});
const attributes = get(component, ['schema', 'attributes'], []);
return Object.keys(attributes).some(attributeName => {
const type = get(attributes, [attributeName, 'type'], '');
return attributes.some(attribute => {
const { type } = attribute;
return type === 'component';
});

View File

@ -3,7 +3,7 @@ import makeUnique from '../../../utils/makeUnique';
const retrieveNestedComponents = appComponents => {
const nestedComponents = Object.keys(appComponents).reduce((acc, current) => {
const componentAttributes = get(appComponents, [current, 'schema', 'attributes'], {});
const componentAttributes = get(appComponents, [current, 'schema', 'attributes'], []);
const currentComponentNestedCompos = getComponentsFromComponent(componentAttributes);
return [...acc, ...currentComponentNestedCompos];
@ -13,9 +13,8 @@ const retrieveNestedComponents = appComponents => {
};
const getComponentsFromComponent = componentAttributes => {
return Object.keys(componentAttributes).reduce((acc, current) => {
const attribute = get(componentAttributes, current, {});
const { type, component } = attribute;
return componentAttributes.reduce((acc, current) => {
const { type, component } = current;
if (type === 'component') {
acc.push(component);

View File

@ -5,9 +5,7 @@ describe('CONTENT TYPE BUILDER | COMPONENTS | DataManagerProvider | utils | cre
const dataSchema = {
apiID: 'test',
schema: {
attributes: {
name: { type: 'string' },
},
attributes: [{ type: 'string', name: 'name' }],
},
};
@ -29,9 +27,7 @@ describe('CONTENT TYPE BUILDER | COMPONENTS | DataManagerProvider | utils | cre
const dataSchema = {
apiID: 'test',
schema: {
attributes: {
name: { type: 'string' },
},
attributes: [{ type: 'string', name: 'name' }],
},
};
@ -53,20 +49,20 @@ describe('CONTENT TYPE BUILDER | COMPONENTS | DataManagerProvider | utils | cre
const dataSchema = {
apiID: 'test',
schema: {
attributes: {
name: { type: 'string' },
compo: { type: 'component', component: 'blog.dish' },
},
attributes: [
{ type: 'string', name: 'name' },
{ type: 'component', component: 'blog.dish', name: 'compo' },
],
},
};
const retrievedComponents = ['blog.dish'];
const components = {
'blog.dish': {
schema: { attributes: { name: { type: 'string' } } },
schema: { attributes: [{ name: 'name', type: 'string' }] },
},
'blog.test': {
schema: { attributes: { name: { type: 'string' } } },
schema: { attributes: [{ type: 'string', name: 'name' }] },
},
};
const isInContentTypeView = true;
@ -75,7 +71,7 @@ describe('CONTENT TYPE BUILDER | COMPONENTS | DataManagerProvider | utils | cre
contentType: dataSchema,
components: {
'blog.dish': {
schema: { attributes: { name: { type: 'string' } } },
schema: { attributes: [{ type: 'string', name: 'name' }] },
},
},
};

View File

@ -0,0 +1,71 @@
import formatSchemas, { toAttributesArray } from '../formatSchemas';
describe('CONTENT TYPE BUILDER | components | DataManagerProvider | utils ', () => {
describe('toAttributesArray', () => {
it('should return an array of attributes', () => {
const attributes = {
name: { type: 'string', pluginOptions: { i18n: { enabled: false } } },
price: { type: 'integer' },
};
const expected = [
{ type: 'string', name: 'name', pluginOptions: { i18n: { enabled: false } } },
{ type: 'integer', name: 'price' },
];
expect(toAttributesArray(attributes)).toEqual(expected);
});
});
describe('formatSchema', () => {
it('should format all the attributes to an array', () => {
const schemas = {
address: {
apiID: 'address',
schema: {
pluginOptions: { i18n: { enabled: false } },
attributes: {
name: { type: 'string', pluginOptions: { i18n: { enabled: false } } },
price: { type: 'integer' },
},
},
},
test: {
apiID: 'test',
schema: {
pluginOptions: { i18n: { enabled: false } },
attributes: {
firstName: { type: 'string', pluginOptions: { i18n: { enabled: false } } },
lastName: { type: 'string' },
},
},
},
};
const expected = {
address: {
apiID: 'address',
schema: {
pluginOptions: { i18n: { enabled: false } },
attributes: [
{ type: 'string', pluginOptions: { i18n: { enabled: false } }, name: 'name' },
{ type: 'integer', name: 'price' },
],
},
},
test: {
apiID: 'test',
schema: {
pluginOptions: { i18n: { enabled: false } },
attributes: [
{ type: 'string', pluginOptions: { i18n: { enabled: false } }, name: 'firstName' },
{ type: 'string', name: 'lastName' },
],
},
},
};
expect(formatSchemas(schemas)).toEqual(expected);
});
});
});

View File

@ -0,0 +1,146 @@
import retrieveComponentsFromSchema from '../retrieveComponentsFromSchema';
describe('CONTENT TYPE BUILDER | components | DataManagerProvider | utils | retrieveComponentsFromSchema', () => {
it('should return an array of components name', () => {
const attributes = [
{
maxLength: 50,
required: true,
minLength: 5,
type: 'string',
pluginOptions: {},
name: 'name',
},
{ type: 'uid', targetField: 'name', pluginOptions: {}, name: 'slug' },
{
type: 'relation',
relation: 'oneToOne',
target: 'application::address.address',
targetAttribute: null,
private: false,
name: 'address',
},
{
type: 'relation',
relation: 'oneToOne',
target: 'application::menu.menu',
inversedBy: 'restaurant',
targetAttribute: 'restaurant',
private: false,
name: 'menu',
},
{
component: 'default.openingtimes',
type: 'component',
repeatable: true,
min: 1,
max: 10,
pluginOptions: {},
name: 'opening_times',
},
{
type: 'dynamiczone',
components: ['default.restaurantservice'],
pluginOptions: {},
name: 'dz',
},
];
const components = {
'blog.test-como': {
uid: 'blog.test-como',
category: 'blog',
apiId: 'test-como',
schema: {
icon: 'ad',
name: 'test comp',
description: '',
collectionName: 'components_blog_test_comos',
attributes: [{ type: 'string', name: 'name' }],
},
},
'default.closingperiod': {
uid: 'default.closingperiod',
category: 'default',
apiId: 'closingperiod',
schema: {
icon: 'angry',
name: 'closingperiod',
description: '',
collectionName: 'components_closingperiods',
attributes: [
{ type: 'string', name: 'label' },
{ type: 'date', required: true, name: 'start_date' },
{ type: 'date', required: true, name: 'end_date' },
{ type: 'media', multiple: false, required: false, name: 'media' },
{ component: 'default.dish', type: 'component', name: 'dish' },
],
},
},
'default.dish': {
uid: 'default.dish',
category: 'default',
apiId: 'dish',
schema: {
icon: 'address-book',
name: 'dish',
description: '',
collectionName: 'components_dishes',
attributes: [
{ type: 'string', required: false, default: 'My super dish', name: 'name' },
{ type: 'text', name: 'description' },
{ type: 'float', name: 'price' },
{ type: 'media', multiple: false, required: false, name: 'picture' },
{ type: 'richtext', name: 'very_long_description' },
{
type: 'relation',
relation: 'oneToOne',
target: 'application::category.category',
targetAttribute: null,
private: false,
name: 'categories',
},
],
},
},
'default.openingtimes': {
uid: 'default.openingtimes',
category: 'default',
apiId: 'openingtimes',
schema: {
icon: 'calendar',
name: 'openingtimes',
description: '',
collectionName: 'components_openingtimes',
attributes: [
{ type: 'string', required: true, default: 'something', name: 'label' },
{ type: 'string', name: 'time' },
{ type: 'component', repeatable: true, component: 'default.dish', name: 'dishrep' },
],
},
},
'default.restaurantservice': {
uid: 'default.restaurantservice',
category: 'default',
apiId: 'restaurantservice',
schema: {
icon: 'cannabis',
name: 'restaurantservice',
description: '',
collectionName: 'components_restaurantservices',
attributes: [
{ type: 'string', required: true, default: 'something', name: 'name' },
{ type: 'media', multiple: false, required: false, name: 'media' },
{ type: 'boolean', required: true, default: true, name: 'is_available' },
],
},
},
};
const expected = ['default.openingtimes', 'default.dish', 'default.restaurantservice'];
expect(retrieveComponentsFromSchema(attributes, components)).toEqual(expected);
});
});

View File

@ -14,19 +14,30 @@ const data = {
description: '',
connection: 'default',
collectionName: 'components_sliders',
attributes: {
title: {
type: 'string',
required: true,
},
slide: {
attributes: [
{ type: 'string', required: true, name: 'title' },
{
name: 'slide',
component: 'default.slide',
type: 'component',
repeatable: true,
min: 1,
max: 5,
},
},
],
// attributes: {
// title: {
// type: 'string',
// required: true,
// },
// slide: {
// component: 'default.slide',
// type: 'component',
// repeatable: true,
// min: 1,
// max: 5,
// },
// },
},
},
'default.dish': {
@ -38,39 +49,44 @@ const data = {
description: '',
connection: 'default',
collectionName: 'components_dishes',
attributes: {
name: {
attributes: [
{
name: 'name',
type: 'string',
required: true,
default: 'My super dish',
},
description: {
{
name: 'description',
type: 'text',
},
price: {
{
name: 'price',
type: 'float',
},
address: {
nature: 'oneWay',
{
name: 'address',
relation: 'oneToOne',
target: 'application::address.address',
dominant: false,
unique: false,
type: 'relation',
},
addresses: {
nature: 'manyWay',
{
name: 'addresses',
relation: 'oneToMany',
target: 'application::address.address',
dominant: false,
unique: false,
type: 'relation',
},
picture: {
{
name: 'picture',
type: 'media',
multiple: false,
required: false,
},
very_long_description: {
{
name: 'very_long_description',
type: 'richtext',
},
},
],
},
},
};

View File

@ -0,0 +1,56 @@
import retrieveNestedComponents from '../retrieveNestedComponents';
describe('CONTENT TYPE BUILDER | COMPONENTS | DataManagerProvider | utils | retrieveNestedComponents', () => {
it('should return an array of nested components', () => {
const components = {
'default.closingperiod': {
uid: 'default.closingperiod',
category: 'default',
apiId: 'closingperiod',
schema: {
icon: 'angry',
name: 'closingperiod',
description: '',
collectionName: 'components_closingperiods',
attributes: [
{ type: 'string', name: 'label' },
{ type: 'date', required: true, name: 'start_date' },
{ type: 'date', required: true, name: 'end_date' },
{ type: 'media', multiple: false, required: false, name: 'media' },
{ component: 'default.dish', type: 'component', name: 'dish' },
],
},
},
'default.dish': {
uid: 'default.dish',
category: 'default',
apiId: 'dish',
schema: {
icon: 'address-book',
name: 'dish',
description: '',
collectionName: 'components_dishes',
attributes: [
{ type: 'string', required: false, default: 'My super dish', name: 'name' },
{ type: 'text', name: 'description' },
{ type: 'float', name: 'price' },
{ type: 'media', multiple: false, required: false, name: 'picture' },
{ type: 'richtext', name: 'very_long_description' },
{
type: 'relation',
relation: 'oneToOne',
target: 'application::category.category',
targetAttribute: null,
private: false,
name: 'categories',
},
],
},
},
};
const expected = ['default.dish'];
expect(retrieveNestedComponents(components)).toEqual(expected);
});
});

View File

@ -6,7 +6,8 @@ import { BackHeader, ListWrapper, useTracking } from '@strapi/helper-plugin';
import { useIntl } from 'react-intl';
import { Header } from '@buffetjs/custom';
import ListViewContext from '../../contexts/ListViewContext';
import convertAttrObjToArray from '../../utils/convertAttrObjToArray';
// import convertAttrObjToArray from '../../utils/convertAttrObjToArray';
// import convertAttrObjToArray from '../../utils/convertAttrObjToArray';
import getAttributeDisplayedType from '../../utils/getAttributeDisplayedType';
import pluginId from '../../pluginId';
import getTrad from '../../utils/getTrad';
@ -57,8 +58,10 @@ const ListView = () => {
const isTemporary = get(modifiedData, [firstMainDataPath, 'isTemporary'], false);
const contentTypeKind = get(modifiedData, [firstMainDataPath, 'schema', 'kind'], null);
const attributes = get(modifiedData, mainDataTypeAttributesPath, {});
const attributesLength = Object.keys(attributes).length;
const attributes = get(modifiedData, mainDataTypeAttributesPath, []);
const attributesLength = attributes.length;
// const attributes = get(modifiedData, mainDataTypeAttributesPath, {});
// const attributesLength = Object.keys(attributes).length;
const currentDataName = get(initialData, [firstMainDataPath, 'schema', 'name'], '');
const isFromPlugin = has(initialData, [firstMainDataPath, 'plugin']);
const hasModelBeenModified = !isEqual(modifiedData, initialData);
@ -302,7 +305,8 @@ const ListView = () => {
<ListWrapper style={{ marginBottom: 80 }}>
<ListHeader actions={listActions} title={listTitle} />
<List
items={convertAttrObjToArray(attributes)}
// items={convertAttrObjToArray(attributes)}
items={attributes}
customRowComponent={props => <CustomRow {...props} />}
addComponentToDZ={handleClickAddComponentToDZ}
targetUid={targetUid}