import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import dateFnsFormat from 'date-fns/format'; import { withUnstatedContainers } from '../../UnstatedUtils'; import AppContainer from '../../../services/AppContainer'; class UserGroupTable extends React.Component { constructor(props) { super(props); this.xss = window.xss; this.state = { userGroups: this.props.userGroups, userGroupMap: {}, }; this.generateUserGroupMap = this.generateUserGroupMap.bind(this); this.onDelete = this.onDelete.bind(this); } componentWillMount() { const userGroupMap = this.generateUserGroupMap(this.props.userGroups, this.props.userGroupRelations); this.setState({ userGroupMap }); } componentWillReceiveProps(nextProps) { const { userGroups, userGroupRelations } = nextProps; const userGroupMap = this.generateUserGroupMap(userGroups, userGroupRelations); this.setState({ userGroups, userGroupMap, }); } generateUserGroupMap(userGroups, userGroupRelations) { const userGroupMap = {}; userGroupRelations.forEach((relation) => { const group = relation.relatedGroup; const users = userGroupMap[group] || []; users.push(relation.relatedUser); // register userGroupMap[group] = users; }); return userGroupMap; } onDelete(e) { const { target } = e; const groupId = target.getAttribute('data-user-group-id'); const group = this.state.userGroups.find((group) => { return group._id === groupId; }); this.props.onDelete(group); } render() { const { t } = this.props; return (

{t('admin:user_group_management.group_list')}

{this.state.userGroups.map((group) => { const users = this.state.userGroupMap[group._id]; return ( {this.props.isAclEnabled ? ( ) : ( ) } {this.props.isAclEnabled ? ( ) : ( ) } ); })}
{t('Name')} {t('User')} {t('Created')}
{this.xss.process(group.name)}{this.xss.process(group.name)}
    {users != null && users.map((user) => { return
  • {this.xss.process(user.username)}
  • ; })}
{dateFnsFormat(new Date(group.createdAt), 'yyyy-MM-dd')}
{t('Edit')}
); } } /** * Wrapper component for using unstated */ const UserGroupTableWrapper = withUnstatedContainers(UserGroupTable, [AppContainer]); UserGroupTable.propTypes = { t: PropTypes.func.isRequired, // i18next appContainer: PropTypes.instanceOf(AppContainer).isRequired, userGroups: PropTypes.arrayOf(PropTypes.object).isRequired, userGroupRelations: PropTypes.arrayOf(PropTypes.object).isRequired, isAclEnabled: PropTypes.bool.isRequired, onDelete: PropTypes.func.isRequired, }; export default withTranslation()(UserGroupTableWrapper);