/**
*
* ListRow
*
*/
import React from 'react';
import cn from 'classnames';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import { capitalize, get, includes } from 'lodash';
// Design
import { IcoContainer, PopUpWarning } from 'strapi-helper-plugin';
import en from '../../translations/en.json';
import styles from './styles.scss';
class ListRow extends React.Component {
// eslint-disable-line react/prefer-stateless-function
state = { showModalDelete: false };
// Roles that can't be deleted && modified
// Don't delete this line
protectedRoleIDs = [];
// Roles that can't be deleted;
undeletableIDs = ['public', 'authenticated'];
generateContent = () => {
let icons = [
{
icoType: 'pencil',
onClick: this.handleClick,
},
{
icoType: 'trash',
onClick: () => {
this.setState({ showModalDelete: true });
},
},
];
switch (this.props.settingType) {
case 'roles':
if (includes(this.protectedRoleIDs, get(this.props.item, 'type', ''))) {
icons = [];
}
if (includes(this.undeletableIDs, get(this.props.item, 'type', ''))) {
icons = [{ icoType: 'pencil', onClick: this.handleClick }];
}
return (
{this.props.item.name}
{this.props.item.description}
{this.props.item.nb_users || 0}
{this.props.item.nb_users > 1 ? 'users' : 'user'}
);
case 'providers':
icons.pop(); // Remove the icon-trash
return (
{capitalize(this.props.item.name)}
{get(this.props.values, [
get(this.props.item, 'name'),
'enabled',
]) ? (
Enabled
) : (
Disabled
)}
);
case 'email-templates':
icons.pop();
return (
{this.props.item.display && en[this.props.item.display] ? (
) : (
this.props.item.name
)}
);
default:
return '';
}
};
handleClick = () => {
const { pathname, push } = this.context;
switch (this.props.settingType) {
case 'roles': {
if (
!includes(this.protectedRoleIDs, get(this.props.item, 'type', ''))
) {
return push(`${pathname}/edit/${this.props.item.id}`);
}
return;
}
case 'providers':
this.context.emitEvent('willEditAuthenticationProvider');
return this.context.setDataToEdit(this.props.item.name);
case 'email-templates':
this.context.emitEvent('willEditEmailTemplates');
return this.context.setDataToEdit(this.props.item.name);
default:
return;
}
};
handleDelete = () => {
this.props.deleteData(this.props.item, this.props.settingType);
this.setState({ showModalDelete: false });
};
render() {
return (
{this.generateContent()}
this.setState({ showModalDelete: false })}
/>
);
}
}
ListRow.contextTypes = {
emitEvent: PropTypes.func,
pathname: PropTypes.string,
push: PropTypes.func,
setDataToEdit: PropTypes.func.isRequired,
};
ListRow.defaultProps = {
item: {
name: 'Owner',
description: 'Rule them all. This role can\'t be deleted',
nb_users: 1,
icon: 'envelope',
},
settingType: 'roles',
};
ListRow.propTypes = {
deleteData: PropTypes.func.isRequired,
item: PropTypes.object,
settingType: PropTypes.string,
values: PropTypes.object.isRequired,
};
export default ListRow;