2017-07-06 14:03:20 +02:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* 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-25 17:03:47 +02:00
|
|
|
import {
|
|
|
|
|
find,
|
|
|
|
|
findIndex,
|
|
|
|
|
findKey,
|
|
|
|
|
forEach,
|
|
|
|
|
get,
|
|
|
|
|
isEmpty,
|
|
|
|
|
includes,
|
|
|
|
|
isObject,
|
|
|
|
|
map,
|
|
|
|
|
toNumber,
|
|
|
|
|
} from 'lodash';
|
2017-07-20 16:59:47 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2017-07-06 14:03:20 +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-20 16:59:47 +02:00
|
|
|
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';
|
2017-07-06 14:03:20 +02:00
|
|
|
import selectHome from './selectors';
|
2017-07-24 11:11:28 +02:00
|
|
|
import {
|
|
|
|
|
cancelChanges,
|
2017-07-24 17:28:10 +02:00
|
|
|
changeDefaultLanguage,
|
2017-07-25 17:03:47 +02:00
|
|
|
changeInput,
|
|
|
|
|
configFetch,
|
|
|
|
|
editSettings,
|
|
|
|
|
languageDelete,
|
|
|
|
|
languagesFetch,
|
|
|
|
|
newLanguagePost,
|
2017-07-24 11:11:28 +02:00
|
|
|
} from './actions'
|
2017-07-06 14:03:20 +02:00
|
|
|
import styles from './styles.scss';
|
2017-07-17 18:53:42 +02:00
|
|
|
import config from './config.json';
|
2017-07-06 14:03:20 +02:00
|
|
|
|
|
|
|
|
export class Home extends React.Component { // eslint-disable-line react/prefer-stateless-function
|
2017-07-17 12:36:04 +02:00
|
|
|
|
2017-07-18 15:53:56 +02:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.customComponents = config.customComponents;
|
|
|
|
|
this.components = {
|
|
|
|
|
editForm: EditForm,
|
2017-07-20 16:59:47 +02:00
|
|
|
list: List,
|
2017-07-19 13:10:53 +02:00
|
|
|
defaultComponent: HeaderNav, // TODO change to default
|
2017-07-18 15:53:56 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 18:50:51 +02:00
|
|
|
componentDidMount() {
|
|
|
|
|
if (this.props.params.slug) {
|
2017-07-21 16:37:12 +02:00
|
|
|
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 18:50:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 12:36:04 +02:00
|
|
|
|
2017-07-13 16:55:59 +02:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2017-07-17 12:36:04 +02:00
|
|
|
// check if params slug updated
|
2017-07-20 15:27:05 +02:00
|
|
|
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
|
2017-07-21 16:37:12 +02:00
|
|
|
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) {
|
2017-07-17 12:36:04 +02:00
|
|
|
// 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-21 16:37:12 +02:00
|
|
|
}
|
2017-07-17 18:40:11 +02:00
|
|
|
|
2017-07-25 17:03:47 +02:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
|
if (prevProps.home.didCreatedNewLanguage !== this.props.home.didCreatedNewLanguage) {
|
|
|
|
|
this.handleFetch(this.props);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 16:37:12 +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 }) => {
|
2017-07-19 14:52:59 +02:00
|
|
|
const value = target.type === 'number' ? toNumber(target.value) : target.value;
|
|
|
|
|
this.props.changeInput(target.name, value);
|
2017-07-18 17:52:39 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-19 10:49:23 +02:00
|
|
|
handleCancel = () => {
|
|
|
|
|
this.props.cancelChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
2017-07-24 17:28:10 +02:00
|
|
|
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;
|
2017-07-24 17:28:10 +02:00
|
|
|
|
|
|
|
|
// 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-19 10:49:23 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-25 17:03:47 +02:00
|
|
|
handleLanguageDelete = ({ target }) => {
|
2017-07-26 18:32:54 +02:00
|
|
|
// Display notification
|
|
|
|
|
window.Strapi.notification.success('Deleting language...');
|
|
|
|
|
// retrieve the language to delete using the target id
|
2017-07-25 17:03:47 +02:00
|
|
|
this.props.languageDelete(target.id);
|
|
|
|
|
}
|
2017-07-20 18:27:38 +02:00
|
|
|
|
|
|
|
|
addLanguage = () => {
|
2017-07-25 17:03:47 +02:00
|
|
|
this.props.newLanguagePost();
|
2017-07-24 17:28:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2017-07-20 17:05:11 +02:00
|
|
|
// custom Row rendering for the component List with params slug === languages
|
2017-07-27 16:07:09 +02:00
|
|
|
renderRowLanguage = (props, key, liStyles) => {
|
2017-07-25 17:03:47 +02:00
|
|
|
// assign the target id the language name to prepare for delete
|
|
|
|
|
const deleteIcon = props.active ? '' : <i className="fa fa-trash" onClick={this.handleLanguageDelete} id={props.name} />; // eslint-disable-line jsx-a11y/no-static-element-interactions
|
2017-07-24 17:28:10 +02:00
|
|
|
// 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 }} /> : '';
|
|
|
|
|
|
2017-07-20 18:27:38 +02:00
|
|
|
const languageLabel = props.active ?
|
2017-07-27 16:07:09 +02:00
|
|
|
<span className={liStyles.italicText}>
|
2017-07-20 18:27:38 +02:00
|
|
|
<FormattedMessage {...{id: 'list.languages.default.languages'}} />
|
|
|
|
|
</span> :
|
2017-07-24 17:28:10 +02:00
|
|
|
// set the span's id with the language name to retrieve it
|
|
|
|
|
<FormattedMessage {...{id: 'list.languages.set.languages'}}>
|
|
|
|
|
{(message) => (
|
2017-07-27 16:07:09 +02:00
|
|
|
<button className={liStyles.normal} onClick={this.changeDefaultLanguage} id={props.name}>
|
2017-07-24 17:28:10 +02:00
|
|
|
{message}
|
2017-07-24 22:35:30 +02:00
|
|
|
</button>
|
2017-07-24 17:28:10 +02:00
|
|
|
)}
|
|
|
|
|
</FormattedMessage>;
|
2017-07-20 18:27:38 +02:00
|
|
|
|
2017-07-20 16:59:47 +02:00
|
|
|
return (
|
2017-07-27 16:07:09 +02:00
|
|
|
<li key={key}>
|
|
|
|
|
<div className={liStyles.flexLi}>
|
|
|
|
|
<div className={liStyles.flexed}>
|
|
|
|
|
<div>{key}</div>
|
|
|
|
|
<div className={liStyles.label}>{languageDisplay}</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div>{props.name}</div>
|
|
|
|
|
<div className={liStyles.centered}>{languageLabel}</div>
|
|
|
|
|
<div>{deleteIcon}</div>
|
|
|
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
|
)
|
2017-07-20 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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} {titleDisplay}</span>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderListButtonLabel = () => `list.${this.props.params.slug}.button.label`;
|
|
|
|
|
|
2017-07-25 15:34:43 +02:00
|
|
|
renderPopUpFormLanguage = (section, props) => (
|
|
|
|
|
map(section.items, (item, key) => (
|
|
|
|
|
<div key={key}>
|
|
|
|
|
<form>
|
|
|
|
|
{props.renderInput(item, key)}
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
))
|
|
|
|
|
)
|
|
|
|
|
|
2017-07-28 15:01:51 +02:00
|
|
|
// renderRowDatabase = (props, key, listStyles) => {
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
|
2017-07-19 13:10:53 +02:00
|
|
|
renderComponent = () => {
|
|
|
|
|
// check if settingName (params.slug) has a custom view display
|
2017-07-24 19:09:56 +02:00
|
|
|
const specificComponent = findKey(this.customComponents, (value) => includes(value, this.props.params.slug)) || 'defaultComponent';
|
2017-07-19 13:10:53 +02:00
|
|
|
// if custom view display render specificComponent
|
|
|
|
|
const Component = this.components[specificComponent];
|
2017-07-28 15:01:51 +02:00
|
|
|
|
2017-07-20 16:59:47 +02:00
|
|
|
const listTitle = this.props.params.slug === 'languages' ? this.renderListTitle() : '';
|
2017-07-25 12:42:03 +02:00
|
|
|
// sections is the props used by EditForm in case of list of table rendering we need to change its value
|
2017-07-25 15:34:43 +02:00
|
|
|
const sections = this.props.params.slug === 'languages' ? this.props.home.listLanguages.sections : this.props.home.configsDisplay.sections;
|
2017-07-20 16:59:47 +02:00
|
|
|
const listButtonLabel = this.props.params.slug === 'languages' ? this.renderListButtonLabel() : '';
|
|
|
|
|
|
2017-07-25 15:34:43 +02:00
|
|
|
// custom selectOptions for languages
|
|
|
|
|
const selectOptions = this.props.params.slug === 'languages' ? this.props.home.listLanguages : [];
|
|
|
|
|
|
|
|
|
|
// custom rendering for PopUpForm
|
|
|
|
|
const renderPopUpForm = this.props.params.slug === 'languages' ? this.renderPopUpFormLanguage : false;
|
|
|
|
|
|
2017-07-27 16:07:09 +02:00
|
|
|
// TODO remove temporary condition to handle nestedForm rendering
|
|
|
|
|
const checkForNestedForm = this.props.params.slug !== 'languages'
|
|
|
|
|
|
2017-07-28 15:01:51 +02:00
|
|
|
let renderRow = false;
|
|
|
|
|
|
|
|
|
|
if (this.props.params.slug === 'languages') {
|
|
|
|
|
renderRow = this.renderRowDatabase;
|
|
|
|
|
} else if (this.props.params.slug === 'databases') {
|
|
|
|
|
renderRow = this.renderRowLanguage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO uncomment
|
|
|
|
|
// if (this.props.params.slug === 'languages') {
|
|
|
|
|
// renderRow = this.renderRowLanguage;
|
|
|
|
|
// } else if (this.props.params.slug === 'databases') {
|
|
|
|
|
// renderRow = this.renderRowDatabase;
|
|
|
|
|
// }
|
|
|
|
|
// const renderRow = this.props.params.slug === 'languages' ? this.renderRowLanguage : false;
|
|
|
|
|
|
2017-07-19 13:10:53 +02:00
|
|
|
return (
|
|
|
|
|
<Component
|
2017-07-25 12:42:03 +02:00
|
|
|
sections={sections}
|
|
|
|
|
listItems={this.props.home.configsDisplay.sections}
|
2017-07-19 13:10:53 +02:00
|
|
|
values={this.props.home.modifiedData}
|
|
|
|
|
handleChange={this.handleChange}
|
|
|
|
|
handleCancel={this.handleCancel}
|
|
|
|
|
handleSubmit={this.handleSubmit}
|
|
|
|
|
links={this.props.environments}
|
|
|
|
|
path={this.props.location.pathname}
|
2017-07-20 15:27:05 +02:00
|
|
|
slug={this.props.params.slug}
|
2017-07-20 16:59:47 +02:00
|
|
|
renderRow={renderRow}
|
|
|
|
|
listTitle={listTitle}
|
|
|
|
|
listButtonLabel={listButtonLabel}
|
|
|
|
|
handlei18n
|
2017-07-21 16:37:12 +02:00
|
|
|
handleListPopUpSubmit={this.addLanguage}
|
2017-07-25 15:34:43 +02:00
|
|
|
selectOptions={selectOptions}
|
|
|
|
|
renderPopUpForm={renderPopUpForm}
|
2017-07-27 16:07:09 +02:00
|
|
|
checkForNestedForm={checkForNestedForm}
|
2017-07-19 13:10:53 +02:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 14:03:20 +02:00
|
|
|
render() {
|
2017-07-17 19:19:54 +02:00
|
|
|
if (this.props.home.loading) {
|
|
|
|
|
return <div />;
|
|
|
|
|
}
|
2017-07-21 16:37:12 +02:00
|
|
|
|
2017-07-06 14:03:20 +02:00
|
|
|
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
|
2017-07-21 16:37:12 +02:00
|
|
|
title="Settings Manager"
|
2017-07-06 16:18:43 +02:00
|
|
|
meta={[
|
2017-07-21 16:37:12 +02:00
|
|
|
{ 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}
|
|
|
|
|
/>
|
2017-07-19 14:52:59 +02:00
|
|
|
{this.renderComponent()}
|
2017-07-06 14:03:20 +02:00
|
|
|
</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
|
|
|
})
|
2017-07-06 14:03:20 +02:00
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
2017-07-13 16:55:59 +02:00
|
|
|
return bindActionCreators(
|
|
|
|
|
{
|
2017-07-19 10:49:23 +02:00
|
|
|
cancelChanges,
|
2017-07-24 17:28:10 +02:00
|
|
|
changeDefaultLanguage,
|
2017-07-18 18:32:32 +02:00
|
|
|
changeInput,
|
2017-07-13 16:55:59 +02:00
|
|
|
configFetch,
|
2017-07-25 17:03:47 +02:00
|
|
|
editSettings,
|
|
|
|
|
languageDelete,
|
2017-07-20 15:27:05 +02:00
|
|
|
languagesFetch,
|
2017-07-25 17:03:47 +02:00
|
|
|
newLanguagePost,
|
2017-07-13 16:55:59 +02:00
|
|
|
},
|
|
|
|
|
dispatch
|
|
|
|
|
)
|
2017-07-06 14:03:20 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-17 12:36:04 +02:00
|
|
|
Home.propTypes = {
|
2017-07-19 10:49:23 +02:00
|
|
|
cancelChanges: React.PropTypes.func,
|
2017-07-24 17:28:10 +02:00
|
|
|
changeDefaultLanguage: React.PropTypes.func,
|
2017-07-18 18:32:32 +02:00
|
|
|
changeInput: React.PropTypes.func,
|
2017-07-17 12:36:04 +02:00
|
|
|
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,
|
2017-07-25 17:03:47 +02:00
|
|
|
languageDelete: React.PropTypes.func,
|
2017-07-20 15:27:05 +02:00
|
|
|
languagesFetch: React.PropTypes.func,
|
2017-07-19 14:52:59 +02:00
|
|
|
location: React.PropTypes.object,
|
2017-07-19 13:10:53 +02:00
|
|
|
menuSections: React.PropTypes.array,
|
2017-07-25 17:03:47 +02:00
|
|
|
newLanguagePost: React.PropTypes.func,
|
2017-07-19 14:52:59 +02:00
|
|
|
params: React.PropTypes.object.isRequired,
|
2017-07-17 12:36:04 +02:00
|
|
|
};
|
|
|
|
|
|
2017-07-06 14:03:20 +02:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(Home);
|