Merge pull request #8043 from strapi/fix/ctb-redirection

Fixes #8040
This commit is contained in:
cyril lopez 2020-09-25 12:31:03 +02:00 committed by GitHub
commit 5bae91462e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import React, { memo, useEffect, useReducer, useState, useRef } from 'react'; import React, { memo, useEffect, useMemo, useReducer, useState, useRef } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { get, groupBy, set, size } from 'lodash'; import { get, groupBy, set, size } from 'lodash';
import { import {
@ -382,16 +382,22 @@ const DataManagerProvider = ({ allIcons, children }) => {
}); });
}; };
const shouldRedirect = () => { const shouldRedirect = useMemo(() => {
const dataSet = isInContentTypeView ? contentTypes : components; const dataSet = isInContentTypeView ? contentTypes : components;
return !Object.keys(dataSet).includes(currentUid) && !isLoading; return !Object.keys(dataSet).includes(currentUid) && !isLoading;
}; }, [components, contentTypes, currentUid, isInContentTypeView, isLoading]);
if (shouldRedirect()) { const redirectEndpoint = useMemo(() => {
const firstCTUid = Object.keys(contentTypes).sort()[0]; const allowedEndpoints = Object.keys(contentTypes)
.filter(uid => get(contentTypes, [uid, 'schema', 'editable'], true))
.sort();
return <Redirect to={`/plugins/${pluginId}/content-types/${firstCTUid}`} />; return get(allowedEndpoints, '0', '');
}, [contentTypes]);
if (shouldRedirect) {
return <Redirect to={`/plugins/${pluginId}/content-types/${redirectEndpoint}`} />;
} }
const submitData = async additionalContentTypeData => { const submitData = async additionalContentTypeData => {