90 lines
2.6 KiB
JavaScript
Raw Normal View History

/**
*
* 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';
import InputCheckbox from 'components/InputCheckboxPlugin';
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>
</div>
2017-11-16 15:13:46 +01:00
);
}
}
2017-11-29 18:51:56 +01:00
Controller.contextTypes = {
selectAllActions: PropTypes.func.isRequired,
};
Controller.defaultProps = {
actions: {},
inputNamePath: 'permissions.application',
name: '',
};
Controller.propTypes = {
actions: PropTypes.object,
inputNamePath: PropTypes.string,
2017-11-30 15:01:19 +01:00
isOpen: PropTypes.bool.isRequired,
name: PropTypes.string,
};
export default Controller;