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 } from 'react-router-dom';
|
2019-09-13 17:05:21 +02:00
|
|
|
import {
|
|
|
|
|
LoadingIndicatorPage,
|
|
|
|
|
getQueryParameters,
|
|
|
|
|
useGlobalContext,
|
|
|
|
|
} 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';
|
2019-07-17 15:35:19 +02:00
|
|
|
import DragLayer from '../../components/DragLayer';
|
2019-11-05 15:42:54 +01:00
|
|
|
import {
|
|
|
|
|
deleteLayout,
|
|
|
|
|
deleteLayouts,
|
|
|
|
|
getData,
|
|
|
|
|
getLayout,
|
|
|
|
|
resetProps,
|
|
|
|
|
} from './actions';
|
2019-07-04 11:32:51 +02:00
|
|
|
import reducer from './reducer';
|
|
|
|
|
import saga from './saga';
|
|
|
|
|
import makeSelectMain from './selectors';
|
|
|
|
|
|
2020-01-21 16:03:31 +01:00
|
|
|
const EditSettingsView = lazy(() => import('../EditSettingsView'));
|
|
|
|
|
const RecursivePath = lazy(() => import('../RecursivePath'));
|
|
|
|
|
|
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,
|
2019-07-25 09:36:47 +02:00
|
|
|
getData,
|
2019-07-11 16:53:00 +02:00
|
|
|
getLayout,
|
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,
|
2019-07-24 11:10:29 +02:00
|
|
|
location: { pathname, search },
|
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-07-04 11:32:51 +02:00
|
|
|
strapi.useInjectReducer({ key: 'main', reducer, pluginId });
|
|
|
|
|
strapi.useInjectSaga({ key: 'main', saga, pluginId });
|
2019-09-13 17:05:21 +02:00
|
|
|
const { emitEvent } = useGlobalContext();
|
2019-07-08 20:27:38 +02:00
|
|
|
const slug = pathname.split('/')[3];
|
2019-07-24 11:10:29 +02:00
|
|
|
const source = getQueryParameters(search, 'source');
|
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
|
|
|
|
|
|
|
|
getDataRef.current = getData;
|
|
|
|
|
getLayoutRef.current = getLayout;
|
2019-07-30 09:08:10 +02:00
|
|
|
resetPropsRef.current = resetProps;
|
2019-07-24 11:10:29 +02:00
|
|
|
|
2019-07-08 20:27:38 +02:00
|
|
|
const shouldShowLoader =
|
2019-10-24 17:08:52 +02:00
|
|
|
!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]);
|
2019-07-08 20:27:38 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (shouldShowLoader) {
|
2019-07-25 09:36:47 +02:00
|
|
|
getLayoutRef.current(slug, source);
|
2019-07-08 20:27:38 +02:00
|
|
|
}
|
2019-07-25 09:36:47 +02:00
|
|
|
}, [getLayoutRef, shouldShowLoader, slug, source]);
|
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}
|
|
|
|
|
componentsAndModelsMainPossibleMainFields={
|
|
|
|
|
componentsAndModelsMainPossibleMainFields
|
2019-07-25 09:36:47 +02:00
|
|
|
}
|
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 = [
|
2019-10-15 15:12:53 +02:00
|
|
|
{
|
2019-10-28 11:08:26 +01:00
|
|
|
path: 'ctm-configurations/edit-settings/:type/:componentSlug',
|
2019-10-17 16:32:39 +02:00
|
|
|
comp: EditSettingsView,
|
|
|
|
|
},
|
2019-10-24 17:08:52 +02:00
|
|
|
{ path: ':slug', comp: RecursivePath },
|
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 />}>
|
|
|
|
|
<Switch>{routes}</Switch>
|
|
|
|
|
</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,
|
2019-07-25 09:36:47 +02:00
|
|
|
getData: PropTypes.func.isRequired,
|
2019-07-08 20:27:38 +02:00
|
|
|
getLayout: 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,
|
2019-07-25 09:36:47 +02:00
|
|
|
getData,
|
2019-07-08 20:27:38 +02:00
|
|
|
getLayout,
|
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-10-24 17:08:52 +02:00
|
|
|
export default compose(withConnect)(Main);
|