UserGroupUserTable.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import dateFnsFormat from 'date-fns/format';
  5. import UserPicture from '../../User/UserPicture';
  6. import { createSubscribedElement } from '../../UnstatedUtils';
  7. import AppContainer from '../../../services/AppContainer';
  8. import UserGroupDetailContainer from '../../../services/UserGroupDetailContainer';
  9. import { toastSuccess, toastError } from '../../../util/apiNotification';
  10. class UserGroupUserTable extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.xss = window.xss;
  14. this.removeUser = this.removeUser.bind(this);
  15. }
  16. async removeUser(username) {
  17. try {
  18. await this.props.userGroupDetailContainer.removeUserByUsername(username);
  19. toastSuccess(`Removed "${this.xss.process(username)}" from "${this.xss.process(this.props.userGroupDetailContainer.state.userGroup.name)}"`);
  20. }
  21. catch (err) {
  22. // eslint-disable-next-line max-len
  23. toastError(new Error(`Unable to remove "${this.xss.process(username)}" from "${this.xss.process(this.props.userGroupDetailContainer.state.userGroup.name)}"`));
  24. }
  25. }
  26. render() {
  27. const { t, userGroupDetailContainer } = this.props;
  28. return (
  29. <table className="table table-bordered table-user-list">
  30. <thead>
  31. <tr>
  32. <th width="100px">#</th>
  33. <th>
  34. {t('username')}
  35. </th>
  36. <th>{t('Name')}</th>
  37. <th width="100px">{t('Created')}</th>
  38. <th width="160px">{t('Last_Login')}</th>
  39. <th width="70px"></th>
  40. </tr>
  41. </thead>
  42. <tbody>
  43. {userGroupDetailContainer.state.userGroupRelations.map((sRelation) => {
  44. const { relatedUser } = sRelation;
  45. return (
  46. <tr key={sRelation._id}>
  47. <td>
  48. <UserPicture user={relatedUser} className="picture img-circle" />
  49. </td>
  50. <td>
  51. <strong>{relatedUser.username}</strong>
  52. </td>
  53. <td>{relatedUser.name}</td>
  54. <td>{relatedUser.createdAt ? dateFnsFormat(new Date(relatedUser.createdAt), 'yyyy-MM-dd') : ''}</td>
  55. <td>{relatedUser.lastLoginAt ? dateFnsFormat(new Date(relatedUser.lastLoginAt), 'yyyy-MM-dd HH:mm:ss') : ''}</td>
  56. <td>
  57. <div className="btn-group admin-user-menu">
  58. <button type="button" className="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
  59. <i className="icon-settings"></i> <span className="caret"></span>
  60. </button>
  61. <ul className="dropdown-menu" role="menu">
  62. <li>
  63. <a onClick={() => { return this.removeUser(relatedUser.username) }}>
  64. <i className="icon-fw icon-user-unfollow"></i> {t('admin:user_group_management.remove_from_group')}
  65. </a>
  66. </li>
  67. </ul>
  68. </div>
  69. </td>
  70. </tr>
  71. );
  72. })}
  73. <tr>
  74. <td></td>
  75. <td className="text-center">
  76. <button className="btn btn-default" type="button" onClick={userGroupDetailContainer.openUserGroupUserModal}>
  77. <i className="ti-plus"></i>
  78. </button>
  79. </td>
  80. <td></td>
  81. <td></td>
  82. <td></td>
  83. <td></td>
  84. </tr>
  85. </tbody>
  86. </table>
  87. );
  88. }
  89. }
  90. UserGroupUserTable.propTypes = {
  91. t: PropTypes.func.isRequired, // i18next
  92. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  93. userGroupDetailContainer: PropTypes.instanceOf(UserGroupDetailContainer).isRequired,
  94. };
  95. /**
  96. * Wrapper component for using unstated
  97. */
  98. const UserGroupUserTableWrapper = (props) => {
  99. return createSubscribedElement(UserGroupUserTable, props, [AppContainer, UserGroupDetailContainer]);
  100. };
  101. export default withTranslation()(UserGroupUserTableWrapper);