2019-07-08 20:27:38 +02:00
|
|
|
import React, { memo, useEffect } 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-07-24 11:10:29 +02:00
|
|
|
import { LoadingIndicatorPage, getQueryParameters } 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-07-10 09:31:26 +02:00
|
|
|
import EditView from '../EditView';
|
2019-07-08 17:01:12 +02:00
|
|
|
import ListView from '../ListView';
|
2019-07-04 18:28:57 +02:00
|
|
|
import SettingViewModel from '../SettingViewModel';
|
|
|
|
|
import SettingViewGroup from '../SettingViewGroup';
|
2019-07-04 11:32:51 +02:00
|
|
|
import SettingsView from '../SettingsView';
|
|
|
|
|
|
2019-07-08 20:27:38 +02:00
|
|
|
import { getLayout } from './actions';
|
2019-07-04 11:32:51 +02:00
|
|
|
import reducer from './reducer';
|
|
|
|
|
import saga from './saga';
|
|
|
|
|
import makeSelectMain from './selectors';
|
|
|
|
|
|
2019-07-11 16:53:00 +02:00
|
|
|
function Main({
|
|
|
|
|
admin: { currentEnvironment },
|
|
|
|
|
emitEvent,
|
|
|
|
|
getLayout,
|
|
|
|
|
layouts,
|
2019-07-24 11:10:29 +02:00
|
|
|
location: { pathname, search },
|
2019-07-11 16:53:00 +02:00
|
|
|
global: { plugins },
|
|
|
|
|
}) {
|
2019-07-04 11:32:51 +02:00
|
|
|
strapi.useInjectReducer({ key: 'main', reducer, pluginId });
|
|
|
|
|
strapi.useInjectSaga({ key: 'main', saga, pluginId });
|
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-08 20:27:38 +02:00
|
|
|
const shouldShowLoader =
|
|
|
|
|
slug !== 'ctm-configurations' && layouts[slug] === undefined;
|
2019-07-04 11:32:51 +02:00
|
|
|
|
2019-07-08 20:27:38 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (shouldShowLoader) {
|
2019-07-24 11:10:29 +02:00
|
|
|
getLayout(slug, source);
|
2019-07-08 20:27:38 +02:00
|
|
|
}
|
2019-07-24 11:10:29 +02:00
|
|
|
}, [getLayout, shouldShowLoader, slug, source]);
|
2019-07-08 20:27:38 +02:00
|
|
|
|
|
|
|
|
if (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}
|
|
|
|
|
emitEvent={emitEvent}
|
|
|
|
|
layouts={layouts}
|
|
|
|
|
plugins={plugins}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
2019-07-04 13:08:31 +02:00
|
|
|
);
|
2019-07-11 11:35:18 +02:00
|
|
|
const routes = [
|
|
|
|
|
{
|
|
|
|
|
path: 'ctm-configurations/models/:name/:settingType',
|
|
|
|
|
comp: SettingViewModel,
|
|
|
|
|
},
|
|
|
|
|
{ path: 'ctm-configurations/groups/:name', comp: SettingViewGroup },
|
|
|
|
|
{ path: 'ctm-configurations/:type', comp: SettingsView },
|
|
|
|
|
{ path: ':slug/:id', comp: EditView },
|
|
|
|
|
{ path: ':slug', comp: ListView },
|
|
|
|
|
].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 />
|
|
|
|
|
<Switch>{routes}</Switch>
|
|
|
|
|
</DndProvider>
|
|
|
|
|
);
|
2019-07-04 11:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Main.propTypes = {
|
2019-07-11 16:53:00 +02:00
|
|
|
admin: PropTypes.shape({
|
|
|
|
|
currentEnvironment: PropTypes.string.isRequired,
|
|
|
|
|
}),
|
2019-07-04 13:08:31 +02:00
|
|
|
emitEvent: 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({
|
|
|
|
|
plugins: PropTypes.object,
|
|
|
|
|
}),
|
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,
|
2019-07-08 20:27:38 +02:00
|
|
|
}),
|
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(
|
|
|
|
|
{
|
|
|
|
|
getLayout,
|
|
|
|
|
},
|
|
|
|
|
dispatch
|
|
|
|
|
);
|
2019-07-04 11:32:51 +02:00
|
|
|
}
|
|
|
|
|
const withConnect = connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
|
withConnect,
|
|
|
|
|
memo
|
|
|
|
|
)(Main);
|