UserGroupTable.jsx 3.9 KB

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