Migrate basic actions to immer

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2021-07-12 18:02:32 +02:00
parent c11e05fce6
commit 6ed7ef9cdf
2 changed files with 379 additions and 536 deletions

View File

@ -34,8 +34,6 @@ const getOppositeRelation = originalRelation => {
return originalRelation; return originalRelation;
}; };
// const addComponentsToState = (state, componentToAddUid, )
const addComponentsToState = (state, componentToAddUid, objToUpdate) => { const addComponentsToState = (state, componentToAddUid, objToUpdate) => {
let newObj = objToUpdate; let newObj = objToUpdate;
const componentToAdd = state.getIn(['components', componentToAddUid]); const componentToAdd = state.getIn(['components', componentToAddUid]);
@ -210,6 +208,55 @@ const reducer = (state = initialState, action) =>
break; break;
} }
case actions.REMOVE_COMPONENT_FROM_DYNAMIC_ZONE: {
const dzAttributeIndex = findAttributeIndex(state.modifiedData.contentType, action.dzName);
draftState.modifiedData.contentType.schema.attributes[dzAttributeIndex].components.splice(
action.componentToRemoveIndex,
1
);
break;
}
case actions.REMOVE_FIELD_FROM_DISPLAYED_COMPONENT: {
const { attributeToRemoveName, componentUid } = action;
const attributeToRemoveIndex = findAttributeIndex(
state.modifiedData.components[componentUid],
attributeToRemoveName
);
draftState.modifiedData.components[componentUid].schema.attributes.splice(
attributeToRemoveIndex,
1
);
break;
}
case actions.UPDATE_SCHEMA: {
const {
data: { name, collectionName, category, icon, kind },
schemaType,
uid,
} = action;
draftState.modifiedData[schemaType].schema.collectionName = collectionName;
draftState.modifiedData[schemaType].schema.name = name;
if (action.schemaType === 'component') {
draftState.modifiedData.component.category = category;
draftState.modifiedData.component.schema.icon = icon;
const addedComponent = current(draftState.modifiedData.component);
draftState.components[uid] = addedComponent;
break;
}
draftState.modifiedData.contentType.schema.kind = kind;
break;
}
default: default:
return draftState; return draftState;
} }
@ -430,30 +477,7 @@ const reducer = (state = initialState, action) =>
// }); // });
// } // }
// case actions.REMOVE_FIELD_FROM_DISPLAYED_COMPONENT: { //
// const { attributeToRemoveName, componentUid } = action;
// return state.removeIn([
// 'modifiedData',
// 'components',
// componentUid,
// 'schema',
// 'attributes',
// attributeToRemoveName,
// ]);
// }
// case actions.REMOVE_COMPONENT_FROM_DYNAMIC_ZONE:
// return state.removeIn([
// 'modifiedData',
// 'contentType',
// 'schema',
// 'attributes',
// action.dzName,
// 'components',
// action.componentToRemoveIndex,
// ]);
// // TODO
// case actions.REMOVE_FIELD: { // case actions.REMOVE_FIELD: {
// const { mainDataKey, attributeToRemoveName } = action; // const { mainDataKey, attributeToRemoveName } = action;
// const pathToAttributes = ['modifiedData', mainDataKey, 'schema', 'attributes']; // const pathToAttributes = ['modifiedData', mainDataKey, 'schema', 'attributes'];
@ -491,38 +515,6 @@ const reducer = (state = initialState, action) =>
// }); // });
// } // }
// case actions.UPDATE_SCHEMA: {
// const {
// data: { name, collectionName, category, icon, kind },
// schemaType,
// uid,
// } = action;
// let newState = state.updateIn(['modifiedData', schemaType], obj => {
// let updatedObj = obj
// .updateIn(['schema', 'name'], () => name)
// .updateIn(['schema', 'collectionName'], () => collectionName);
// if (action.schemaType === 'component') {
// updatedObj = updatedObj
// .update('category', () => category)
// .updateIn(['schema', 'icon'], () => icon);
// }
// if (action.schemaType === 'contentType') {
// updatedObj = updatedObj.updateIn(['schema', 'kind'], () => kind);
// }
// return updatedObj;
// });
// if (schemaType === 'component') {
// newState = newState.updateIn(['components'], obj => {
// return obj.update(uid, () => newState.getIn(['modifiedData', 'component']));
// });
// }
// return newState;
// }
// default: // default:
// return state; // return state;
// } // }

View File

