UserGroupTable.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import React, { Fragment } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import dateFnsFormat from 'date-fns/format';
  5. import { createSubscribedElement } from '../../UnstatedUtils';
  6. import AppContainer from '../../../services/AppContainer';
  7. class UserGroupTable extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.xss = window.xss;
  11. this.state = {
  12. userGroups: this.props.userGroups,
  13. userGroupRelations: this.props.userGroupRelations,
  14. };
  15. this.onDelete = this.onDelete.bind(this);
  16. }
  17. componentWillReceiveProps(nextProps) {
  18. this.setState({
  19. userGroups: nextProps.userGroups,
  20. userGroupRelations: nextProps.userGroupRelations,
  21. });
  22. }
  23. onDelete(e) {
  24. const { target } = e;
  25. const groupId = target.getAttribute('data-user-group-id');
  26. const group = this.state.userGroups.find((group) => {
  27. return group._id === groupId;
  28. });
  29. this.props.onDelete(group);
  30. }
  31. render() {
  32. const { t } = this.props;
  33. return (
  34. <Fragment>
  35. <h2>{t('admin:user_group_management.group_list')}</h2>
  36. <table className="table table-bordered table-user-list">
  37. <thead>
  38. <tr>
  39. <th>{t('Name')}</th>
  40. <th>{t('User')}</th>
  41. <th width="100px">{t('Created')}</th>
  42. <th width="70px"></th>
  43. </tr>
  44. </thead>
  45. <tbody>
  46. {this.state.userGroups.map((group) => {
  47. return (
  48. <tr key={group._id}>
  49. {this.props.isAclEnabled
  50. ? (
  51. <td><a href={`/admin/user-group-detail/${group._id}`}>{this.xss.process(group.name)}</a></td>
  52. )
  53. : (
  54. <td>{this.xss.process(group.name)}</td>
  55. )
  56. }
  57. <td>
  58. <ul className="list-inline">
  59. {this.state.userGroupRelations[group._id].map((user) => {
  60. return <li key={user._id} className="list-inline-item badge badge-pill badge-warning">{this.xss.process(user.username)}</li>;
  61. })}
  62. </ul>
  63. </td>
  64. <td>{dateFnsFormat(new Date(group.createdAt), 'yyyy-MM-dd')}</td>
  65. {this.props.isAclEnabled
  66. ? (
  67. <td>
  68. <div className="btn-group admin-group-menu">
  69. <button
  70. type="button"
  71. id={`admin-group-menu-button-${group._id}`}
  72. className="btn btn-outline-secondary btn-sm dropdown-toggle"
  73. data-toggle="dropdown"
  74. >
  75. <i className="icon-settings"></i>
  76. </button>
  77. <div className="dropdown-menu" role="menu" aria-labelledby={`admin-group-menu-button-${group._id}`}>
  78. <a className="dropdown-item" href={`/admin/user-group-detail/${group._id}`}>
  79. <i className="icon-fw icon-note"></i> {t('Edit')}
  80. </a>
  81. <button className="dropdown-item" type="button" role="button" onClick={this.onDelete} data-user-group-id={group._id}>
  82. <i className="icon-fw icon-fire text-danger"></i> {t('Delete')}
  83. </button>
  84. </div>
  85. </div>
  86. </td>
  87. )
  88. : (
  89. <td></td>
  90. )
  91. }
  92. </tr>
  93. );
  94. })}
  95. </tbody>
  96. </table>
  97. </Fragment>
  98. );
  99. }
  100. }
  101. /**
  102. * Wrapper component for using unstated
  103. */
  104. const UserGroupTableWrapper = (props) => {
  105. return createSubscribedElement(UserGroupTable, props, [AppContainer]);
  106. };
  107. UserGroupTable.propTypes = {
  108. t: PropTypes.func.isRequired, // i18next
  109. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  110. userGroups: PropTypes.arrayOf(PropTypes.object).isRequired,
  111. userGroupRelations: PropTypes.object.isRequired,
  112. isAclEnabled: PropTypes.bool.isRequired,
  113. onDelete: PropTypes.func.isRequired,
  114. };
  115. export default withTranslation()(UserGroupTableWrapper);