143 lines
3.7 KiB
JavaScript
Raw Normal View History

import React, { memo, 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-07-24 11:10:29 +02:00
import { LoadingIndicatorPage, getQueryParameters } 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 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';
import { getData, 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,
getData,
2019-07-11 16:53:00 +02:00
getLayout,
groups,
groupsAndModelsMainPossibleMainFields,
isLoading,
2019-07-11 16:53:00 +02:00
layouts,
2019-07-24 11:10:29 +02:00
location: { pathname, search },
2019-07-11 16:53:00 +02:00
global: { plugins },
models,
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-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');
const getDataRef = useRef();
const getLayoutRef = useRef();
getDataRef.current = getData;
getLayoutRef.current = getLayout;
2019-07-24 11:10:29 +02:00
2019-07-08 20:27:38 +02:00
const shouldShowLoader =
slug !== 'ctm-configurations' && layouts[slug] === undefined;
2019-07-04 11:32:51 +02:00
useEffect(() => {
getDataRef.current();
}, [getDataRef]);
2019-07-08 20:27:38 +02:00
useEffect(() => {
if (shouldShowLoader) {
getLayoutRef.current(slug, source);
2019-07-08 20:27:38 +02:00
}
}, [getLayoutRef, shouldShowLoader, slug, source]);
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}
emitEvent={emitEvent}
groups={groups}
groupsAndModelsMainPossibleMainFields={
groupsAndModelsMainPossibleMainFields
}
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: '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
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,
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({
plugins: PropTypes.object,
}),
groups: PropTypes.array.isRequired,
groupsAndModelsMainPossibleMainFields: PropTypes.object.isRequired,
isLoading: PropTypes.bool,
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
}),
models: PropTypes.array.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(
{
getData,
2019-07-08 20:27:38 +02:00
getLayout,
},
dispatch
);
2019-07-04 11:32:51 +02:00
}
const withConnect = connect(
mapStateToProps,
mapDispatchToProps
);
export default compose(
withConnect,
memo
)(Main);