@ -557,354 +557,193 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
}); });
}); });
// describe('REMOVE_COMPONENT_FROM_DYNAMIC_ZONE', () => { describe('REMOVE_COMPONENT_FROM_DYNAMIC_ZONE', () => {
// it('Should remove a component from a dynamic zone', () => { it('Should remove a component from a dynamic zone', () => {
// const state = fromJS({ const components = {
// components: { 'default.openingtimes': {
// 'default.openingtimes': { uid: 'default.openingtimes',
// uid: 'default.openingtimes', category: 'default',
// category: 'default', schema: {
// schema: { icon: 'calendar',
// icon: 'calendar', name: 'openingtimes',
// name: 'openingtimes', description: '',
// description: '', connection: 'default',
// connection: 'default', collectionName: 'components_openingtimes',
// collectionName: 'components_openingtimes', attributes: [
// attributes: { {
// label: { name: 'label',
// type: 'string', type: 'string',
// required: true, required: true,
// default: 'something', default: 'something',
// }, },
// time: { {
// type: 'string', name: 'time',
// }, type: 'string',
// }, },
// }, ],
// }, },
// 'default.dish': { },
// uid: 'default.dish', 'default.dish': {
// category: 'default', uid: 'default.dish',
// schema: { category: 'default',
// icon: 'calendar', schema: {
// name: 'dish', icon: 'calendar',
// description: '', name: 'dish',
// connection: 'default', description: '',
// collectionName: 'components_dishes', connection: 'default',
// attributes: { collectionName: 'components_dishes',
// label: { attributes: [
// type: 'string', {
// required: true, name: 'label',
// default: 'something', type: 'string',
// }, required: true,
// time: { default: 'something',
// type: 'string', },
// },
// },
// },
// },
// },
// modifiedData: {
// components: {
// 'default.dish': {
// uid: 'default.dish',
// category: 'default',
// schema: {
// icon: 'calendar',
// name: 'dish',
// description: '',
// connection: 'default',
// collectionName: 'components_dishes',
// attributes: {
// label: {
// type: 'string',
// required: true,
// default: 'something',
// },
// time: {
// type: 'string',
// },
// },
// },
// },
// 'default.openingtimes': {
// uid: 'default.openingtimes',
// category: 'default',
// schema: {
// icon: 'calendar',
// name: 'openingtimes',
// description: '',
// connection: 'default',
// collectionName: 'components_openingtimes',
// attributes: {
// label: {
// type: 'string',
// required: true,
// default: 'something',
// },
// time: {
// type: 'string',
// },
// },
// },
// },
// },
// contentType: {
// uid: 'application::address.address',
// schema: {
// name: 'address',
// description: '',
// connection: 'default',
// collectionName: 'addresses',
// attributes: {
// geolocation: {
// type: 'json',
// required: true,
// },
// city: {
// type: 'string',
// required: true,
// },
// postal_coder: {
// type: 'string',
// },
// category: {
// model: 'category',
// },
// cover: {
// model: 'file',
// via: 'related',
// plugin: 'upload',
// required: false,
// },
// images: {
// collection: 'file',
// via: 'related',
// plugin: 'upload',
// required: false,
// },
// full_name: {
// type: 'string',
// required: true,
// },
// dz: {
// type: 'dynamiczone',
// components: ['default.openingtimes', 'default.dish'],
// },
// otherDz: {
// type: 'dynamiczone',
// components: ['default.openingtimes', 'default.dish'],
// },
// },
// },
// },
// },
// });
// const expected = fromJS({ { name: 'time', type: 'string' },
// components: { ],
// 'default.openingtimes': { },
// uid: 'default.openingtimes', },
// category: 'default', };
// schema: {
// icon: 'calendar',
// name: 'openingtimes',
// description: '',
// connection: 'default',
// collectionName: 'components_openingtimes',
// attributes: {
// label: {
// type: 'string',
// required: true,
// default: 'something',
// },
// time: {
// type: 'string',
// },
// },
// },
// },
// 'default.dish': {
// uid: 'default.dish',
// category: 'default',
// schema: {
// icon: 'calendar',
// name: 'dish',
// description: '',
// connection: 'default',
// collectionName: 'components_dishes',
// attributes: {
// label: {
// type: 'string',
// required: true,
// default: 'something',
// },
// time: {
// type: 'string',
// },
// },
// },
// },
// },
// modifiedData: {
// components: {
// 'default.dish': {
// uid: 'default.dish',
// category: 'default',
// schema: {
// icon: 'calendar',
// name: 'dish',
// description: '',
// connection: 'default',
// collectionName: 'components_dishes',
// attributes: {
// label: {
// type: 'string',
// required: true,
// default: 'something',
// },
// time: {
// type: 'string',
// },
// },
// },
// },
// 'default.openingtimes': {
// uid: 'default.openingtimes',
// category: 'default',
// schema: {
// icon: 'calendar',
// name: 'openingtimes',
// description: '',
// connection: 'default',
// collectionName: 'components_openingtimes',
// attributes: {
// label: {
// type: 'string',
// required: true,
// default: 'something',
// },
// time: {
// type: 'string',
// },
// },
// },
// },
// },
// contentType: {
// uid: 'application::address.address',
// schema: {
// name: 'address',
// description: '',
// connection: 'default',
// collectionName: 'addresses',
// attributes: {
// geolocation: {
// type: 'json',
// required: true,
// },
// city: {
// type: 'string',
// required: true,
// },
// postal_coder: {
// type: 'string',
// },
// category: {
// model: 'category',
// },
// cover: {
// model: 'file',
// via: 'related',
// plugin: 'upload',
// required: false,
// },
// images: {
// collection: 'file',
// via: 'related',
// plugin: 'upload',
// required: false,
// },
// full_name: {
// type: 'string',
// required: true,
// },
// dz: {
// type: 'dynamiczone',
// components: ['default.openingtimes'],
// },
// otherDz: {
// type: 'dynamiczone',
// components: ['default.openingtimes', 'default.dish'],
// },
// },
// },
// },
// },
// });
// expect( const modifiedData = {
// reducer(state, { components,
// type: actions.REMOVE_COMPONENT_FROM_DYNAMIC_ZONE, contentType: {
// dzName: 'dz', uid: 'application::address.address',
// componentToRemoveIndex: 1, schema: {
// }) name: 'address',
// ).toEqual(expected); description: '',
// }); connection: 'default',
// }); collectionName: 'addresses',
attributes: [
{
name: 'full_name',
type: 'string',
required: true,
},
{
name: 'dz',
type: 'dynamiczone',
components: ['default.openingtimes', 'default.dish'],
},
{
name: 'otherDz',
type: 'dynamiczone',
components: ['default.openingtimes', 'default.dish'],
},
],
},
},
};
const state = {
...initialState,
components,
modifiedData,
};
// describe('REMOVE_FIELD_FROM_DISPLAYED_COMPONENT', () => { const action = {
// it('Should remove the selected field', () => { type: actions.REMOVE_COMPONENT_FROM_DYNAMIC_ZONE,
// const state = fromJS({ dzName: 'dz',
// modifiedData: { componentToRemoveIndex: 1,
// components: { };
// 'default.test': {
// schema: {
// attributes: {
// text: {
// type: 'text',
// },
// other: {
// type: 'string',
// },
// last: {
// type: 'integer',
// },
// },
// },
// },
// },
// },
// });
// const expected = fromJS({ const expected = {
// modifiedData: { ...initialState,
// components: { components,
// 'default.test': { modifiedData: {
// schema: { components,
// attributes: { contentType: {
// text: { uid: 'application::address.address',
// type: 'text', schema: {
// }, name: 'address',
// last: { description: '',
// type: 'integer', connection: 'default',
// }, collectionName: 'addresses',
// }, attributes: [
// }, {
// }, name: 'full_name',
// }, type: 'string',
// }, required: true,
// }); },
{
name: 'dz',
type: 'dynamiczone',
components: ['default.openingtimes'],
},
{
name: 'otherDz',
type: 'dynamiczone',
components: ['default.openingtimes', 'default.dish'],
},
],
},
},
},
};
// expect( expect(reducer(state, action)).toEqual(expected);
// reducer(state, { });
// type: actions.REMOVE_FIELD_FROM_DISPLAYED_COMPONENT, });
// componentUid: 'default.test',
// attributeToRemoveName: 'other', describe('REMOVE_FIELD_FROM_DISPLAYED_COMPONENT', () => {
// }) it('Should remove the selected field', () => {
// ).toEqual(expected); const state = {
// }); ...initialState,
// }); modifiedData: {
components: {
'default.test': {
schema: {
attributes: [
{
name: 'text',
type: 'text',
},
{
name: 'other',
type: 'string',
},
{
name: 'last',
type: 'integer',
},
],
},
},
},
},
};
const action = {
type: actions.REMOVE_FIELD_FROM_DISPLAYED_COMPONENT,
componentUid: 'default.test',
attributeToRemoveName: 'other',
};
const expected = {
...initialState,
modifiedData: {
components: {
'default.test': {
schema: {
attributes: [
{
name: 'text',
type: 'text',
},
{
name: 'last',
type: 'integer',
},
],
},
},
},
},
};
expect(reducer(state, action)).toEqual(expected);
});
});
describe('SET_MODIFIED_DATA', () => { describe('SET_MODIFIED_DATA', () => {
it('Should set the modifiedData object correctly if the user did create a new type', () => { it('Should set the modifiedData object correctly if the user did create a new type', () => {
@ -974,142 +813,154 @@ describe('CTB | components | DataManagerProvider | reducer | basics actions ', (
}); });
}); });
// describe('UPDATE_SCHEMA', () => { describe('UPDATE_SCHEMA', () => {
// it('Should update the modified data correctly if the schemaType is a content type', () => { it('Should update the modified data correctly if the schemaType is a content type', () => {
// const data = { const data = {
// name: 'test1', name: 'test1',
// collectionName: 'newTest', collectionName: 'newTest',
// }; };
// const state = fromJS({
// modifiedData: {
// components: {},
// contentType: {
// uid: 'test',
// schema: {
// name: 'test',
// collectionName: 'test',
// attributes: {
// something: {
// type: 'string',
// },
// },
// },
// },
// },
// });
// const expected = fromJS({
// modifiedData: {
// components: {},
// contentType: {
// uid: 'test',
// schema: {
// name: 'test1',
// collectionName: 'newTest',
// attributes: {
// something: {
// type: 'string',
// },
// },
// },
// },
// },
// });
// expect( const state = {
// reducer(state, { ...initialState,
// type: actions.UPDATE_SCHEMA, modifiedData: {
// data, components: {},
// schemaType: 'contentType', contentType: {
// }) uid: 'test',
// ).toEqual(expected); schema: {
// }); name: 'test',
collectionName: 'test',
attributes: [
{
name: 'something',
type: 'string',
},
],
},
},
},
};
// it('Should update the modified data correctly if the schemaType is a component', () => { const action = {
// const data = { type: actions.UPDATE_SCHEMA,
// name: 'newTest', data,
// collectionName: 'newTest', schemaType: 'contentType',
// category: 'test', };
// icon: 'test', const expected = {
// }; ...initialState,
// const state = fromJS({ modifiedData: {
// components: { components: {},
// test: { contentType: {
// uid: 'test', uid: 'test',
// category: 'default', schema: {
// schema: { name: 'test1',
// name: 'test', collectionName: 'newTest',
// icon: 'book', attributes: [
// collectionName: 'components_tests', {
// attributes: { name: 'something',
// something: { type: 'string',
// type: 'string', },
// }, ],
// }, },
// }, },
// }, },
// }, };
// modifiedData: {
// components: {},
// component: {
// uid: 'test',
// category: 'default',
// schema: {
// name: 'test',
// icon: 'book',
// collectionName: 'components_tests',
// attributes: {
// something: {
// type: 'string',
// },
// },
// },
// },
// },
// });
// const expected = fromJS({
// components: {
// test: {
// uid: 'test',
// category: 'test',
// schema: {
// name: 'newTest',
// icon: 'test',
// collectionName: 'newTest',
// attributes: {
// something: {
// type: 'string',
// },
// },
// },
// },
// },
// modifiedData: {
// components: {},
// component: {
// uid: 'test',
// category: 'test',
// schema: {
// name: 'newTest',
// icon: 'test',
// collectionName: 'newTest',
// attributes: {
// something: {
// type: 'string',
// },
// },
// },
// },
// },
// });
// expect( expect(reducer(state, action)).toEqual(expected);
// reducer(state, { });
// type: actions.UPDATE_SCHEMA,
// data, it('Should update the modified data correctly if the schemaType is a component', () => {
// schemaType: 'component', const data = {
// uid: 'test', name: 'newTest',
// }) collectionName: 'newTest',
// ).toEqual(expected); category: 'test',
// }); icon: 'test',
// }); };
const state = {
...initialState,
components: {
test: {
uid: 'test',
category: 'default',
schema: {
name: 'test',
icon: 'book',
collectionName: 'components_tests',
attributes: [
{
name: 'something',
type: 'string',
},
],
},
},
},
modifiedData: {
components: {},
component: {
uid: 'test',
category: 'default',
schema: {
name: 'test',
icon: 'book',
collectionName: 'components_tests',
attributes: [
{
name: 'something',
type: 'string',
},
],
},
},
},
};
const action = {
type: actions.UPDATE_SCHEMA,
data,
schemaType: 'component',
uid: 'test',
};
const expected = {
...initialState,
components: {
test: {
uid: 'test',
category: 'test',
schema: {
name: 'newTest',
icon: 'test',
collectionName: 'newTest',
attributes: [
{
name: 'something',
type: 'string',
},
],
},
},
},
modifiedData: {
components: {},
component: {
uid: 'test',
category: 'test',
schema: {
name: 'newTest',
icon: 'test',
collectionName: 'newTest',
attributes: [
{
name: 'something',
type: 'string',
},
],
},
},
},
};
expect(reducer(state, action)).toEqual(expected);
});
});
}); });