import React, { memo, useCallback, useMemo, useEffect, useReducer, useRef, } from 'react'; import PropTypes from 'prop-types'; import { get } from 'lodash'; import { useHistory, useLocation } from 'react-router-dom'; import { BackHeader, getQueryParameters, LiLink } from 'strapi-helper-plugin'; import pluginId from '../../pluginId'; import Container from '../../components/Container'; import DynamicZone from '../../components/DynamicZone'; import FormWrapper from '../../components/FormWrapper'; // import ComponentField from '../../components/ComponentField'; import Inputs from '../../components/Inputs'; import SelectWrapper from '../../components/SelectWrapper'; import EditViewDataManagerProvider from '../EditViewDataManagerProvider'; import EditViewProvider from '../EditViewProvider'; import Header from './Header'; import getInjectedComponents from './utils/getComponents'; import init from './init'; import reducer, { initialState } from './reducer'; import { LinkWrapper, SubWrapper } from './components'; import createAttributesLayout from './utils/createAttributesLayout'; const EditView = ({ currentEnvironment, emitEvent, layouts, plugins, slug, }) => { const formatLayoutRef = useRef(); formatLayoutRef.current = createAttributesLayout; // Retrieve push to programmatically navigate between views const { push } = useHistory(); // Retrieve the search const { search } = useLocation(); // eslint-disable-next-line react-hooks/exhaustive-deps const [reducerState, dispatch] = useReducer(reducer, initialState, () => init(initialState) ); const allLayoutData = useMemo(() => get(layouts, [slug], {}), [ layouts, slug, ]); const currentContentTypeLayoutData = useMemo( () => get(allLayoutData, ['contentType'], {}), [allLayoutData] ); const currentContentTypeLayout = useMemo( () => get(currentContentTypeLayoutData, ['layouts', 'edit'], []), [currentContentTypeLayoutData] ); const currentContentTypeLayoutRelations = useMemo( () => get(currentContentTypeLayoutData, ['layouts', 'editRelations'], []), [currentContentTypeLayoutData] ); const currentContentTypeSchema = useMemo( () => get(currentContentTypeLayoutData, ['schema'], {}), [currentContentTypeLayoutData] ); const source = getQueryParameters(search, 'source'); const getFieldType = useCallback( fieldName => { return get( currentContentTypeSchema, ['attributes', fieldName, 'type'], '' ); }, [currentContentTypeSchema] ); // Check if a block is a dynamic zone const isDynamicZone = useCallback( block => { return block.every(subBlock => { return subBlock.every(obj => getFieldType(obj.name) === 'dynamiczone'); }); }, [getFieldType] ); useEffect(() => { // Force state to be cleared when navigation from one entry to another dispatch({ type: 'RESET_PROPS' }); dispatch({ type: 'SET_LAYOUT_DATA', formattedContentTypeLayout: formatLayoutRef.current( currentContentTypeLayout, currentContentTypeSchema.attributes ), }); }, [currentContentTypeLayout, currentContentTypeSchema.attributes]); const { formattedContentTypeLayout } = reducerState.toJS(); // We can't use the getQueryParameters helper here because the search // can contain 'redirectUrl' several times since we can navigate between documents const redirectURL = search .split('redirectUrl=') .filter((_, index) => index !== 0) .join(''); const redirectToPreviousPage = () => push(redirectURL); return ( redirectToPreviousPage()} />
{formattedContentTypeLayout.map((block, blockIndex) => { if (isDynamicZone(block)) { const { 0: { 0: { name }, }, } = block; return ; } return ( {block.map((fieldsBlock, fieldsBlockIndex) => { return (
{fieldsBlock.map(({ name, size }, fieldIndex) => { const isComponent = getFieldType(name) === 'component'; if (isComponent) { return (
COMPONENT: {name}
); } return (
{}} />
); })}
); })}
); })}
{currentContentTypeLayoutRelations.length > 0 && (
{currentContentTypeLayoutRelations.map(relationName => { const relation = get( currentContentTypeLayoutData, ['schema', 'attributes', relationName], {} ); const relationMetas = get( currentContentTypeLayoutData, ['metadatas', relationName, 'edit'], {} ); return ( ); })}
)}
    { // emitEvent('willEditContentTypeLayoutFromEditView'); }} /> {getInjectedComponents( 'right.links', plugins, currentEnvironment, slug, source, emitEvent )}
); }; EditView.defaultProps = { currentEnvironment: 'production', emitEvent: () => {}, plugins: {}, }; EditView.propTypes = { currentEnvironment: PropTypes.string, emitEvent: PropTypes.func, layouts: PropTypes.object.isRequired, slug: PropTypes.string.isRequired, plugins: PropTypes.object, }; export { EditView }; export default memo(EditView);