288 lines
9.2 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';
import { isEmpty, findKey, includes, get, toNumber, isObject, find, forEach, findIndex } from 'lodash';
import { FormattedMessage } from 'react-intl';
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';
import List from 'components/List';
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';
2017-07-24 11:11:28 +02:00
import {
configFetch,
changeInput,
cancelChanges,
languagesFetch,
editSettings,
changeDefaultLanguage,
2017-07-24 11:11:28 +02:00
} 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,
list: List,
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) {
this.handleFetch(this.props);
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
if (this.props.params.slug !== nextProps.params.slug && nextProps.params.slug) {
2017-07-17 18:40:11 +02:00
if (nextProps.params.slug) {
// get data from api if params slug updated
this.handleFetch(nextProps);
2017-07-17 18:40:11 +02:00
} 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
handleFetch(props) {
if (props.params.slug !== 'languages') {
const apiUrl = props.params.env ? `${props.params.slug}/${props.params.env}` : props.params.slug;
this.props.configFetch(apiUrl);
} else {
this.props.languagesFetch();
}
2017-07-13 16:55:59 +02:00
}
2017-07-18 17:52:39 +02:00
handleChange = ({ target }) => {
const value = target.type === 'number' ? toNumber(target.value) : target.value;
this.props.changeInput(target.name, value);
2017-07-18 17:52:39 +02:00
}
handleCancel = () => {
this.props.cancelChanges();
}
handleSubmit = () => {
const prevSettings = this.props.home.initialData;
const body = {};
2017-07-24 11:11:28 +02:00
const apiUrl = this.props.params.env ? `${this.props.params.slug}/${this.props.params.env}` : this.props.params.slug;
// send only updated settings
forEach(this.props.home.modifiedData, (value, key) => {
if (value !== prevSettings[key]) {
body[key] = value;
}
});
if (!isEmpty(body)) {
this.props.editSettings(body, apiUrl);
} else {
window.Strapi.notification.error('Settings are equals');
}
}
2017-07-20 18:27:38 +02:00
addLanguage = () => {
// console.log('click Add Language');
}
changeDefaultLanguage = ({ target }) => {
// create new object configsDisplay based on store property configsDisplay
const configsDisplay = {
name: this.props.home.configsDisplay.name,
description: this.props.home.configsDisplay.description,
sections: [],
};
// Find the index of the new setted language
const activeLanguageIndex = findIndex(this.props.home.configsDisplay.sections, ['name', target.id]);
forEach(this.props.home.configsDisplay.sections, (section, key) => {
// set all Language active state to false
if (key !== activeLanguageIndex) {
configsDisplay.sections.push({ name: section.name, active: false });
} else {
// set the new language active state to true
configsDisplay.sections.push({ name: section.name, active: true });
}
});
// reset all the configs to ensure component is updated
this.props.changeDefaultLanguage(configsDisplay, target.id);
// Edit the new config
this.props.editSettings({ 'i18n.i18n.defaultLocale': target.id }, 'i18n');
2017-07-20 18:27:38 +02:00
}
// custom Row rendering for the component List with params slug === languages
renderRowLanguage = (props, key) => {
// custom style must be inline
2017-07-20 18:27:38 +02:00
const rowStyle = {
defaultSpanStyle: {
color: '#49515A',
fontStyle: 'italic',
},
normalSpanStyle: {
color: '#1C5DE7',
},
tdNameStyle: {
color: '#333740',
},
tdLangDisplayStyle: {
color: '#333740',
fontWeight: '600',
},
};
const deleteIcon = props.active ? '' : <i className="fa fa-trash" />;
// retrieve language name from i18n translation
const languageObject = find(get(this.props.home.listLanguages, ['sections', '0', 'items', '0', 'items']), ['value', props.name]);
// apply i18n
const languageDisplay = isObject(languageObject) ? <FormattedMessage {...{ id: languageObject.name }} /> : '';
/* eslint-disable jsx-a11y/no-static-element-interactions */
2017-07-20 18:27:38 +02:00
const languageLabel = props.active ?
<span style={rowStyle.defaultSpanStyle}>
<FormattedMessage {...{id: 'list.languages.default.languages'}} />
</span> :
// set the span's id with the language name to retrieve it
<FormattedMessage {...{id: 'list.languages.set.languages'}}>
{(message) => (
<span style={rowStyle.normalSpanStyle} onClick={this.changeDefaultLanguage} id={props.name}>
{message}
</span>
)}
</FormattedMessage>;
2017-07-20 18:27:38 +02:00
return (
<tr key={key}>
<th>{key}</th>
2017-07-20 18:27:38 +02:00
<td style={rowStyle.tdLangDisplayStyle}>{languageDisplay}</td>
<td style={rowStyle.tdNameStyle}>{props.name}</td>
<td>{languageLabel}</td>
<td>{deleteIcon}</td>
</tr>
);
}
renderListTitle = () => {
const availableContentNumber = this.props.home.configsDisplay.sections.length;
const title = availableContentNumber > 1 ? `list.${this.props.params.slug}.title.plural` : `list.${this.props.params.slug}.title.singular`;
const titleDisplay = title ? <FormattedMessage {...{id: title}} /> : '';
return <span>{availableContentNumber}&nbsp;{titleDisplay}</span>
}
renderListButtonLabel = () => `list.${this.props.params.slug}.button.label`;
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];
const renderRow = this.props.params.slug === 'languages' ? this.renderRowLanguage : false;
const listTitle = this.props.params.slug === 'languages' ? this.renderListTitle() : '';
const listButtonLabel = this.props.params.slug === 'languages' ? this.renderListButtonLabel() : '';
2017-07-19 13:10:53 +02:00
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}
slug={this.props.params.slug}
renderRow={renderRow}
listTitle={listTitle}
listButtonLabel={listButtonLabel}
handlei18n
handleListPopUpSubmit={this.addLanguage}
2017-07-19 13:10:53 +02:00
/>
);
}
render() {
2017-07-17 19:19:54 +02:00
if (this.props.home.loading) {
return <div />;
}
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="Settings Manager"
2017-07-06 16:18:43 +02:00
meta={[
{ name: 'Settings Manager Plugin', content: 'Modify your app settings' },
2017-07-06 16:18:43 +02:00
]}
/>
2017-07-18 14:07:48 +02:00
<ContentHeader
name={this.props.home.configsDisplay.name}
description={this.props.home.configsDisplay.description}
/>
{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(
{
2017-07-24 11:11:28 +02:00
editSettings,
cancelChanges,
changeDefaultLanguage,
changeInput,
2017-07-13 16:55:59 +02:00
configFetch,
languagesFetch,
2017-07-13 16:55:59 +02:00
},
dispatch
)
}
Home.propTypes = {
cancelChanges: React.PropTypes.func,
changeDefaultLanguage: React.PropTypes.func,
changeInput: React.PropTypes.func,
configFetch: React.PropTypes.func.isRequired,
2017-07-24 11:11:28 +02:00
editSettings: React.PropTypes.func,
2017-07-19 13:10:53 +02:00
environments: React.PropTypes.array,
2017-07-18 18:06:15 +02:00
home: React.PropTypes.object,
languagesFetch: React.PropTypes.func,
location: React.PropTypes.object,
2017-07-19 13:10:53 +02:00
menuSections: React.PropTypes.array,
params: React.PropTypes.object.isRequired,
};
export default connect(mapStateToProps, mapDispatchToProps)(Home);