159 lines
4.9 KiB
JavaScript
Raw Normal View History

/*
*
* Home
*
*/
import React from 'react';
import { connect } from 'react-redux';
2017-07-13 16:55:59 +02:00
import { bindActionCreators } from 'redux';
2017-07-17 18:40:11 +02:00
import { createStructuredSelector } from 'reselect';
2017-07-19 09:51:23 +02:00
import { findKey, includes, get } from 'lodash';
2017-07-18 15:53:56 +02:00
import Helmet from 'react-helmet';
2017-07-17 18:40:11 +02:00
import { router } from 'app';
2017-07-18 14:07:48 +02:00
2017-07-17 19:19:54 +02:00
// design
import ContentHeader from 'components/ContentHeader';
2017-07-18 15:53:56 +02:00
import EditForm from 'components/EditForm';
2017-07-19 13:10:53 +02:00
import HeaderNav from 'components/HeaderNav';
2017-07-17 19:19:54 +02:00
2017-07-19 13:10:53 +02:00
import { makeSelectSections, makeSelectEnvironments } from 'containers/App/selectors';
import selectHome from './selectors';
import { configFetch, changeInput, cancelChanges, submitChanges } from './actions'
import styles from './styles.scss';
import config from './config.json';
export class Home extends React.Component { // eslint-disable-line react/prefer-stateless-function
2017-07-18 15:53:56 +02:00
constructor(props) {
super(props);
this.customComponents = config.customComponents;
this.components = {
editForm: EditForm,
2017-07-19 13:10:53 +02:00
defaultComponent: HeaderNav, // TODO change to default
2017-07-18 15:53:56 +02:00
};
}
componentDidMount() {
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);
2017-07-17 18:40:11 +02:00
} else {
2017-07-19 13:10:53 +02:00
router.push(`/plugins/settings-manager/${get(this.props.menuSections, ['0', 'items', '0', 'slug'])}`);
}
}
2017-07-13 16:55:59 +02:00
componentWillReceiveProps(nextProps) {
// check if params slug updated
2017-07-17 18:40:11 +02:00
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
2017-07-19 13:10:53 +02:00
router.push(`/plugins/settings-manager/${get(this.props.menuSections, ['0', 'items', '0', 'slug'])}`);
2017-07-17 18:40:11 +02:00
}
2017-07-19 13:10:53 +02:00
} else if (this.props.params.env !== nextProps.params.env && nextProps.params.env && this.props.params.env) {
// get data if params env updated
this.props.configFetch(`${this.props.params.slug}/${nextProps.params.env}`);
2017-07-13 16:55:59 +02:00
}
2017-07-17 18:40:11 +02:00
2017-07-13 16:55:59 +02:00
}
2017-07-18 17:52:39 +02:00
handleChange = ({ target }) => {
this.props.changeInput(target.name, target.value);
2017-07-18 17:52:39 +02:00
}
handleCancel = () => {
console.log('click');
this.props.cancelChanges();
}
handleSubmit = () => {
this.props.submitChanges();
}
2017-07-19 13:10:53 +02:00
renderComponent = () => {
// check if settingName (params.slug) has a custom view display
const specificComponent = findKey(this.customComponents, (value) => includes(value, this.props.params.slug)) ?
findKey(this.customComponents, (value) => includes(value, this.props.params.slug)) : 'defaultComponent';
// if custom view display render specificComponent
const Component = this.components[specificComponent];
return (
<Component
sections={this.props.home.configsDisplay.sections}
values={this.props.home.modifiedData}
handleChange={this.handleChange}
handleCancel={this.handleCancel}
handleSubmit={this.handleSubmit}
links={this.props.environments}
path={this.props.location.pathname}
/>
);
// TODO remove environments
}
render() {
2017-07-17 19:19:54 +02:00
if (this.props.home.loading) {
return <div />;
}
2017-07-18 15:53:56 +02:00
2017-07-19 13:10:53 +02:00
// check if settingName (params.slug) has a custom view display
2017-07-18 18:06:15 +02:00
const component = findKey(this.customComponents, (value) => includes(value, this.props.params.slug)) ?
findKey(this.customComponents, (value) => includes(value, this.props.params.slug)) : 'div'; // TODO change div to defaultComponent
2017-07-18 15:53:56 +02:00
// if custom view display render specificComponent
const Form = this.components[component];
return (
2017-07-18 14:07:48 +02:00
<div className={`${styles.home} col-md-9`}>
2017-07-06 16:18:43 +02:00
<Helmet
title="Home"
meta={[
{ name: 'description', content: 'Description of Home' },
]}
/>
2017-07-18 14:07:48 +02:00
<ContentHeader
name={this.props.home.configsDisplay.name}
description={this.props.home.configsDisplay.description}
/>
2017-07-19 13:10:53 +02:00
{this.renderComponent()}
</div>
);
}
}
2017-07-18 12:39:03 +02:00
2017-07-17 18:40:11 +02:00
const mapStateToProps = createStructuredSelector({
2017-07-19 13:10:53 +02:00
environments: makeSelectEnvironments(),
2017-07-17 18:40:11 +02:00
home: selectHome(),
2017-07-19 13:10:53 +02:00
menuSections: makeSelectSections(),
2017-07-17 18:40:11 +02:00
})
function mapDispatchToProps(dispatch) {
2017-07-13 16:55:59 +02:00
return bindActionCreators(
{
cancelChanges,
changeInput,
2017-07-13 16:55:59 +02:00
configFetch,
submitChanges,
2017-07-13 16:55:59 +02:00
},
dispatch
)
}
Home.propTypes = {
cancelChanges: React.PropTypes.func,
changeInput: React.PropTypes.func,
configFetch: React.PropTypes.func.isRequired,
2017-07-19 13:10:53 +02:00
environments: React.PropTypes.array,
2017-07-18 18:06:15 +02:00
home: React.PropTypes.object,
params: React.PropTypes.object.isRequired,
2017-07-19 13:10:53 +02:00
menuSections: React.PropTypes.array,
submitChanges: React.PropTypes.func,
};
export default connect(mapStateToProps, mapDispatchToProps)(Home);