UserGroupTable.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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('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-primary">{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 type="button" className="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
  70. <i className="icon-settings"></i> <span className="caret"></span>
  71. </button>
  72. <ul className="dropdown-menu" role="menu">
  73. <li>
  74. <a href={`/admin/user-group-detail/${group._id}`}>
  75. <i className="icon-fw icon-note"></i> { t('Edit') }
  76. </a>
  77. </li>
  78. <li>
  79. <a href="#" onClick={this.onDelete} data-user-group-id={group._id}>
  80. <i className="icon-fw icon-fire text-danger"></i> { t('Delete') }
  81. </a>
  82. </li>
  83. </ul>
  84. </div>
  85. </td>
  86. )
  87. : (
  88. <td></td>
  89. )
  90. }
  91. </tr>
  92. );
  93. })}
  94. </tbody>
  95. </table>
  96. </Fragment>
  97. );
  98. }
  99. }
  100. /**
  101. * Wrapper component for using unstated
  102. */
  103. const UserGroupTableWrapper = (props) => {
  104. return createSubscribedElement(UserGroupTable, props, [AppContainer]);
  105. };
  106. UserGroupTable.propTypes = {
  107. t: PropTypes.func.isRequired, // i18next
  108. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  109. userGroups: PropTypes.arrayOf(PropTypes.object).isRequired,
  110. userGroupRelations: PropTypes.object.isRequired,
  111. isAclEnabled: PropTypes.bool,
  112. onDelete: PropTypes.func.isRequired,
  113. };
  114. export default withTranslation()(UserGroupTableWrapper);