115 lines
3.0 KiB
JavaScript
Raw Normal View History

/**
2019-06-19 11:51:44 +02:00
*
* Controller
*
*/
import React from 'react';
import PropTypes from 'prop-types';
2019-07-02 08:37:41 +02:00
import { get, map } from 'lodash';
2017-11-29 18:51:56 +01:00
import { FormattedMessage } from 'react-intl';
2019-06-19 11:51:44 +02:00
import pluginId from '../../pluginId';
2019-02-22 11:16:42 +01:00
import InputCheckbox from '../InputCheckboxPlugin';
2019-09-16 19:18:35 +02:00
import { Header, Label, Separator, Wrapper } from './Components';
2017-11-16 15:13:46 +01:00
class Controller extends React.Component {
2019-06-19 11:51:44 +02:00
state = { inputSelected: '' };
2017-11-16 15:13:46 +01:00
2019-06-19 11:51:44 +02:00
setNewInputSelected = name => {
this.setState({ inputSelected: name });
};
2017-11-29 18:51:56 +01:00
handleChange = () => {
2019-06-19 11:51:44 +02:00
this.context.selectAllActions(
`${this.props.inputNamePath}.controllers.${this.props.name}`,
!this.areAllActionsSelected()
);
};
2017-11-29 18:51:56 +01:00
2019-06-19 11:51:44 +02:00
hasSomeActionsSelected = () => {
const { actions } = this.props;
return Object.keys(actions).some(
action => actions[action].enabled === true
);
};
areAllActionsSelected = () => {
const { actions } = this.props;
return Object.keys(actions).every(
action => actions[action].enabled === true
);
};
2017-11-16 15:13:46 +01:00
render() {
2019-06-19 11:51:44 +02:00
const labelId = this.areAllActionsSelected() ? 'unselectAll' : 'selectAll';
2017-11-16 15:13:46 +01:00
return (
2019-09-16 19:18:35 +02:00
<Wrapper>
<Header>
2017-11-29 18:51:56 +01:00
<div>{this.props.name}</div>
2019-09-16 19:18:35 +02:00
<Separator />
2017-11-29 18:51:56 +01:00
<div>
2019-09-17 15:24:22 +02:00
<div className="checkbox-wrapper">
2017-11-29 18:51:56 +01:00
<div className="form-check">
2019-09-16 19:18:35 +02:00
<Label
2019-09-17 15:24:22 +02:00
className={`form-check-label ${this.areAllActionsSelected() &&
2019-09-16 19:18:35 +02:00
'checked'} ${!this.areAllActionsSelected() &&
this.hasSomeActionsSelected() &&
'some-checked'}`}
2019-06-19 11:51:44 +02:00
htmlFor={this.props.name}
>
2017-11-29 18:51:56 +01:00
<input
className="form-check-input"
2019-06-19 11:51:44 +02:00
checked={this.areAllActionsSelected()}
2017-11-29 18:51:56 +01:00
id={this.props.name}
name={this.props.name}
onChange={this.handleChange}
type="checkbox"
/>
2019-06-19 11:51:44 +02:00
<FormattedMessage id={`${pluginId}.Controller.${labelId}`} />
2019-09-16 19:18:35 +02:00
</Label>
2017-11-29 18:51:56 +01:00
</div>
</div>
</div>
2019-09-16 19:18:35 +02:00
</Header>
2017-11-16 15:13:46 +01:00
<div className="row">
2019-06-19 11:51:44 +02:00
{map(Object.keys(this.props.actions).sort(), actionKey => (
2017-11-16 15:13:46 +01:00
<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}
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>
2019-09-16 19:18:35 +02:00
</Wrapper>
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;