2019-11-06 13:02:48 +01:00
|
|
|
import React, { useCallback, useState } from 'react';
|
2019-11-04 09:00:59 +01:00
|
|
|
import { get } from 'lodash';
|
2019-10-29 18:03:06 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2019-11-04 10:10:38 +01:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
|
|
|
|
import pluginId from '../../pluginId';
|
2019-11-04 09:00:59 +01:00
|
|
|
import useDataManager from '../../hooks/useDataManager';
|
2019-11-04 10:10:38 +01:00
|
|
|
import DynamicComponentCard from '../DynamicComponentCard';
|
2019-11-04 09:00:59 +01:00
|
|
|
import Button from './Button';
|
2019-11-04 10:10:38 +01:00
|
|
|
import ComponentsPicker from './ComponentsPicker';
|
2019-11-04 09:00:59 +01:00
|
|
|
import Wrapper from './Wrapper';
|
2019-10-29 18:03:06 +01:00
|
|
|
|
|
|
|
|
const DynamicZone = ({ name }) => {
|
2019-11-04 09:00:59 +01:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
2019-11-04 10:10:38 +01:00
|
|
|
const [isOver, setIsOver] = useState(false);
|
2019-11-04 14:23:46 +01:00
|
|
|
const {
|
|
|
|
|
addComponentToDynamicZone,
|
2019-11-06 13:02:48 +01:00
|
|
|
allLayoutData,
|
2019-11-04 14:23:46 +01:00
|
|
|
layout,
|
2019-11-06 13:02:48 +01:00
|
|
|
modifiedData,
|
2019-11-04 14:23:46 +01:00
|
|
|
} = useDataManager();
|
2019-11-06 13:02:48 +01:00
|
|
|
const getDynamicDisplayedComponents = useCallback(() => {
|
|
|
|
|
return get(modifiedData, [name], []).map(data => data.__component);
|
|
|
|
|
}, [modifiedData, name]);
|
|
|
|
|
const getDynamicComponentSchemaData = useCallback(
|
|
|
|
|
compoUid => {
|
|
|
|
|
return get(allLayoutData, compoUid, {});
|
|
|
|
|
},
|
|
|
|
|
[allLayoutData]
|
|
|
|
|
);
|
2019-11-04 14:23:46 +01:00
|
|
|
|
2019-11-04 09:00:59 +01:00
|
|
|
const dynamicZoneAvailableComponents = get(
|
|
|
|
|
layout,
|
|
|
|
|
['schema', 'attributes', name, 'components'],
|
|
|
|
|
[]
|
|
|
|
|
);
|
2019-11-04 10:10:38 +01:00
|
|
|
const displayInfo = isOver && !isOpen;
|
2019-11-04 14:23:46 +01:00
|
|
|
const handleMouseEvent = () => setIsOver(prev => !prev);
|
2019-11-04 09:00:59 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2019-11-04 10:10:38 +01:00
|
|
|
<Wrapper show={displayInfo}>
|
2019-11-04 09:00:59 +01:00
|
|
|
<Button
|
|
|
|
|
isOpen={isOpen}
|
|
|
|
|
type="button"
|
2019-11-04 10:10:38 +01:00
|
|
|
onMouseEnter={handleMouseEvent}
|
|
|
|
|
onMouseLeave={handleMouseEvent}
|
2019-11-04 09:00:59 +01:00
|
|
|
onClick={() => setIsOpen(prev => !prev)}
|
2019-11-05 09:07:03 +01:00
|
|
|
/>
|
2019-11-04 10:10:38 +01:00
|
|
|
|
|
|
|
|
<div className="info">
|
|
|
|
|
<FormattedMessage
|
|
|
|
|
id={`${pluginId}.components.DynamicZone.add-compo`}
|
|
|
|
|
values={{ componentName: name }}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<ComponentsPicker isOpen={isOpen}>
|
2019-11-04 14:23:46 +01:00
|
|
|
{dynamicZoneAvailableComponents.map(componentUid => {
|
|
|
|
|
return (
|
|
|
|
|
<DynamicComponentCard
|
|
|
|
|
key={componentUid}
|
|
|
|
|
componentUid={componentUid}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setIsOpen(false);
|
|
|
|
|
addComponentToDynamicZone(name, componentUid);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2019-11-04 10:10:38 +01:00
|
|
|
})}
|
|
|
|
|
</ComponentsPicker>
|
2019-11-06 13:02:48 +01:00
|
|
|
{getDynamicDisplayedComponents().map((componentUid, index) => {
|
|
|
|
|
console.log({ all: getDynamicComponentSchemaData(componentUid) });
|
|
|
|
|
return <div key={index}>{componentUid}</div>;
|
|
|
|
|
})}
|
2019-11-04 09:00:59 +01:00
|
|
|
</Wrapper>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2019-10-29 18:03:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
DynamicZone.propTypes = {
|
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { DynamicZone };
|
2019-11-04 14:23:46 +01:00
|
|
|
export default DynamicZone;
|