98 lines
2.6 KiB
JavaScript
Raw Normal View History

/**
2019-04-16 16:55:53 +02:00
*
* Policies
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
2017-12-01 10:26:06 +01:00
import { get, isEmpty, map, takeRight, toLower, without } from 'lodash';
2017-11-30 15:01:19 +01:00
2019-04-16 16:55:53 +02:00
import { InputsIndex as Input } from 'strapi-helper-plugin';
2019-02-22 11:16:42 +01:00
import BoundRoute from '../BoundRoute';
2019-09-17 15:24:22 +02:00
import { Header, Wrapper } from './Components';
2019-04-16 16:55:53 +02:00
class Policies extends React.Component {
// eslint-disable-line react/prefer-stateless-function
handleChange = e => this.context.onChange(e);
2017-11-30 15:01:19 +01:00
render() {
const baseTitle = 'users-permissions.Policies.header';
const title = this.props.shouldDisplayPoliciesHint ? 'hint' : 'title';
2017-11-30 15:01:19 +01:00
const value = get(this.props.values, this.props.inputSelectName);
2019-04-16 16:55:53 +02:00
const path = without(
this.props.inputSelectName.split('.'),
'permissions',
'controllers',
2019-09-17 15:24:22 +02:00
'policy'
2019-04-16 16:55:53 +02:00
);
const controllerRoutes = get(
this.props.routes,
without(
this.props.inputSelectName.split('.'),
'permissions',
'controllers',
2019-09-17 15:24:22 +02:00
'policy'
)[0]
2019-04-16 16:55:53 +02:00
);
const routes = isEmpty(controllerRoutes)
? []
: controllerRoutes.filter(
2019-09-17 15:24:22 +02:00
o => toLower(o.handler) === toLower(takeRight(path, 2).join('.'))
);
2017-11-30 18:07:30 +01:00
return (
2019-09-17 15:24:22 +02:00
<Wrapper className="col-md-5">
<div className="container-fluid">
2019-09-17 15:24:22 +02:00
<div className="row">
<Header className="col-md-12">
<FormattedMessage id={`${baseTitle}.${title}`} />
2019-09-17 15:24:22 +02:00
</Header>
2017-11-30 15:01:19 +01:00
{!this.props.shouldDisplayPoliciesHint ? (
<Input
customBootstrapClass="col-md-12"
label={{ id: 'users-permissions.Policies.InputSelect.label' }}
2017-11-30 15:01:19 +01:00
name={this.props.inputSelectName}
onChange={this.handleChange}
selectOptions={this.props.selectOptions}
type="select"
validations={{}}
value={value}
/>
2019-04-16 16:55:53 +02:00
) : (
''
)}
</div>
2017-11-30 18:07:30 +01:00
<div className="row">
2019-04-16 16:55:53 +02:00
{!this.props.shouldDisplayPoliciesHint
? map(routes, (route, key) => (
2019-09-17 15:24:22 +02:00
<BoundRoute key={key} route={route} />
))
2019-04-16 16:55:53 +02:00
: ''}
2017-11-30 18:07:30 +01:00
</div>
</div>
2019-09-17 15:24:22 +02:00
</Wrapper>
);
}
}
2017-11-30 15:01:19 +01:00
Policies.contextTypes = {
onChange: PropTypes.func.isRequired,
};
2017-11-30 16:34:43 +01:00
Policies.defaultProps = {
routes: {},
};
Policies.propTypes = {
2017-11-30 15:01:19 +01:00
inputSelectName: PropTypes.string.isRequired,
2017-11-30 16:34:43 +01:00
routes: PropTypes.object,
2017-11-30 15:01:19 +01:00
selectOptions: PropTypes.array.isRequired,
shouldDisplayPoliciesHint: PropTypes.bool.isRequired,
2017-11-30 15:01:19 +01:00
values: PropTypes.object.isRequired,
};
export default Policies;