189 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-11-06 18:15:53 +01:00
/**
*
* ListRow
*
*/
import React from 'react';
import cn from 'classnames';
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 { router } from 'app';
2017-11-06 18:15:53 +01:00
// Design
import IcoContainer from 'components/IcoContainer';
2017-11-06 18:25:40 +01:00
import PopUpWarning from 'components/PopUpWarning';
2018-01-18 18:21:49 +01:00
import en from 'translations/en.json';
2017-11-06 18:15:53 +01:00
import styles from './styles.scss';
class ListRow extends React.Component { // eslint-disable-line react/prefer-stateless-function
2017-11-06 18:25:40 +01:00
state = { showModalDelete: false };
2017-12-04 15:03:53 +01:00
// Roles that can't be deleted && modified
// Don't delete this line
protectedRoleIDs = ['root'];
2017-12-04 15:03:53 +01:00
// Roles that can't be deleted;
undeletableIDs = ['guest', 'registered'];
2017-12-04 15:03:53 +01:00
2017-11-06 18:15:53 +01:00
generateContent = () => {
let icons = [
{
icoType: 'pencil',
onClick: this.handleClick,
2017-11-06 18:15:53 +01:00
},
{
icoType: 'trash',
2017-11-08 16:06:21 +01:00
onClick: () => { 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', ''))) {
2017-11-27 17:50:51 +01:00
icons = [];
}
2018-01-24 13:51:22 +01:00
if (includes(this.undeletableIDs, get(this.props.item, 'type', ''))) {
2017-12-04 15:03:53 +01:00
icons = [{ icoType: 'pencil', onClick: this.handleClick }];
}
2017-11-06 18:15:53 +01:00
return (
<div className={cn('row', styles.wrapper)} style={{ paddingLeft: '20px'}}>
2017-11-06 18:15:53 +01:00
<div className="col-md-2">
<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;
2017-11-06 18:15:53 +01:00
{this.props.item.nb_users > 1 ? (
'users'
) : (
'user'
)}
</div>
<div className="col-md-2">
<IcoContainer icons={icons} />
</div>
</div>
);
case 'providers':
2018-01-25 13:59:05 +01:00
icons.pop(); // Remove the icon-trash
2017-11-06 18:15:53 +01:00
return (
<div className={cn('row', styles.wrapper)}>
<div className="col-md-4">
<div className={styles.flex}>
<div>
2018-01-19 17:16:14 +01:00
<i className={`fa fa-${this.props.item.icon}`} />
2017-11-06 18:15:53 +01:00
</div>
<div>
{capitalize(this.props.item.name)}
2017-11-06 18:15:53 +01:00
</div>
</div>
</div>
2017-11-14 10:31:18 +01:00
<div className="col-md-6" style={{ fontWeight: '500' }}>
2018-01-22 11:00:41 +01:00
{get(this.props.values, [get(this.props.item, 'name'), 'enabled']) ? (
2017-11-06 18:15:53 +01:00
<span style={{ color: '#5A9E06' }}>Enabled</span>
) : (
<span style={{ color: '#F64D0A' }}>Disabled</span>
)}
</div>
<div className="col-md-2">
<IcoContainer icons={icons} />
</div>
</div>
);
case 'email-templates':
2018-01-25 13:59:05 +01:00
icons.pop();
2017-11-06 18:15:53 +01:00
return (
<div className={cn('row', styles.wrapper)}>
<div className="col-md-4">
<div className={styles.flex}>
<div>
2018-01-18 18:21:49 +01:00
<i className={`fa 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={`users-permissions.${this.props.item.display}`} />
): this.props.item.name}
2017-11-06 18:15:53 +01:00
</div>
</div>
</div>
<div className="col-md-8">
<IcoContainer icons={icons} />
</div>
</div>
);
default:
return '';
}
}
handleClick = () => {
switch (this.props.settingType) {
2017-11-27 17:50:51 +01:00
case 'roles': {
2018-01-24 13:51:22 +01:00
if (!includes(this.protectedRoleIDs, get(this.props.item, 'type', ''))) {
2017-11-27 17:50:51 +01:00
return router.push(`${router.location.pathname}/edit/${this.props.item.id}`);
}
return;
}
case 'providers':
case 'email-templates':
2018-01-19 13:34:55 +01:00
return this.context.setDataToEdit(this.props.item.name);
default:
return;
}
}
handleDelete = () => {
this.props.deleteData(this.props.item, this.props.settingType);
this.setState({ showModalDelete: false });
}
2017-11-06 18:15:53 +01:00
render() {
return (
<li className={styles.li} onClick={this.handleClick}>
2017-11-06 18:15:53 +01:00
<div className={styles.container}>
{this.generateContent()}
</div>
<PopUpWarning
isOpen={this.state.showModalDelete}
onConfirm={this.handleDelete}
toggleModal={() => this.setState({ showModalDelete: false })}
/>
2017-11-06 18:15:53 +01:00
</li>
);
}
}
2018-01-19 13:34:55 +01:00
ListRow.contextTypes = {
setDataToEdit: PropTypes.func.isRequired,
};
2017-11-06 18:15:53 +01:00
ListRow.defaultProps = {
item: {
name: 'Owner',
description: 'Rule them all. This role can\'t be deleted',
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;