Fix loading state and ctb setup

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-02-24 08:53:34 +01:00
parent 761675253e
commit 7ab7ebda51

View File

@ -7,12 +7,7 @@ import {
useGlobalContext, useGlobalContext,
PopUpWarning, PopUpWarning,
} from 'strapi-helper-plugin'; } from 'strapi-helper-plugin';
import { import { useHistory, useLocation, useRouteMatch, Redirect } from 'react-router-dom';
useHistory,
useLocation,
useRouteMatch,
Redirect,
} from 'react-router-dom';
import DataManagerContext from '../../contexts/DataManagerContext'; import DataManagerContext from '../../contexts/DataManagerContext';
import getTrad from '../../utils/getTrad'; import getTrad from '../../utils/getTrad';
import makeUnique from '../../utils/makeUnique'; import makeUnique from '../../utils/makeUnique';
@ -55,21 +50,17 @@ const DataManagerProvider = ({ allIcons, children }) => {
} = reducerState.toJS(); } = reducerState.toJS();
const { pathname } = useLocation(); const { pathname } = useLocation();
const { push } = useHistory(); const { push } = useHistory();
const contentTypeMatch = useRouteMatch( const contentTypeMatch = useRouteMatch(`/plugins/${pluginId}/content-types/:uid`);
`/plugins/${pluginId}/content-types/:uid`
);
const componentMatch = useRouteMatch( const componentMatch = useRouteMatch(
`/plugins/${pluginId}/component-categories/:categoryUid/:componentUid` `/plugins/${pluginId}/component-categories/:categoryUid/:componentUid`
); );
const formatMessageRef = useRef(); const formatMessageRef = useRef();
formatMessageRef.current = formatMessage; formatMessageRef.current = formatMessage;
const isInDevelopmentMode = const isInDevelopmentMode = currentEnvironment === 'development' && autoReload;
currentEnvironment === 'development' && autoReload;
const isInContentTypeView = contentTypeMatch !== null; const isInContentTypeView = contentTypeMatch !== null;
const firstKeyToMainSchema = isInContentTypeView const firstKeyToMainSchema = isInContentTypeView ? 'contentType' : 'component';
? 'contentType'
: 'component';
const currentUid = isInContentTypeView const currentUid = isInContentTypeView
? get(contentTypeMatch, 'params.uid', null) ? get(contentTypeMatch, 'params.uid', null)
: get(componentMatch, 'params.componentUid', null); : get(componentMatch, 'params.componentUid', null);
@ -80,10 +71,7 @@ const DataManagerProvider = ({ allIcons, children }) => {
getDataRef.current = async () => { getDataRef.current = async () => {
try { try {
const [ const [{ data: componentsArray }, { data: contentTypesArray }] = await Promise.all(
{ data: componentsArray },
{ data: contentTypesArray },
] = await Promise.all(
['components', 'content-types'].map(endPoint => { ['components', 'content-types'].map(endPoint => {
return request(`/${pluginId}/${endPoint}`, { return request(`/${pluginId}/${endPoint}`, {
method: 'GET', method: 'GET',
@ -118,11 +106,11 @@ const DataManagerProvider = ({ allIcons, children }) => {
useEffect(() => { useEffect(() => {
// We need to set the modifiedData after the data has been retrieved // We need to set the modifiedData after the data has been retrieved
// and also on pathname change // and also on pathname change
if (!isLoading) { if (!isLoading && currentUid) {
setModifiedData(); setModifiedData();
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoading, pathname]); }, [isLoading, pathname, currentUid]);
useEffect(() => { useEffect(() => {
if (currentEnvironment === 'development' && !autoReload) { if (currentEnvironment === 'development' && !autoReload) {
@ -135,8 +123,7 @@ const DataManagerProvider = ({ allIcons, children }) => {
}, [autoReload, currentEnvironment]); }, [autoReload, currentEnvironment]);
const didModifiedComponents = const didModifiedComponents =
getCreatedAndModifiedComponents(modifiedData.components || {}, components) getCreatedAndModifiedComponents(modifiedData.components || {}, components).length > 0;
.length > 0;
const addAttribute = ( const addAttribute = (
attributeToSet, attributeToSet,
@ -158,10 +145,7 @@ const DataManagerProvider = ({ allIcons, children }) => {
}); });
}; };
const addCreatedComponentToDynamicZone = ( const addCreatedComponentToDynamicZone = (dynamicZoneTarget, componentsToAdd) => {
dynamicZoneTarget,
componentsToAdd
) => {
dispatch({ dispatch({
type: 'ADD_CREATED_COMPONENT_TO_DYNAMIC_ZONE', type: 'ADD_CREATED_COMPONENT_TO_DYNAMIC_ZONE',
dynamicZoneTarget, dynamicZoneTarget,
@ -181,10 +165,7 @@ const DataManagerProvider = ({ allIcons, children }) => {
componentCategory, componentCategory,
shouldAddComponentToData = false shouldAddComponentToData = false
) => { ) => {
const type = const type = schemaType === 'contentType' ? 'CREATE_SCHEMA' : 'CREATE_COMPONENT_SCHEMA';
schemaType === 'contentType'
? 'CREATE_SCHEMA'
: 'CREATE_COMPONENT_SCHEMA';
dispatch({ dispatch({
type, type,
@ -204,15 +185,9 @@ const DataManagerProvider = ({ allIcons, children }) => {
}); });
}; };
const removeAttribute = ( const removeAttribute = (mainDataKey, attributeToRemoveName, componentUid = '') => {
mainDataKey,
attributeToRemoveName,
componentUid = ''
) => {
const type = const type =
mainDataKey === 'components' mainDataKey === 'components' ? 'REMOVE_FIELD_FROM_DISPLAYED_COMPONENT' : 'REMOVE_FIELD';
? 'REMOVE_FIELD_FROM_DISPLAYED_COMPONENT'
: 'REMOVE_FIELD';
if (mainDataKey === 'contentType') { if (mainDataKey === 'contentType') {
emitEvent('willDeleteFieldOfContentType'); emitEvent('willDeleteFieldOfContentType');
@ -253,17 +228,11 @@ const DataManagerProvider = ({ allIcons, children }) => {
const deleteData = async () => { const deleteData = async () => {
try { try {
const requestURL = `/${pluginId}/${endPoint}/${currentUid}`; const requestURL = `/${pluginId}/${endPoint}/${currentUid}`;
const isTemporary = get( const isTemporary = get(modifiedData, [firstKeyToMainSchema, 'isTemporary'], false);
modifiedData,
[firstKeyToMainSchema, 'isTemporary'],
false
);
const userConfirm = window.confirm( const userConfirm = window.confirm(
formatMessage({ formatMessage({
id: getTrad( id: getTrad(
`popUpWarning.bodyMessage.${ `popUpWarning.bodyMessage.${isInContentTypeView ? 'contentType' : 'component'}.delete`
isInContentTypeView ? 'contentType' : 'component'
}.delete`
), ),
}) })
); );
@ -338,9 +307,7 @@ const DataManagerProvider = ({ allIcons, children }) => {
const getAllNestedComponents = () => { const getAllNestedComponents = () => {
const appNestedCompo = retrieveNestedComponents(components); const appNestedCompo = retrieveNestedComponents(components);
const editingDataNestedCompos = retrieveNestedComponents( const editingDataNestedCompos = retrieveNestedComponents(modifiedData.components || {});
modifiedData.components || {}
);
return makeUnique([...editingDataNestedCompos, ...appNestedCompo]); return makeUnique([...editingDataNestedCompos, ...appNestedCompo]);
}; };
@ -370,10 +337,7 @@ const DataManagerProvider = ({ allIcons, children }) => {
isInContentTypeView isInContentTypeView
); );
const dataShape = orderAllDataAttributesWithImmutable( const dataShape = orderAllDataAttributesWithImmutable(newSchemaToSet, isInContentTypeView);
newSchemaToSet,
isInContentTypeView
);
// This prevents from losing the created content type or component when clicking on the link from the left menu // This prevents from losing the created content type or component when clicking on the link from the left menu
const hasJustCreatedSchema = const hasJustCreatedSchema =
@ -401,11 +365,7 @@ const DataManagerProvider = ({ allIcons, children }) => {
const submitData = async additionalContentTypeData => { const submitData = async additionalContentTypeData => {
try { try {
const isCreating = get( const isCreating = get(modifiedData, [firstKeyToMainSchema, 'isTemporary'], false);
modifiedData,
[firstKeyToMainSchema, 'isTemporary'],
false
);
const body = { const body = {
components: getComponentsToPost( components: getComponentsToPost(
modifiedData.components, modifiedData.components,
@ -505,14 +465,11 @@ const DataManagerProvider = ({ allIcons, children }) => {
value={{ value={{
addAttribute, addAttribute,
addCreatedComponentToDynamicZone, addCreatedComponentToDynamicZone,
allComponentsCategories: retrieveSpecificInfoFromComponents( allComponentsCategories: retrieveSpecificInfoFromComponents(components, ['category']),
components, allComponentsIconAlreadyTaken: retrieveSpecificInfoFromComponents(components, [
['category'] 'schema',
), 'icon',
allComponentsIconAlreadyTaken: retrieveSpecificInfoFromComponents( ]),
components,
['schema', 'icon']
),
allIcons, allIcons,
changeDynamicZoneComponents, changeDynamicZoneComponents,
components, components,