97 lines
2.5 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-04 11:32:51 +02:00
return (
<Switch>
2019-07-04 15:56:42 +02:00
<Route
2019-07-08 17:01:12 +02:00
path={`/plugins/${pluginId}/ctm-configurations/models/:name/:settingType`}
2019-07-05 14:59:31 +02:00
render={props => renderRoute(props, SettingViewModel)}
2019-07-04 18:28:57 +02:00
/>
<Route
2019-07-08 17:01:12 +02:00
path={`/plugins/${pluginId}/ctm-configurations/groups/:name`}
2019-07-04 18:28:57 +02:00
component={SettingViewGroup}
2019-07-04 15:56:42 +02:00
/>
2019-07-04 11:32:51 +02:00
<Route
2019-07-08 17:01:12 +02:00
path={`/plugins/${pluginId}/ctm-configurations/:type`}
2019-07-05 10:54:34 +02:00
render={props => renderRoute(props, SettingsView)}
2019-07-04 11:32:51 +02:00
/>
2019-07-10 09:31:26 +02:00
<Route
path={`/plugins/${pluginId}/:slug/:id`}
render={props => renderRoute(props, EditView)}
/>
2019-07-08 17:01:12 +02:00
<Route
path={`/plugins/${pluginId}/:slug`}
render={props => renderRoute(props, ListView)}
/>
2019-07-04 11:32:51 +02:00
</Switch>
);
}
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);