201 lines
5.6 KiB
JavaScript
Raw Normal View History

2017-11-06 18:15:53 +01:00
/**
2019-04-02 17:44:25 +02:00
*
* ListRow
*
*/
2017-11-06 18:15:53 +01:00
import React from 'react';
import PropTypes from 'prop-types';
2018-01-18 18:21:49 +01:00
import { FormattedMessage } from 'react-intl';
import { capitalize, get, includes } from 'lodash';
import { IconLinks } from '@buffetjs/core';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import getTrad from '../../utils/getTrad';
import { PopUpWarning } from 'strapi-helper-plugin';
import en from '../../translations/en.json';
import { HomePageContext } from '../../contexts/HomePage';
2019-09-18 17:39:54 +02:00
import { Container, Flex, Row, Wrapper } from './Components';
2017-11-06 18:25:40 +01:00
2019-04-02 17:44:25 +02:00
class ListRow extends React.Component {
// eslint-disable-line react/prefer-stateless-function
2017-11-06 18:25:40 +01:00
state = { showModalDelete: false };
static contextType = HomePageContext;
2017-12-04 15:03:53 +01:00
// Roles that can't be deleted && modified
// Don't delete this line
protectedRoleIDs = [];
2017-12-04 15:03:53 +01:00
// Roles that can't be deleted;
undeletableIDs = ['public', 'authenticated'];
2017-12-04 15:03:53 +01:00
2017-11-06 18:15:53 +01:00
generateContent = () => {
let links = [
2017-11-06 18:15:53 +01:00
{
icon: <FontAwesomeIcon icon="pencil-alt" />,
onClick: this.handleClick,
2017-11-06 18:15:53 +01:00
},
{
icon: <FontAwesomeIcon icon="trash-alt" />,
onClick: e => {
e.stopPropagation();
2019-04-02 17:44:25 +02:00
this.setState({ showModalDelete: true });
},
2017-11-06 18:15:53 +01:00
},
];
switch (this.props.settingType) {
case 'roles':
2018-01-24 13:51:22 +01:00
if (includes(this.protectedRoleIDs, get(this.props.item, 'type', ''))) {
links = [];
}
2018-01-24 13:51:22 +01:00
if (includes(this.undeletableIDs, get(this.props.item, 'type', ''))) {
links = [
{
icon: <FontAwesomeIcon icon="pencil-alt" />,
onClick: this.handleClick,
},
];
2017-12-04 15:03:53 +01:00
}
2017-11-06 18:15:53 +01:00
return (
2019-09-13 15:17:24 +02:00
<Wrapper className="row" style={{ paddingLeft: '20px' }}>
<div className="col-md-2">
2017-11-06 18:15:53 +01:00
<b>{this.props.item.name}</b>
</div>
<div className="col-md-7">{this.props.item.description}</div>
<div className="col-md-1">
2017-11-30 16:34:43 +01:00
<strong>{this.props.item.nb_users || 0}</strong>&nbsp;
2019-04-02 17:44:25 +02:00
{this.props.item.nb_users > 1 ? 'users' : 'user'}
2017-11-06 18:15:53 +01:00
</div>
<div className="col-md-2">
<IconLinks links={links} />
2017-11-06 18:15:53 +01:00
</div>
2019-09-13 15:17:24 +02:00
</Wrapper>
2017-11-06 18:15:53 +01:00
);
case 'providers':
links.pop(); // Remove the delete CTA
2019-11-26 16:24:53 +01:00
2017-11-06 18:15:53 +01:00
return (
2019-09-13 15:17:24 +02:00
<Wrapper className="row">
<div className="col-md-4">
2019-09-13 15:17:24 +02:00
<Flex>
2017-11-06 18:15:53 +01:00
<div>
2019-11-25 17:08:30 +01:00
<i
className={`fa${this.props.item.key !== undefined ? 'b' : ''} fa-${
this.props.item.icon
}`}
2019-11-25 17:08:30 +01:00
/>
2017-11-06 18:15:53 +01:00
</div>
2019-04-02 17:44:25 +02:00
<div>{capitalize(this.props.item.name)}</div>
2019-09-13 15:17:24 +02:00
</Flex>
2017-11-06 18:15:53 +01:00
</div>
<div className="col-md-6" style={{ fontWeight: '500' }}>
{get(this.props.values, [get(this.props.item, 'name'), 'enabled']) ? (
<span style={{ color: '#5A9E06' }}>
<FormattedMessage id={getTrad('ListRow.enabled')} />
</span>
2019-09-13 15:17:24 +02:00
) : (
<span style={{ color: '#F64D0A' }}>
<FormattedMessage id={getTrad('ListRow.disabled')} />
</span>
2019-09-13 15:17:24 +02:00
)}
2017-11-06 18:15:53 +01:00
</div>
<div className="col-md-2">
<IconLinks links={links} />
2017-11-06 18:15:53 +01:00
</div>
2019-09-13 15:17:24 +02:00
</Wrapper>
2017-11-06 18:15:53 +01:00
);
case 'email-templates':
links.pop(); // Remove the delete CTA
2017-11-06 18:15:53 +01:00
return (
2019-09-13 15:17:24 +02:00
<Wrapper className="row">
<div className="col-md-4">
2019-09-13 15:17:24 +02:00
<Flex>
2017-11-06 18:15:53 +01:00
<div>
2019-11-25 17:08:30 +01:00
<i className={`fas fa-${this.props.item.icon}`} />
2017-11-06 18:15:53 +01:00
</div>
<div>
2018-01-18 18:21:49 +01:00
{this.props.item.display && en[this.props.item.display] ? (
<FormattedMessage id={getTrad(this.props.item.display)} />
2019-04-02 17:44:25 +02:00
) : (
this.props.item.name
)}
2017-11-06 18:15:53 +01:00
</div>
2019-09-13 15:17:24 +02:00
</Flex>
2017-11-06 18:15:53 +01:00
</div>
<div className="col-md-8">
<IconLinks links={links} />
2017-11-06 18:15:53 +01:00
</div>
2019-09-13 15:17:24 +02:00
</Wrapper>
2017-11-06 18:15:53 +01:00
);
default:
return '';
}
2019-04-02 17:44:25 +02:00
};
2017-11-06 18:15:53 +01:00
handleClick = () => {
2019-04-02 17:44:25 +02:00
const { pathname, push } = this.context;
switch (this.props.settingType) {
2017-11-27 17:50:51 +01:00
case 'roles': {
if (!includes(this.protectedRoleIDs, get(this.props.item, 'type', ''))) {
2019-04-02 17:44:25 +02:00
return push(`${pathname}/edit/${this.props.item.id}`);
2017-11-27 17:50:51 +01:00
}
return;
}
case 'providers':
this.context.emitEvent('willEditAuthenticationProvider');
2019-04-02 17:44:25 +02:00
return this.context.setDataToEdit(this.props.item.name);
case 'email-templates':
this.context.emitEvent('willEditEmailTemplates');
2019-04-02 17:44:25 +02:00
2018-01-19 13:34:55 +01:00
return this.context.setDataToEdit(this.props.item.name);
default:
return;
}
2019-04-02 17:44:25 +02:00
};
handleDelete = () => {
this.props.deleteData(this.props.item, this.props.settingType);
this.setState({ showModalDelete: false });
2019-04-02 17:44:25 +02:00
};
2017-11-06 18:15:53 +01:00
render() {
return (
2019-09-13 15:17:24 +02:00
<Row onClick={this.handleClick}>
<Container>{this.generateContent()}</Container>
<PopUpWarning
isOpen={this.state.showModalDelete}
onConfirm={this.handleDelete}
toggleModal={() => this.setState({ showModalDelete: false })}
/>
2019-09-13 15:17:24 +02:00
</Row>
2017-11-06 18:15:53 +01:00
);
}
}
ListRow.defaultProps = {
item: {
name: 'Owner',
2019-09-13 15:17:24 +02:00
description: "Rule them all. This role can't be deleted",
2017-11-06 18:15:53 +01:00
nb_users: 1,
2018-01-18 18:21:49 +01:00
icon: 'envelope',
2017-11-06 18:15:53 +01:00
},
settingType: 'roles',
2017-11-08 16:06:21 +01:00
};
2017-11-06 18:15:53 +01:00
2017-11-08 16:06:21 +01:00
ListRow.propTypes = {
deleteData: PropTypes.func.isRequired,
2017-11-06 18:15:53 +01:00
item: PropTypes.object,
settingType: PropTypes.string,
2018-01-22 11:00:41 +01:00
values: PropTypes.object.isRequired,
2017-11-06 18:15:53 +01:00
};
export default ListRow;