UserTable.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 UserPicture from '../../User/UserPicture';
  6. import UserMenu from './UserMenu';
  7. import { createSubscribedElement } from '../../UnstatedUtils';
  8. import AppContainer from '../../../services/AppContainer';
  9. import AdminUsersContainer from '../../../services/AdminUsersContainer';
  10. class UserTable extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. };
  15. this.getUserStatusLabel = this.getUserStatusLabel.bind(this);
  16. }
  17. /**
  18. * return status label element by `userStatus`
  19. * @param {string} userStatus
  20. * @return status label element
  21. */
  22. getUserStatusLabel(userStatus) {
  23. let additionalClassName;
  24. let text;
  25. switch (userStatus) {
  26. case 1:
  27. additionalClassName = 'label-info';
  28. text = 'Approval Pending';
  29. break;
  30. case 2:
  31. additionalClassName = 'label-success';
  32. text = 'Active';
  33. break;
  34. case 3:
  35. additionalClassName = 'label-warning';
  36. text = 'Suspended';
  37. break;
  38. case 4:
  39. additionalClassName = 'label-danger';
  40. text = 'Deleted';
  41. break;
  42. case 5:
  43. additionalClassName = 'label-info';
  44. text = 'Invited';
  45. break;
  46. }
  47. return (
  48. <span className={`label ${additionalClassName}`}>
  49. {text}
  50. </span>
  51. );
  52. }
  53. /**
  54. * return admin label element by `isAdmin`
  55. * @param {string} isAdmin
  56. * @return admin label element
  57. */
  58. getUserAdminLabel(isAdmin) {
  59. const { t } = this.props;
  60. if (isAdmin) {
  61. return <span className="label label-inverse label-admin ml-2">{t('admin:user_management.user_table.administrator')}</span>;
  62. }
  63. }
  64. render() {
  65. const { t, adminUsersContainer } = this.props;
  66. return (
  67. <Fragment>
  68. <table className="table table-default table-bordered table-user-list">
  69. <thead>
  70. <tr>
  71. <th width="100px">#</th>
  72. <th>
  73. {t('status')}
  74. <a className="glyphicon glyphicon-triangle-top" aria-hidden="true"></a>
  75. <a className="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></a>
  76. </th>
  77. <th>
  78. <code>username</code>
  79. <a className="glyphicon glyphicon-triangle-top" aria-hidden="true"></a>
  80. <a className="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></a>
  81. </th>
  82. <th>
  83. {t('Name')}
  84. <a className="glyphicon glyphicon-triangle-top" aria-hidden="true"></a>
  85. <a className="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></a>
  86. </th>
  87. <th>
  88. {t('Email')}
  89. <a className="glyphicon glyphicon-triangle-top" aria-hidden="true"></a>
  90. <a className="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></a>
  91. </th>
  92. <th width="100px">
  93. {t('Created')}
  94. <a className="glyphicon glyphicon-triangle-top" aria-hidden="true"></a>
  95. <a className="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></a>
  96. </th>
  97. <th width="150px">{t('Last_Login')}</th>
  98. <th width="70px"></th>
  99. </tr>
  100. </thead>
  101. <tbody>
  102. {adminUsersContainer.state.users.map((user) => {
  103. return (
  104. <tr key={user._id}>
  105. <td>
  106. <UserPicture user={user} className="picture img-circle" />
  107. </td>
  108. <td>{this.getUserStatusLabel(user.status)} {this.getUserAdminLabel(user.admin)}</td>
  109. <td>
  110. <strong>{user.username}</strong>
  111. </td>
  112. <td>{user.name}</td>
  113. <td>{user.email}</td>
  114. <td>{dateFnsFormat(new Date(user.createdAt), 'yyyy-MM-dd')}</td>
  115. <td>
  116. {user.lastLoginAt && <span>{dateFnsFormat(new Date(user.lastLoginAt), 'yyyy-MM-dd HH:mm')}</span>}
  117. </td>
  118. <td>
  119. <UserMenu user={user} />
  120. </td>
  121. </tr>
  122. );
  123. })}
  124. </tbody>
  125. </table>
  126. </Fragment>
  127. );
  128. }
  129. }
  130. const UserTableWrapper = (props) => {
  131. return createSubscribedElement(UserTable, props, [AppContainer, AdminUsersContainer]);
  132. };
  133. UserTable.propTypes = {
  134. t: PropTypes.func.isRequired, // i18next
  135. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  136. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  137. };
  138. export default withTranslation()(UserTableWrapper);