2017-11-15 17:35:32 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Controller
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-11-29 18:51:56 +01:00
|
|
|
import { get, map, some } from 'lodash';
|
|
|
|
import cn from 'classnames';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2017-11-15 17:35:32 +01:00
|
|
|
|
2017-11-15 17:48:30 +01:00
|
|
|
import InputCheckbox from 'components/InputCheckbox';
|
2017-11-15 17:35:32 +01:00
|
|
|
import styles from './styles.scss';
|
|
|
|
|
2017-11-16 15:13:46 +01:00
|
|
|
class Controller extends React.Component {
|
2017-11-29 18:51:56 +01:00
|
|
|
state = { inputSelected: '', checked: false };
|
2017-11-16 15:13:46 +01:00
|
|
|
|
2017-11-29 18:51:56 +01:00
|
|
|
setNewInputSelected = (name) => {
|
|
|
|
this.setState({ inputSelected: name, checked: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = () => {
|
|
|
|
this.setState({ checked: !this.state.checked });
|
|
|
|
this.context.selectAllActions(`${this.props.inputNamePath}.controllers.${this.props.name}`, !this.isAllActionsSelected());
|
|
|
|
}
|
|
|
|
|
|
|
|
isAllActionsSelected = () => !some(this.props.actions, ['enabled', false]);
|
2017-11-16 15:13:46 +01:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className={styles.controller}>
|
|
|
|
<div className={styles.controllerHeader}>
|
2017-11-29 18:51:56 +01:00
|
|
|
<div>{this.props.name}</div>
|
2017-12-04 17:32:45 +01:00
|
|
|
<div className={styles.separator}></div>
|
2017-11-29 18:51:56 +01:00
|
|
|
<div>
|
|
|
|
<div className={cn(styles.inputCheckbox)}>
|
|
|
|
<div className="form-check">
|
|
|
|
<label className={cn('form-check-label', styles.label, this.state.checked ? styles.checked : '')} htmlFor={this.props.name}>
|
|
|
|
<input
|
|
|
|
className="form-check-input"
|
|
|
|
checked={this.state.checked}
|
|
|
|
id={this.props.name}
|
|
|
|
name={this.props.name}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
type="checkbox"
|
|
|
|
/>
|
|
|
|
<FormattedMessage id="users-permissions.Controller.selectAll" />
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2017-11-16 15:13:46 +01:00
|
|
|
</div>
|
|
|
|
<div className="row">
|
|
|
|
{map(Object.keys(this.props.actions).sort(), (actionKey) => (
|
|
|
|
<InputCheckbox
|
|
|
|
inputSelected={this.state.inputSelected}
|
2017-11-30 15:01:19 +01:00
|
|
|
isOpen={this.props.isOpen}
|
2017-11-16 15:13:46 +01:00
|
|
|
key={actionKey}
|
|
|
|
label={actionKey}
|
2017-11-16 17:59:41 +01:00
|
|
|
name={`${this.props.inputNamePath}.controllers.${this.props.name}.${actionKey}.enabled`}
|
2017-11-16 15:13:46 +01:00
|
|
|
setNewInputSelected={this.setNewInputSelected}
|
|
|
|
value={get(this.props.actions[actionKey], 'enabled')}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
2017-11-15 17:35:32 +01:00
|
|
|
</div>
|
2017-11-16 15:13:46 +01:00
|
|
|
);
|
|
|
|
}
|
2017-11-15 17:35:32 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 18:51:56 +01:00
|
|
|
Controller.contextTypes = {
|
|
|
|
selectAllActions: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2017-11-15 17:35:32 +01:00
|
|
|
Controller.defaultProps = {
|
|
|
|
actions: {},
|
2017-11-16 13:43:55 +01:00
|
|
|
inputNamePath: 'permissions.application',
|
2017-11-15 17:35:32 +01:00
|
|
|
name: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
Controller.propTypes = {
|
|
|
|
actions: PropTypes.object,
|
2017-11-16 13:43:55 +01:00
|
|
|
inputNamePath: PropTypes.string,
|
2017-11-30 15:01:19 +01:00
|
|
|
isOpen: PropTypes.bool.isRequired,
|
2017-11-15 17:35:32 +01:00
|
|
|
name: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Controller;
|