206 lines
6.0 KiB
JavaScript
Raw Normal View History

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';
import { Switch, Route, useRouteMatch } from 'react-router-dom';
import {
LoadingIndicatorPage,
useGlobalContext,
request,
CheckPagePermissions,
} from 'strapi-helper-plugin';
import { DndProvider } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
2019-07-04 11:32:51 +02:00
import pluginId from '../../pluginId';
import pluginPermissions from '../../permissions';
import DragLayer from '../../components/DragLayer';
import getRequestUrl from '../../utils/getRequestUrl';
import createPossibleMainFieldsForModelsAndComponents from './utils/createPossibleMainFieldsForModelsAndComponents';
import {
deleteLayout,
deleteLayouts,
getDataSucceeded,
getLayoutSucceeded,
resetProps,
} from './actions';
2019-07-04 11:32:51 +02:00
import makeSelectMain from './selectors';
const EditSettingsView = lazy(() => import('../EditSettingsView'));
const CollectionTypeRecursivePath = lazy(() => import('../CollectionTypeRecursivePath'));
const SingleTypeRecursivePath = lazy(() => import('../SingleTypeRecursivePath'));
2019-07-11 16:53:00 +02:00
function Main({
2019-10-16 16:00:24 +02:00
deleteLayout,
deleteLayouts,
getDataSucceeded,
getLayoutSucceeded,
2019-10-28 11:08:26 +01:00
components,
componentsAndModelsMainPossibleMainFields,
isLoading,
2019-07-11 16:53:00 +02:00
layouts,
location: { pathname },
global: { currentEnvironment, plugins },
models,
2019-07-30 09:08:10 +02:00
resetProps,
2019-07-11 16:53:00 +02:00
}) {
const { emitEvent } = useGlobalContext();
const {
params: { slug },
} = useRouteMatch('/plugins/content-manager/:contentType/:slug');
const getDataRef = useRef();
const getLayoutRef = useRef();
2019-07-30 09:08:10 +02:00
const resetPropsRef = useRef();
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) {
strapi.notification.toggle({
type: 'warning',
message: { id: 'notification.error' },
});
}
};
getLayoutRef.current = async uid => {
try {
const { data: layout } = await request(getRequestUrl(`content-types/${uid}`), {
method: 'GET',
});
getLayoutSucceeded(layout, uid);
} catch (err) {
strapi.notification.toggle({
type: 'warning',
message: { id: 'notification.error' },
});
}
};
2019-07-30 09:08:10 +02:00
resetPropsRef.current = resetProps;
2019-07-24 11:10:29 +02:00
const shouldShowLoader = !pathname.includes('ctm-configurations/') && layouts[slug] === undefined;
2019-07-04 11:32:51 +02:00
useEffect(() => {
getDataRef.current();
2019-07-30 09:08:10 +02:00
return () => {
resetPropsRef.current();
};
}, [getDataRef]);
2019-07-08 20:27:38 +02:00
useEffect(() => {
if (shouldShowLoader) {
getLayoutRef.current(slug);
2019-07-08 20:27:38 +02:00
}
}, [getLayoutRef, shouldShowLoader, slug]);
2019-07-08 20:27:38 +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}
deleteLayouts={deleteLayouts}
2019-07-11 16:53:00 +02:00
emitEvent={emitEvent}
2019-10-28 11:08:26 +01:00
components={components}
componentsAndModelsMainPossibleMainFields={componentsAndModelsMainPossibleMainFields}
2019-07-11 16:53:00 +02:00
layouts={layouts}
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 = [
{ 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
return (
<DndProvider backend={HTML5Backend}>
<DragLayer />
2019-10-17 16:32:39 +02:00
<Suspense fallback={<LoadingIndicatorPage />}>
<Switch>
<Route
path={`/plugins/${pluginId}/ctm-configurations/edit-settings/:type/:componentSlug`}
render={routeProps => (
<CheckPagePermissions permissions={pluginPermissions.componentsConfigurations}>
<EditSettingsView
currentEnvironment={currentEnvironment}
deleteLayout={deleteLayout}
deleteLayouts={deleteLayouts}
emitEvent={emitEvent}
components={components}
componentsAndModelsMainPossibleMainFields={
componentsAndModelsMainPossibleMainFields
}
layouts={layouts}
models={models}
plugins={plugins}
{...routeProps}
/>
</CheckPagePermissions>
)}
/>
{routes}
</Switch>
2019-10-17 16:32:39 +02:00
</Suspense>
</DndProvider>
);
2019-07-04 11:32:51 +02:00
}
Main.propTypes = {
2019-10-16 16:00:24 +02:00
deleteLayout: PropTypes.func.isRequired,
deleteLayouts: PropTypes.func.isRequired,
getDataSucceeded: PropTypes.func.isRequired,
getLayoutSucceeded: PropTypes.func.isRequired,
2019-07-11 16:53:00 +02:00
global: PropTypes.shape({
currentEnvironment: PropTypes.string.isRequired,
2019-07-11 16:53:00 +02:00
plugins: PropTypes.object,
}).isRequired,
2019-10-28 11:08:26 +01:00
components: PropTypes.array.isRequired,
componentsAndModelsMainPossibleMainFields: PropTypes.object.isRequired,
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,
}).isRequired,
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,
deleteLayouts,
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
}
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);