138 lines
4.1 KiB
JavaScript
Raw Normal View History

/**
2019-08-13 11:31:10 +02:00
*
* Plugin
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { Collapse } from 'reactstrap';
2019-12-06 11:09:48 +01:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
2017-12-04 15:03:53 +01:00
import { capitalize, get, isEmpty, map } from 'lodash';
import { FormattedMessage } from 'react-intl';
2019-10-03 12:58:20 +02:00
import { EditPageContext } from '../../contexts/EditPage';
2019-02-22 11:16:42 +01:00
import Controller from '../Controller';
import { Banner, Chevron, ControllerWrapper, Description, Icon, Name, Wrapper } from './Components';
2019-08-13 11:31:10 +02:00
class Plugin extends React.Component {
// eslint-disable-line react/prefer-stateless-function
state = { collapse: false };
2019-10-03 12:58:20 +02:00
static contextType = EditPageContext;
componentDidMount() {
// Open the application's permissions section if there are APIs
if (this.props.name === 'application' && !isEmpty(get(this.props.plugin, 'controllers'))) {
this.props.changePluginSelected('application');
this.setState({ collapse: !this.state.collapse });
}
}
2019-08-13 11:31:10 +02:00
UNSAFE_componentWillReceiveProps(nextProps) {
if (
nextProps.pluginSelected !== this.props.pluginSelected &&
nextProps.pluginSelected !== this.props.name
) {
this.context.resetShouldDisplayPoliciesHint();
2017-11-16 15:13:46 +01:00
this.setState({ collapse: false });
}
}
handleClick = () => {
this.props.changePluginSelected(this.props.name);
2017-12-04 15:03:53 +01:00
if (!isEmpty(get(this.props.plugin, 'controllers'))) {
this.setState({ collapse: !this.state.collapse });
}
2017-11-30 15:01:19 +01:00
if (this.state.collapse) {
this.context.resetShouldDisplayPoliciesHint();
}
2019-08-13 11:31:10 +02:00
};
render() {
const { appPlugins } = this.context;
const { plugin } = this.props;
const divStyle = this.state.collapse ? { marginBottom: '.4rem' } : {};
const pluginId = get(plugin, ['information', 'id'], null);
const icon = get(appPlugins, [pluginId, 'pluginLogo'], null);
2017-12-14 17:06:26 +01:00
const emptyApplication = !isEmpty(get(this.props.plugin, 'controllers'));
if (!emptyApplication) {
return <div />;
}
return (
2019-09-16 19:18:35 +02:00
<Wrapper style={divStyle}>
<Banner onClick={this.handleClick}>
<div>
2018-06-14 15:30:54 +02:00
{this.props.name !== 'application' && (
2019-09-16 19:18:35 +02:00
<Icon>{icon && <img src={icon} alt="icon" />}</Icon>
2018-06-14 15:30:54 +02:00
)}
2019-09-16 19:18:35 +02:00
<Name>{this.props.name}</Name>
&nbsp;&nbsp;
2019-09-16 19:18:35 +02:00
<Description>
2017-11-16 17:59:41 +01:00
{this.props.name === 'application' ? (
2019-08-13 11:31:10 +02:00
<FormattedMessage id="users-permissions.Plugin.permissions.application.description" />
2017-11-16 17:59:41 +01:00
) : (
<FormattedMessage
id="users-permissions.Plugin.permissions.plugins.description"
values={{ name: capitalize(this.props.name) }}
/>
)}
2019-09-16 19:18:35 +02:00
</Description>
{emptyApplication && (
<Chevron>
{this.state.collapse ? (
2019-12-06 11:09:48 +01:00
<FontAwesomeIcon icon="chevron-up" />
) : (
2019-12-06 11:09:48 +01:00
<FontAwesomeIcon icon="chevron-down" />
)}
</Chevron>
)}
</div>
2019-09-16 19:18:35 +02:00
</Banner>
<Collapse isOpen={this.state.collapse}>
<div />
2019-09-16 19:18:35 +02:00
<ControllerWrapper>
{map(get(this.props.plugin, 'controllers'), (controllerActions, key) => (
<Controller
inputNamePath={`permissions.${this.props.name}`}
isOpen={this.state.collapse}
key={key}
name={key}
actions={controllerActions}
resetInputBackground={this.state.resetInputBackground}
/>
))}
2019-09-16 19:18:35 +02:00
</ControllerWrapper>
</Collapse>
2019-09-16 19:18:35 +02:00
</Wrapper>
);
}
}
Plugin.defaultProps = {
name: '',
plugin: {
description: 'users-permissions.Plugin.permissions.description.empty',
controllers: {},
information: {},
},
};
Plugin.propTypes = {
2017-11-16 15:13:46 +01:00
changePluginSelected: PropTypes.func.isRequired,
name: PropTypes.string,
plugin: PropTypes.shape({
description: PropTypes.string,
information: PropTypes.shape({
2018-01-12 17:50:18 +01:00
logo: PropTypes.string,
2018-01-11 17:13:09 +01:00
}),
}),
2017-11-16 15:13:46 +01:00
pluginSelected: PropTypes.string.isRequired,
};
export default Plugin;