95 lines
2.4 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';
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-10-03 12:58:20 +02:00
import { useEditPageContext } from '../../contexts/EditPage';
2019-09-17 15:24:22 +02:00
import { Header, Wrapper } from './Components';
2019-10-03 12:58:20 +02:00
const Policies = ({
inputSelectName,
routes,
selectOptions,
shouldDisplayPoliciesHint,
values,
}) => {
const { onChange } = useEditPageContext();
const baseTitle = 'users-permissions.Policies.header';
const title = shouldDisplayPoliciesHint ? 'hint' : 'title';
const value = get(values, inputSelectName);
const path = without(
inputSelectName.split('.'),
'permissions',
'controllers',
'policy'
);
const controllerRoutes = get(
routes,
without(
inputSelectName.split('.'),
2019-04-16 16:55:53 +02:00
'permissions',
'controllers',
2019-09-17 15:24:22 +02:00
'policy'
2019-10-03 12:58:20 +02:00
)[0]
);
const displayedRoutes = isEmpty(controllerRoutes)
? []
: controllerRoutes.filter(
o => toLower(o.handler) === toLower(takeRight(path, 2).join('.'))
);
2017-11-30 18:07:30 +01:00
2019-10-03 12:58:20 +02:00
return (
<Wrapper className="col-md-5">
<div className="container-fluid">
<div className="row">
<Header className="col-md-12">
<FormattedMessage id={`${baseTitle}.${title}`} />
</Header>
{!shouldDisplayPoliciesHint ? (
<Input
customBootstrapClass="col-md-12"
label={{ id: 'users-permissions.Policies.InputSelect.label' }}
name={inputSelectName}
onChange={onChange}
selectOptions={selectOptions}
type="select"
validations={{}}
value={value}
/>
) : (
''
)}
</div>
2019-10-03 12:58:20 +02:00
<div className="row">
{!shouldDisplayPoliciesHint
? map(displayedRoutes, (route, key) => (
<BoundRoute key={key} route={route} />
))
: ''}
</div>
</div>
</Wrapper>
);
2017-11-30 15:01:19 +01:00
};
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;