90 lines
2.3 KiB
JavaScript
Raw Normal View History

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';
import { LoadingIndicatorPage } from 'strapi-helper-plugin';
import pluginId from '../../pluginId';
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-08 20:27:38 +02:00
function Main({ emitEvent, getLayout, layouts, location: { pathname } }) {
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];
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) {
getLayout(slug);
}
}, [getLayout, shouldShowLoader, slug]);
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-08 17:01:12 +02:00
<Component emitEvent={emitEvent} layouts={layouts} {...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-11 11:35:18 +02:00
return <Switch>{routes}</Switch>;
2019-07-04 11:32:51 +02:00
}
Main.propTypes = {
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-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-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);