2019-10-24 17:08:52 +02:00
|
|
|
import React, { Suspense, lazy, useEffect, useRef } from 'react';
|
2019-07-04 11:32:51 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators, compose } from 'redux';
|
2020-02-19 16:52:46 +01:00
|
|
|
import { Switch, Route, useRouteMatch } from 'react-router-dom';
|
2020-06-10 15:52:10 +02:00
|
|
|
import {
|
|
|
|
|
LoadingIndicatorPage,
|
|
|
|
|
useGlobalContext,
|
|
|
|
|
request,
|
2020-06-11 09:38:34 +02:00
|
|
|
CheckPagePermissions,
|
2020-06-10 15:52:10 +02:00
|
|
|
} from 'strapi-helper-plugin';
|
2019-07-17 15:35:19 +02:00
|
|
|
import { DndProvider } from 'react-dnd';
|
|
|
|
|
import HTML5Backend from 'react-dnd-html5-backend';
|
2019-07-04 11:32:51 +02:00
|
|
|
import pluginId from '../../pluginId';
|
2020-06-10 15:52:10 +02:00
|
|
|
import pluginPermissions from '../../permissions';
|
2019-07-17 15:35:19 +02:00
|
|
|
import DragLayer from '../../components/DragLayer';
|
2020-02-10 15:27:49 +01:00
|
|
|
import getRequestUrl from '../../utils/getRequestUrl';
|
|
|
|
|
import createPossibleMainFieldsForModelsAndComponents from './utils/createPossibleMainFieldsForModelsAndComponents';
|
2019-11-05 15:42:54 +01:00
|
|
|
import {
|
|
|
|
|
deleteLayout,
|
|
|
|
|
deleteLayouts,
|
2020-02-10 15:27:49 +01:00
|
|
|
getDataSucceeded,
|
|
|
|
|
getLayoutSucceeded,
|
2019-11-05 15:42:54 +01:00
|
|
|
resetProps,
|
|
|
|
|
} from './actions';
|
2019-07-04 11:32:51 +02:00
|
|
|
import makeSelectMain from './selectors';
|
|
|
|
|
|
2020-01-21 16:03:31 +01:00
|
|
|
const EditSettingsView = lazy(() => import('../EditSettingsView'));
|
2020-02-19 16:52:46 +01:00
|
|
|
const CollectionTypeRecursivePath = lazy(() => import('../CollectionTypeRecursivePath'));
|
|
|
|
|
const SingleTypeRecursivePath = lazy(() => import('../SingleTypeRecursivePath'));
|
2020-01-21 16:03:31 +01:00
|
|
|
|
2019-07-11 16:53:00 +02:00
|
|
|
function Main({
|
2019-10-16 16:00:24 +02:00
|
|
|
deleteLayout,
|
2019-11-05 15:42:54 +01:00
|
|
|
deleteLayouts,
|
2020-02-10 15:27:49 +01:00
|
|
|
getDataSucceeded,
|
|
|
|
|
getLayoutSucceeded,
|
2019-10-28 11:08:26 +01:00
|
|
|
components,
|
|
|
|
|
componentsAndModelsMainPossibleMainFields,
|
2019-07-25 09:36:47 +02:00
|
|
|
isLoading,
|
2019-07-11 16:53:00 +02:00
|
|
|
layouts,
|
2020-02-10 15:27:49 +01:00
|
|
|
location: { pathname },
|
2019-09-09 17:12:01 +02:00
|
|
|
global: { currentEnvironment, plugins },
|
2019-07-25 09:36:47 +02:00
|
|
|
models,
|
2019-07-30 09:08:10 +02:00
|
|
|
resetProps,
|
2019-07-11 16:53:00 +02:00
|
|
|
}) {
|
2019-09-13 17:05:21 +02:00
|
|
|
const { emitEvent } = useGlobalContext();
|
2020-02-19 16:52:46 +01:00
|
|
|
const {
|
|
|
|
|
params: { slug },
|
|
|
|
|
} = useRouteMatch('/plugins/content-manager/:contentType/:slug');
|
2019-07-25 09:36:47 +02:00
|
|
|
const getDataRef = useRef();
|
|
|
|
|
const getLayoutRef = useRef();
|
2019-07-30 09:08:10 +02:00
|
|
|
const resetPropsRef = useRef();
|
2019-07-25 09:36:47 +02:00
|
|
|
|
2020-02-10 15:27:49 +01:00
|
|
|
getDataRef.current = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const [{ data: components }, { data: models }] = await Promise.all(
|
|
|
|
|
['components', 'content-types'].map(endPoint =>
|
|
|
|
|
request(getRequestUrl(endPoint), { method: 'GET' })
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
getDataSucceeded(components, models, {
|
|
|
|
|
...createPossibleMainFieldsForModelsAndComponents(components),
|
|
|
|
|
...createPossibleMainFieldsForModelsAndComponents(models),
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
2020-10-02 18:58:15 +02:00
|
|
|
strapi.notification.toggle({
|
|
|
|
|
type: 'warning',
|
|
|
|
|
message: { id: 'notification.error' },
|
|
|
|
|
});
|
2020-02-10 15:27:49 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getLayoutRef.current = async uid => {
|
|
|
|
|
try {
|
2020-02-19 16:52:46 +01:00
|
|
|
const { data: layout } = await request(getRequestUrl(`content-types/${uid}`), {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
});
|
2020-02-10 15:27:49 +01:00
|
|
|
|
|
|
|
|
getLayoutSucceeded(layout, uid);
|
|
|
|
|
} catch (err) {
|
2020-10-02 18:58:15 +02:00
|
|
|
strapi.notification.toggle({
|
|
|
|
|
type: 'warning',
|
|
|
|
|
message: { id: 'notification.error' },
|
|
|
|
|
});
|
2020-02-10 15:27:49 +01:00
|
|
|
}
|
|
|
|
|
};
|
2019-07-30 09:08:10 +02:00
|
|
|
resetPropsRef.current = resetProps;
|
2019-07-24 11:10:29 +02:00
|
|
|
|
2020-02-19 16:52:46 +01:00
|
|
|
const shouldShowLoader = !pathname.includes('ctm-configurations/') && layouts[slug] === undefined;
|
2019-07-04 11:32:51 +02:00
|
|
|
|
2019-07-25 09:36:47 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
getDataRef.current();
|
2019-07-30 09:08:10 +02:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
resetPropsRef.current();
|
|
|
|
|
};
|
2019-07-25 09:36:47 +02:00
|
|
|
}, [getDataRef]);
|
2020-02-10 15:27:49 +01:00
|
|
|
|
2019-07-08 20:27:38 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (shouldShowLoader) {
|
2020-02-10 15:27:49 +01:00
|
|
|
getLayoutRef.current(slug);
|
2019-07-08 20:27:38 +02:00
|
|
|
}
|
2020-02-10 15:27:49 +01:00
|
|
|
}, [getLayoutRef, shouldShowLoader, slug]);
|
2019-07-08 20:27:38 +02:00
|
|
|
|
2019-07-25 09:36:47 +02:00
|
|
|
if (isLoading || shouldShowLoader) {
|
2019-07-04 11:32:51 +02:00
|
|
|
return <LoadingIndicatorPage />;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 10:54:34 +02:00
|
|
|
const renderRoute = (props, Component) => (
|
2019-07-11 16:53:00 +02:00
|
|
|
<Component
|
|
|
|
|
currentEnvironment={currentEnvironment}
|
2019-10-16 16:00:24 +02:00
|
|
|
deleteLayout={deleteLayout}
|
2019-11-05 15:42:54 +01:00
|
|
|
deleteLayouts={deleteLayouts}
|
2019-07-11 16:53:00 +02:00
|
|
|
emitEvent={emitEvent}
|
2019-10-28 11:08:26 +01:00
|
|
|
components={components}
|
2020-02-19 16:52:46 +01:00
|
|
|
componentsAndModelsMainPossibleMainFields={componentsAndModelsMainPossibleMainFields}
|
2019-07-11 16:53:00 +02:00
|
|
|
layouts={layouts}
|
2019-07-25 09:36:47 +02:00
|
|
|
models={models}
|
2019-07-11 16:53:00 +02:00
|
|
|
plugins={plugins}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
2019-07-04 13:08:31 +02:00
|
|
|
);
|
2019-07-11 11:35:18 +02:00
|
|
|
const routes = [
|
2020-02-17 10:01:56 +01:00
|
|
|
{ path: 'singleType/:slug', comp: SingleTypeRecursivePath },
|
|
|
|
|
{ path: 'collectionType/:slug', comp: CollectionTypeRecursivePath },
|
2019-07-11 11:35:18 +02:00
|
|
|
].map(({ path, comp }) => (
|
|
|
|
|
<Route
|
|
|
|
|
key={path}
|
|
|
|
|
path={`/plugins/${pluginId}/${path}`}
|
|
|
|
|
render={props => renderRoute(props, comp)}
|
|
|
|
|
/>
|
|
|
|
|
));
|
2019-07-04 13:08:31 +02:00
|
|
|
|
2019-07-17 15:35:19 +02:00
|
|
|
return (
|
|
|
|
|
<DndProvider backend={HTML5Backend}>
|
|
|
|
|
<DragLayer />
|
2019-10-17 16:32:39 +02:00
|
|
|
<Suspense fallback={<LoadingIndicatorPage />}>
|
2020-06-10 15:52:10 +02:00
|
|
|
<Switch>
|
|
|
|
|
<Route
|
|
|
|
|
path={`/plugins/${pluginId}/ctm-configurations/edit-settings/:type/:componentSlug`}
|
|
|
|
|
render={routeProps => (
|
2020-06-11 09:38:34 +02:00
|
|
|
<CheckPagePermissions permissions={pluginPermissions.componentsConfigurations}>
|
2020-06-10 15:52:10 +02:00
|
|
|
<EditSettingsView
|
|
|
|
|
currentEnvironment={currentEnvironment}
|
|
|
|
|
deleteLayout={deleteLayout}
|
|
|
|
|
deleteLayouts={deleteLayouts}
|
|
|
|
|
emitEvent={emitEvent}
|
|
|
|
|
components={components}
|
|
|
|
|
componentsAndModelsMainPossibleMainFields={
|
|
|
|
|
componentsAndModelsMainPossibleMainFields
|
|
|
|
|
}
|
|
|
|
|
layouts={layouts}
|
|
|
|
|
models={models}
|
|
|
|
|
plugins={plugins}
|
|
|
|
|
{...routeProps}
|
|
|
|
|
/>
|
2020-06-11 09:38:34 +02:00
|
|
|
</CheckPagePermissions>
|
2020-06-10 15:52:10 +02:00
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
{routes}
|
|
|
|
|
</Switch>
|
2019-10-17 16:32:39 +02:00
|
|
|
</Suspense>
|
2019-07-17 15:35:19 +02:00
|
|
|
</DndProvider>
|
|
|
|
|
);
|
2019-07-04 11:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Main.propTypes = {
|
2019-10-16 16:00:24 +02:00
|
|
|
deleteLayout: PropTypes.func.isRequired,
|
2019-11-05 15:42:54 +01:00
|
|
|
deleteLayouts: PropTypes.func.isRequired,
|
2020-02-10 15:27:49 +01:00
|
|
|
getDataSucceeded: PropTypes.func.isRequired,
|
|
|
|
|
getLayoutSucceeded: PropTypes.func.isRequired,
|
2019-07-11 16:53:00 +02:00
|
|
|
global: PropTypes.shape({
|
2019-09-09 17:12:01 +02:00
|
|
|
currentEnvironment: PropTypes.string.isRequired,
|
2019-07-11 16:53:00 +02:00
|
|
|
plugins: PropTypes.object,
|
2020-01-21 16:03:31 +01:00
|
|
|
}).isRequired,
|
2019-10-28 11:08:26 +01:00
|
|
|
components: PropTypes.array.isRequired,
|
|
|
|
|
componentsAndModelsMainPossibleMainFields: PropTypes.object.isRequired,
|
2020-01-21 16:03:31 +01:00
|
|
|
isLoading: PropTypes.bool.isRequired,
|
2019-07-08 17:01:12 +02:00
|
|
|
layouts: PropTypes.object.isRequired,
|
2019-07-08 20:27:38 +02:00
|
|
|
location: PropTypes.shape({
|
|
|
|
|
pathname: PropTypes.string.isRequired,
|
2019-07-24 11:10:29 +02:00
|
|
|
search: PropTypes.string,
|
2020-01-21 16:03:31 +01:00
|
|
|
}).isRequired,
|
2019-07-25 09:36:47 +02:00
|
|
|
models: PropTypes.array.isRequired,
|
2019-07-30 09:08:10 +02:00
|
|
|
resetProps: PropTypes.func.isRequired,
|
2019-07-04 11:32:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = makeSelectMain();
|
|
|
|
|
|
|
|
|
|
export function mapDispatchToProps(dispatch) {
|
2019-07-08 20:27:38 +02:00
|
|
|
return bindActionCreators(
|
|
|
|
|
{
|
2019-10-16 16:00:24 +02:00
|
|
|
deleteLayout,
|
2019-11-05 15:42:54 +01:00
|
|
|
deleteLayouts,
|
2020-02-10 15:27:49 +01:00
|
|
|
getDataSucceeded,
|
|
|
|
|
getLayoutSucceeded,
|
2019-07-30 09:08:10 +02:00
|
|
|
resetProps,
|
2019-07-08 20:27:38 +02:00
|
|
|
},
|
|
|
|
|
dispatch
|
|
|
|
|
);
|
2019-07-04 11:32:51 +02:00
|
|
|
}
|
2020-02-10 15:27:49 +01:00
|
|
|
const withConnect = connect(mapStateToProps, mapDispatchToProps);
|
2019-07-04 11:32:51 +02:00
|
|
|
|
2019-10-24 17:08:52 +02:00
|
|
|
export default compose(withConnect)(Main);
|