persist routing and active link

This commit is contained in:
cyril lopez 2017-07-17 18:40:11 +02:00
parent 74e90d271b
commit d4e57dd40f
5 changed files with 36 additions and 24 deletions

View File

@ -9,14 +9,14 @@ import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { bindActionCreators } from 'redux';
import { isEmpty, map } from 'lodash';
import { map } from 'lodash';
import { pluginId } from 'app';
import PluginLeftMenu from 'components/PluginLeftMenu';
import { define } from 'i18n';
import messages from '../../translations/en.json';
import { menuFetch, environmentsFetch } from './actions';
import { makeSelectSections, makeSelectEnvironments } from './selectors';
import { makeSelectSections, makeSelectEnvironments, makeSelectLoading } from './selectors';
import styles from './styles.scss';
define(map(messages, (message, id) => ({
id,
@ -30,14 +30,10 @@ class App extends React.Component {
this.props.environmentsFetch();
}
componentWillReceiveProps(nextProps) {
// redirect the user to the first general section
if (!this.props.params.slug && !isEmpty(nextProps.sections)) {
this.props.history.push(`${this.props.location.pathname}/${nextProps.sections[0].items[0].slug}`);
}
}
render() {
if (this.props.loading) {
return <div />;
}
// Assign plugin component to children
const content = React.Children.map(this.props.children, child =>
React.cloneElement(child, {
@ -70,10 +66,8 @@ App.propTypes = {
environments: React.PropTypes.array,
environmentsFetch: React.PropTypes.func,
exposedComponents: React.PropTypes.object.isRequired,
history: React.PropTypes.object,
location: React.PropTypes.object,
loading: React.PropTypes.bool,
menuFetch: React.PropTypes.func,
params: React.PropTypes.object,
sections: React.PropTypes.array.isRequired,
};
@ -90,6 +84,7 @@ export function mapDispatchToProps(dispatch) {
const mapStateToProps = createStructuredSelector({
sections: makeSelectSections(),
environments: makeSelectEnvironments(),
loading: makeSelectLoading(),
});
// Wrap the component to inject dispatch and state into it

View File

@ -14,12 +14,13 @@ import {
const initialState = fromJS({
sections: List(), // eslint-disable-line new-cap
environments: List(),
loading: true,
});
function appReducer(state = initialState, action) {
switch (action.type) {
case MENU_FETCH_SUCCEEDED:
return state.set('sections', List(action.menu.sections));
return state.set('sections', List(action.menu.sections)).set('loading', false);
case ENVIRONMENTS_FETCH_SUCCEEDED:
return state
.set('environments', List(action.environments.environments));

View File

@ -32,5 +32,10 @@ const makeSelectEnvironments = () => createSelector(
(globalSate) => globalSate.get('environments').toJS(),
);
export { selectLocationState, makeSelectSections, makeSelectEnvironments };
const makeSelectLoading = () => createSelector(
selectGlobalDomain(),
(globalSate) => globalSate.get('loading'),
);
export { selectLocationState, makeSelectSections, makeSelectEnvironments, makeSelectLoading };
export default selectGlobalDomain;

View File

@ -7,7 +7,10 @@
import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { createStructuredSelector } from 'reselect';
import Helmet from 'react-helmet';
import { router } from 'app';
import { makeSelectSections } from 'containers/App/selectors';
import selectHome from './selectors';
import { configFetch } from './actions'
import styles from './styles.scss';
@ -18,23 +21,28 @@ export class Home extends React.Component { // eslint-disable-line react/prefer-
if (this.props.params.slug) {
const apiUrl = this.props.params.env ? `${this.props.params.slug}/${this.props.params.env}` : this.props.params.slug;
this.props.configFetch(apiUrl);
} else {
router.push(`/plugins/settings-manager/${this.props.sections[0].items[0].slug}`);
}
}
componentWillReceiveProps(nextProps) {
// check if params slug updated
if (this.props.params.slug !== nextProps.params.slug && nextProps.params.slug) {
// get data from api if params slug updated
const apiUrl = nextProps.params.env ? `${nextProps.params.slug}/${nextProps.params.env}` : nextProps.params.slug;
this.props.configFetch(apiUrl);
} else if (this.props.params.env !== nextProps.params.env) {
console.log('-------');
if (this.props.params.slug !== nextProps.params.slug) {
if (nextProps.params.slug) {
// get data from api if params slug updated
const apiUrl = nextProps.params.env ? `${nextProps.params.slug}/${nextProps.params.env}` : nextProps.params.slug;
this.props.configFetch(apiUrl);
} else {
// redirect user if no params slug provided
router.push(`/plugins/settings-manager/${this.props.sections[0].items[0].slug}`);
}
} else if (this.props.params.env !== nextProps.params.env && nextProps.params.env) {
// get data if params env updated
this.props.configFetch(`${this.props.params.slug}/${nextProps.params.env}`);
}
}
render() {
@ -51,7 +59,10 @@ export class Home extends React.Component { // eslint-disable-line react/prefer-
}
}
const mapStateToProps = selectHome();
const mapStateToProps = createStructuredSelector({
home: selectHome(),
sections: makeSelectSections(),
})
function mapDispatchToProps(dispatch) {
return bindActionCreators(
@ -65,6 +76,7 @@ function mapDispatchToProps(dispatch) {
Home.propTypes = {
configFetch: React.PropTypes.func.isRequired,
params: React.PropTypes.object.isRequired,
sections: React.PropTypes.array,
};
export default connect(mapStateToProps, mapDispatchToProps)(Home);

View File

@ -1,5 +1,4 @@
{
"(/:slug(/:env))": {
"name": "home",
"container": "Home"