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';
|
2019-12-02 19:20:55 +01:00
|
|
|
import { Arrow } from '@buffetjs/icons';
|
2019-12-06 11:09:48 +01:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2019-11-04 10:10:38 +01:00
|
|
|
import pluginId from '../../pluginId';
|
2019-11-04 09:00:59 +01:00
|
|
|
import useDataManager from '../../hooks/useDataManager';
|
2019-11-19 16:17:15 +01:00
|
|
|
import useEditView from '../../hooks/useEditView';
|
2019-11-04 10:10:38 +01:00
|
|
|
import DynamicComponentCard from '../DynamicComponentCard';
|
2019-11-06 16:29:19 +01:00
|
|
|
import FieldComponent from '../FieldComponent';
|
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-06 16:29:19 +01:00
|
|
|
import ComponentWrapper from './ComponentWrapper';
|
2019-11-26 16:15:59 +01:00
|
|
|
import DynamicZoneWrapper from './DynamicZoneWrapper';
|
2019-11-06 16:29:19 +01:00
|
|
|
import Label from './Label';
|
|
|
|
|
import RoundCTA from './RoundCTA';
|
2019-11-04 09:00:59 +01:00
|
|
|
import Wrapper from './Wrapper';
|
2019-10-29 18:03:06 +01:00
|
|
|
|
2020-01-21 16:03:31 +01:00
|
|
|
/* eslint-disable react/no-array-index-key */
|
|
|
|
|
|
2019-11-08 16:17:20 +01:00
|
|
|
const DynamicZone = ({ max, min, name }) => {
|
2019-11-20 17:33:49 +01:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
2019-11-04 14:23:46 +01:00
|
|
|
const {
|
|
|
|
|
addComponentToDynamicZone,
|
2019-11-08 16:17:20 +01:00
|
|
|
formErrors,
|
2019-11-04 14:23:46 +01:00
|
|
|
layout,
|
2019-11-06 13:02:48 +01:00
|
|
|
modifiedData,
|
2019-11-06 16:29:19 +01:00
|
|
|
moveComponentUp,
|
|
|
|
|
moveComponentDown,
|
|
|
|
|
removeComponentFromDynamicZone,
|
2019-11-04 14:23:46 +01:00
|
|
|
} = useDataManager();
|
2019-11-18 18:14:36 +01:00
|
|
|
|
2019-11-19 16:17:15 +01:00
|
|
|
const { components } = useEditView();
|
|
|
|
|
|
2019-11-06 13:02:48 +01:00
|
|
|
const getDynamicDisplayedComponents = useCallback(() => {
|
|
|
|
|
return get(modifiedData, [name], []).map(data => data.__component);
|
|
|
|
|
}, [modifiedData, name]);
|
2019-11-04 14:23:46 +01:00
|
|
|
|
2019-11-19 16:17:15 +01:00
|
|
|
const getDynamicComponentSchemaData = componentUid => {
|
|
|
|
|
const component = components.find(compo => compo.uid === componentUid);
|
|
|
|
|
const { schema } = component;
|
|
|
|
|
|
|
|
|
|
return schema;
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-06 11:37:58 +01:00
|
|
|
const getDynamicComponentInfos = componentUid => {
|
2019-11-19 16:17:15 +01:00
|
|
|
const {
|
2019-12-06 11:37:58 +01:00
|
|
|
info: { icon, name },
|
2019-11-19 16:17:15 +01:00
|
|
|
} = getDynamicComponentSchemaData(componentUid);
|
|
|
|
|
|
2019-12-06 11:37:58 +01:00
|
|
|
return { icon, name };
|
2019-11-19 16:17:15 +01:00
|
|
|
};
|
|
|
|
|
|
2019-11-08 16:17:20 +01:00
|
|
|
const dynamicZoneErrors = Object.keys(formErrors)
|
|
|
|
|
.filter(key => {
|
|
|
|
|
return key === name;
|
|
|
|
|
})
|
|
|
|
|
.map(key => formErrors[key]);
|
|
|
|
|
|
2019-11-04 09:00:59 +01:00
|
|
|
const dynamicZoneAvailableComponents = get(
|
|
|
|
|
layout,
|
|
|
|
|
['schema', 'attributes', name, 'components'],
|
|
|
|
|
[]
|
|
|
|
|
);
|
2019-11-18 18:14:36 +01:00
|
|
|
|
2019-11-06 16:29:19 +01:00
|
|
|
const metas = get(layout, ['metadatas', name, 'edit'], {});
|
|
|
|
|
const dynamicDisplayedComponentsLength = getDynamicDisplayedComponents()
|
|
|
|
|
.length;
|
2019-11-08 16:17:20 +01:00
|
|
|
const missingComponentNumber = min - dynamicDisplayedComponentsLength;
|
|
|
|
|
const hasError = dynamicZoneErrors.length > 0;
|
|
|
|
|
const hasMinError =
|
|
|
|
|
dynamicZoneErrors.length > 0 &&
|
2019-11-20 17:33:49 +01:00
|
|
|
get(dynamicZoneErrors, [0, 'id'], '').includes('min');
|
|
|
|
|
|
|
|
|
|
const hasRequiredError = hasError && !hasMinError;
|
2019-12-11 16:52:35 +01:00
|
|
|
const hasMaxError =
|
|
|
|
|
hasError &&
|
|
|
|
|
get(dynamicZoneErrors, [0, 'id'], '') ===
|
|
|
|
|
'components.Input.error.validation.max';
|
2019-11-20 17:33:49 +01:00
|
|
|
|
2019-11-04 09:00:59 +01:00
|
|
|
return (
|
2019-11-26 16:15:59 +01:00
|
|
|
<DynamicZoneWrapper>
|
2019-11-06 16:29:19 +01:00
|
|
|
{getDynamicDisplayedComponents().length > 0 && (
|
|
|
|
|
<Label>
|
|
|
|
|
<p>{metas.label}</p>
|
|
|
|
|
<p>{metas.description}</p>
|
|
|
|
|
</Label>
|
|
|
|
|
)}
|
2019-11-19 18:26:01 +01:00
|
|
|
|
|
|
|
|
<ComponentWrapper>
|
|
|
|
|
{getDynamicDisplayedComponents().map((componentUid, index) => {
|
|
|
|
|
const showDownIcon =
|
|
|
|
|
dynamicDisplayedComponentsLength > 0 &&
|
|
|
|
|
index < dynamicDisplayedComponentsLength - 1;
|
|
|
|
|
const showUpIcon = dynamicDisplayedComponentsLength > 0 && index > 0;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div key={index}>
|
2019-12-02 19:20:55 +01:00
|
|
|
<div className="arrow-icons">
|
|
|
|
|
{showDownIcon && (
|
|
|
|
|
<RoundCTA
|
|
|
|
|
className="arrow-btn arrow-down"
|
|
|
|
|
onClick={() => moveComponentDown(name, index)}
|
|
|
|
|
>
|
|
|
|
|
<Arrow />
|
|
|
|
|
</RoundCTA>
|
|
|
|
|
)}
|
|
|
|
|
{showUpIcon && (
|
|
|
|
|
<RoundCTA
|
|
|
|
|
className="arrow-btn arrow-up"
|
|
|
|
|
onClick={() => moveComponentUp(name, index)}
|
|
|
|
|
>
|
|
|
|
|
<Arrow />
|
|
|
|
|
</RoundCTA>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2019-11-19 18:26:01 +01:00
|
|
|
|
2019-11-06 16:29:19 +01:00
|
|
|
<RoundCTA
|
2019-11-19 18:26:01 +01:00
|
|
|
onClick={() => removeComponentFromDynamicZone(name, index)}
|
2019-11-06 16:29:19 +01:00
|
|
|
>
|
2020-01-21 16:03:31 +01:00
|
|
|
<FontAwesomeIcon icon="trash" />
|
2019-11-06 16:29:19 +01:00
|
|
|
</RoundCTA>
|
2019-11-19 18:26:01 +01:00
|
|
|
<FieldComponent
|
|
|
|
|
componentUid={componentUid}
|
2019-12-09 18:09:48 +01:00
|
|
|
componentFriendlyName={
|
|
|
|
|
getDynamicComponentInfos(componentUid).name
|
|
|
|
|
}
|
2019-12-06 11:37:58 +01:00
|
|
|
icon={getDynamicComponentInfos(componentUid).icon}
|
2019-11-19 18:26:01 +01:00
|
|
|
label=""
|
|
|
|
|
name={`${name}.${index}`}
|
2020-01-21 16:03:31 +01:00
|
|
|
isFromDynamicZone
|
2019-11-19 18:26:01 +01:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</ComponentWrapper>
|
2019-11-18 18:14:36 +01:00
|
|
|
<Wrapper>
|
2019-11-04 09:00:59 +01:00
|
|
|
<Button
|
|
|
|
|
type="button"
|
2019-11-18 18:14:36 +01:00
|
|
|
hasError={hasError}
|
|
|
|
|
className={isOpen && 'isOpen'}
|
2019-11-08 16:17:20 +01:00
|
|
|
onClick={() => {
|
|
|
|
|
if (dynamicDisplayedComponentsLength < max) {
|
|
|
|
|
setIsOpen(prev => !prev);
|
|
|
|
|
} else {
|
|
|
|
|
strapi.notification.info(
|
2019-12-06 11:37:58 +01:00
|
|
|
`${pluginId}.components.notification.info.maximum-requirement`
|
2019-11-08 16:17:20 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}}
|
2019-11-05 09:07:03 +01:00
|
|
|
/>
|
2019-12-11 16:52:35 +01:00
|
|
|
{hasRequiredError && !isOpen && !hasMaxError && (
|
2019-11-21 16:49:06 +01:00
|
|
|
<div className="error-label">Component is required</div>
|
|
|
|
|
)}
|
2019-12-11 16:52:35 +01:00
|
|
|
{hasMaxError && !isOpen && (
|
|
|
|
|
<div className="error-label">
|
|
|
|
|
<FormattedMessage id="components.Input.error.validation.max" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2019-11-26 16:15:59 +01:00
|
|
|
{hasMinError && !isOpen && (
|
2019-11-21 16:49:06 +01:00
|
|
|
<div className="error-label">
|
2019-11-26 16:15:59 +01:00
|
|
|
<FormattedMessage
|
|
|
|
|
id={`${pluginId}.components.DynamicZone.missing${
|
|
|
|
|
missingComponentNumber > 1 ? '.plural' : '.singular'
|
|
|
|
|
}`}
|
|
|
|
|
values={{ count: missingComponentNumber }}
|
|
|
|
|
/>
|
2019-11-21 16:49:06 +01:00
|
|
|
</div>
|
|
|
|
|
)}
|
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-18 18:14:36 +01:00
|
|
|
<div>
|
|
|
|
|
<p className="componentPickerTitle">
|
|
|
|
|
<FormattedMessage
|
|
|
|
|
id={`${pluginId}.components.DynamicZone.pick-compo`}
|
2019-11-04 14:23:46 +01:00
|
|
|
/>
|
2019-11-18 18:14:36 +01:00
|
|
|
</p>
|
|
|
|
|
<div className="componentsList">
|
|
|
|
|
{dynamicZoneAvailableComponents.map(componentUid => {
|
2019-12-06 11:37:58 +01:00
|
|
|
const { icon, name: friendlyName } = getDynamicComponentInfos(
|
|
|
|
|
componentUid
|
|
|
|
|
);
|
|
|
|
|
|
2019-11-18 18:14:36 +01:00
|
|
|
return (
|
|
|
|
|
<DynamicComponentCard
|
|
|
|
|
key={componentUid}
|
|
|
|
|
componentUid={componentUid}
|
2019-12-06 11:37:58 +01:00
|
|
|
friendlyName={friendlyName}
|
|
|
|
|
icon={icon}
|
2019-11-18 18:14:36 +01:00
|
|
|
onClick={() => {
|
|
|
|
|
setIsOpen(false);
|
|
|
|
|
const shouldCheckErrors = hasError;
|
|
|
|
|
addComponentToDynamicZone(
|
|
|
|
|
name,
|
|
|
|
|
componentUid,
|
|
|
|
|
shouldCheckErrors
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2019-11-04 10:10:38 +01:00
|
|
|
</ComponentsPicker>
|
2019-11-04 09:00:59 +01:00
|
|
|
</Wrapper>
|
2019-11-26 16:15:59 +01:00
|
|
|
</DynamicZoneWrapper>
|
2019-11-04 09:00:59 +01:00
|
|
|
);
|
2019-10-29 18:03:06 +01:00
|
|
|
};
|
|
|
|
|
|
2019-11-08 16:17:20 +01:00
|
|
|
DynamicZone.defaultProps = {
|
|
|
|
|
max: Infinity,
|
|
|
|
|
min: -Infinity,
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-29 18:03:06 +01:00
|
|
|
DynamicZone.propTypes = {
|
2019-11-08 16:29:51 +01:00
|
|
|
max: PropTypes.number,
|
|
|
|
|
min: PropTypes.number,
|
2019-10-29 18:03:06 +01:00
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { DynamicZone };
|
2019-11-04 14:23:46 +01:00
|
|
|
export default DynamicZone;
|