Add components to dynamic zone

This commit is contained in:
soupette 2019-11-28 15:10:59 +01:00
parent 3fc5c8fda9
commit 28aecbae74
5 changed files with 71 additions and 19 deletions

View File

@ -9,7 +9,7 @@ import Ul from './Ul';
import hasSubArray from './utils/hasSubArray'; import hasSubArray from './utils/hasSubArray';
const MultipleMenuList = ({ const MultipleMenuList = ({
selectProps: { name, onClickAddComponentsToDynamicZone, refState, value }, selectProps: { name, addComponentsToDynamicZone, refState, value },
...rest ...rest
}) => { }) => {
const { componentsGroupedByCategory } = useDataManager(); const { componentsGroupedByCategory } = useDataManager();
@ -41,7 +41,7 @@ const MultipleMenuList = ({
components: allComponentsCategory[target.name], components: allComponentsCategory[target.name],
shouldAddComponents: target.value, shouldAddComponents: target.value,
}; };
onClickAddComponentsToDynamicZone({ target: dataTarget }); addComponentsToDynamicZone({ target: dataTarget });
}; };
const handleChange = ({ target }) => { const handleChange = ({ target }) => {
@ -51,7 +51,7 @@ const MultipleMenuList = ({
shouldAddComponents: target.value, shouldAddComponents: target.value,
}; };
onClickAddComponentsToDynamicZone({ target: dataTarget }); addComponentsToDynamicZone({ target: dataTarget });
}; };
return ( return (
@ -140,8 +140,8 @@ MultipleMenuList.defaultProps = {
MultipleMenuList.propTypes = { MultipleMenuList.propTypes = {
selectProps: PropTypes.shape({ selectProps: PropTypes.shape({
addComponentsToDynamicZone: PropTypes.func.isRequired,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
onClickAddComponentsToDynamicZone: PropTypes.func.isRequired,
refState: PropTypes.object, refState: PropTypes.object,
value: PropTypes.object, value: PropTypes.object,
}).isRequired, }).isRequired,

View File

@ -6,12 +6,12 @@ import MultipleMenuList from './MultipleMenuList';
import Value from './Value'; import Value from './Value';
const ComponentSelect = ({ const ComponentSelect = ({
addComponentsToDynamicZone,
componentCategoryNeededForAddingAfieldWhileCreatingAComponent, componentCategoryNeededForAddingAfieldWhileCreatingAComponent,
componentNameNeededForAddingAfieldWhileCreatingAComponent, componentNameNeededForAddingAfieldWhileCreatingAComponent,
isCreatingComponentWhileAddingAField, isCreatingComponentWhileAddingAField,
isMultiple, isMultiple,
onChange, onChange,
onClickAddComponentsToDynamicZone,
name, name,
value, value,
styles, styles,
@ -34,6 +34,7 @@ const ComponentSelect = ({
return ( return (
<Select <Select
addComponentsToDynamicZone={addComponentsToDynamicZone}
isClearable={!isMultiple} isClearable={!isMultiple}
isDisabled={isCreatingComponentWhileAddingAField} isDisabled={isCreatingComponentWhileAddingAField}
isCreatingComponent={isCreatingComponentWhileAddingAField} isCreatingComponent={isCreatingComponentWhileAddingAField}
@ -44,7 +45,6 @@ const ComponentSelect = ({
componentName={componentNameNeededForAddingAfieldWhileCreatingAComponent} componentName={componentNameNeededForAddingAfieldWhileCreatingAComponent}
name={name} name={name}
onChange={handleChange} onChange={handleChange}
onClickAddComponentsToDynamicZone={onClickAddComponentsToDynamicZone}
onClickOption={onChange} onClickOption={onChange}
styles={styles} styles={styles}
value={{ label: value, value }} value={{ label: value, value }}
@ -69,6 +69,7 @@ ComponentSelect.defaultProps = {
}; };
ComponentSelect.propTypes = { ComponentSelect.propTypes = {
addComponentsToDynamicZone: PropTypes.func.isRequired,
componentCategoryNeededForAddingAfieldWhileCreatingAComponent: componentCategoryNeededForAddingAfieldWhileCreatingAComponent:
PropTypes.string, PropTypes.string,
componentNameNeededForAddingAfieldWhileCreatingAComponent: PropTypes.string, componentNameNeededForAddingAfieldWhileCreatingAComponent: PropTypes.string,
@ -76,7 +77,7 @@ ComponentSelect.propTypes = {
isMultiple: PropTypes.bool, isMultiple: PropTypes.bool,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired,
onClickAddComponentsToDynamicZone: PropTypes.func.isRequired,
styles: PropTypes.object.isRequired, styles: PropTypes.object.isRequired,
value: PropTypes.oneOfType([ value: PropTypes.oneOfType([
PropTypes.string, PropTypes.string,

View File

@ -94,9 +94,12 @@ const DataManagerProvider = ({ allIcons, children }) => {
shouldAddComponentToData, shouldAddComponentToData,
}); });
}; };
const addComponentsToDynamicZone = (dynamicZoneTarget, componentsToAdd) => { const addCreatedComponentToDynamicZone = (
dynamicZoneTarget,
componentsToAdd
) => {
dispatch({ dispatch({
type: 'ADD_COMPONENTS_TO_DYNAMIC_ZONE', type: 'ADD_CREATED_COMPONENT_TO_DYNAMIC_ZONE',
dynamicZoneTarget, dynamicZoneTarget,
componentsToAdd, componentsToAdd,
}); });
@ -122,6 +125,14 @@ const DataManagerProvider = ({ allIcons, children }) => {
shouldAddComponentToData, shouldAddComponentToData,
}); });
}; };
const changeDynamicZoneComponents = (dynamicZoneTarget, newComponents) => {
console.log({ newComponents });
dispatch({
type: 'CHANGE_DYNAMIC_ZONE_COMPONENTS',
dynamicZoneTarget,
newComponents,
});
};
const removeAttribute = ( const removeAttribute = (
mainDataKey, mainDataKey,
attributeToRemoveName, attributeToRemoveName,
@ -204,7 +215,7 @@ const DataManagerProvider = ({ allIcons, children }) => {
<DataManagerContext.Provider <DataManagerContext.Provider
value={{ value={{
addAttribute, addAttribute,
addComponentsToDynamicZone, addCreatedComponentToDynamicZone,
allComponentsCategories: retrieveSpecificInfoFromComponents( allComponentsCategories: retrieveSpecificInfoFromComponents(
components, components,
['category'] ['category']
@ -213,6 +224,7 @@ const DataManagerProvider = ({ allIcons, children }) => {
components, components,
['schema', 'icon'] ['schema', 'icon']
), ),
changeDynamicZoneComponents,
components, components,
componentsGroupedByCategory: groupBy(components, 'category'), componentsGroupedByCategory: groupBy(components, 'category'),
contentTypes, contentTypes,

View File

@ -93,7 +93,7 @@ const reducer = (state, action) => {
return existingCompos; return existingCompos;
}); });
} }
case 'ADD_COMPONENTS_TO_DYNAMIC_ZONE': { case 'ADD_CREATED_COMPONENT_TO_DYNAMIC_ZONE': {
const { dynamicZoneTarget, componentsToAdd } = action; const { dynamicZoneTarget, componentsToAdd } = action;
return state.updateIn( return state.updateIn(
@ -110,6 +110,32 @@ const reducer = (state, action) => {
} }
); );
} }
case 'CHANGE_DYNAMIC_ZONE_COMPONENTS': {
const { dynamicZoneTarget, newComponents } = action;
return state
.updateIn(
[
'modifiedData',
'contentType',
'schema',
'attributes',
dynamicZoneTarget,
'components',
],
() => fromJS(newComponents)
)
.updateIn(['modifiedData', 'components'], old => {
const componentsSchema = newComponents.reduce((acc, current) => {
const addedCompoSchema = state.getIn(['components', current]);
return acc.set(current, addedCompoSchema);
}, old);
return componentsSchema;
});
}
case 'CREATE_SCHEMA': { case 'CREATE_SCHEMA': {
const newSchema = { const newSchema = {
uid: action.uid, uid: action.uid,

View File

@ -47,7 +47,8 @@ const FormModal = () => {
const attributeOptionRef = useRef(); const attributeOptionRef = useRef();
const { const {
addAttribute, addAttribute,
addComponentsToDynamicZone, addCreatedComponentToDynamicZone,
changeDynamicZoneComponents,
contentTypes, contentTypes,
components, components,
createSchema, createSchema,
@ -128,7 +129,6 @@ const FormModal = () => {
// This condition is added to prevent the reducer state to be cleared when navigating from the base tab to tha advanced one // This condition is added to prevent the reducer state to be cleared when navigating from the base tab to tha advanced one
state.modalType !== 'attribute' state.modalType !== 'attribute'
) { ) {
console.log('effect');
const attributeToEditNotFormatted = get( const attributeToEditNotFormatted = get(
allDataSchema, allDataSchema,
[...pathToSchema, 'schema', 'attributes', attributeName], [...pathToSchema, 'schema', 'attributes', attributeName],
@ -538,10 +538,23 @@ const FormModal = () => {
// Like explained above we will be able to modify the created component structure // Like explained above we will be able to modify the created component structure
isCreatingComponentFromAView isCreatingComponentFromAView
); );
addComponentsToDynamicZone(state.dynamicZoneTarget, [componentUid]); // Add the created component to the DZ
// We don't want to remove the old ones
addCreatedComponentToDynamicZone(state.dynamicZoneTarget, [
componentUid,
]);
// TODO change routing so the user can add fields to the created component
push({ search: '' });
} else { } else {
console.log('not ready'); // Add the components to the DZ
// TODO apply components to DZ changeDynamicZoneComponents(
state.dynamicZoneTarget,
modifiedData.components
);
// TODO nav
push({ search: '' });
} }
} else { } else {
console.log('step 2???'); console.log('step 2???');
@ -825,6 +838,9 @@ const FormModal = () => {
key={input.name} key={input.name}
> >
<Inputs <Inputs
addComponentsToDynamicZone={
handleClickAddComponentsToDynamicZone
}
customInputs={{ customInputs={{
componentIconPicker: ComponentIconPicker, componentIconPicker: ComponentIconPicker,
componentSelect: WrapperSelect, componentSelect: WrapperSelect,
@ -859,9 +875,6 @@ const FormModal = () => {
: formatMessage({ id: errorId }) : formatMessage({ id: errorId })
} }
onChange={handleChange} onChange={handleChange}
onClickAddComponentsToDynamicZone={
handleClickAddComponentsToDynamicZone
}
onBlur={() => {}} onBlur={() => {}}
description={ description={
get(input, 'description.id', null) get(input, 'description.id', null)