UserManagement.jsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import React, { Fragment } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import PaginationWrapper from '../PaginationWrapper';
  5. import { createSubscribedElement } from '../UnstatedUtils';
  6. import { toastError } from '../../util/apiNotification';
  7. import AppContainer from '../../services/AppContainer';
  8. import AdminUsersContainer from '../../services/AdminUsersContainer';
  9. import PasswordResetModal from './Users/PasswordResetModal';
  10. import InviteUserControl from './Users/InviteUserControl';
  11. import UserTable from './Users/UserTable';
  12. class UserManagement extends React.Component {
  13. constructor(props) {
  14. super();
  15. this.state = {
  16. isNotifyCommentShow: false,
  17. };
  18. this.handlePage = this.handlePage.bind(this);
  19. this.handleChangeSearchText = this.handleChangeSearchText.bind(this);
  20. }
  21. componentWillMount() {
  22. this.handlePage(1);
  23. }
  24. async handlePage(selectedPage) {
  25. try {
  26. await this.props.adminUsersContainer.retrieveUsersByPagingNum(selectedPage);
  27. }
  28. catch (err) {
  29. toastError(err);
  30. }
  31. }
  32. /**
  33. * For checking same check box twice
  34. * @param {string} statusType
  35. */
  36. async handleClick(statusType) {
  37. const { adminUsersContainer } = this.props;
  38. if (!this.validateToggleStatus(statusType)) {
  39. return this.setState({ isNotifyCommentShow: true });
  40. }
  41. if (this.state.isNotifyCommentShow) {
  42. await this.setState({ isNotifyCommentShow: false });
  43. }
  44. adminUsersContainer.handleClick(statusType);
  45. }
  46. /**
  47. * Workaround user status check box
  48. * @param {string} statusType
  49. */
  50. validateToggleStatus(statusType) {
  51. if (this.props.adminUsersContainer.isSelected(statusType)) {
  52. return this.props.adminUsersContainer.state.selectedStatusList.size > 1;
  53. }
  54. return true;
  55. }
  56. /**
  57. * Reset button
  58. */
  59. resetButtonClickHandler() {
  60. const { adminUsersContainer } = this.props;
  61. try {
  62. adminUsersContainer.resetAllChanges();
  63. this.searchUserElement.value = '';
  64. this.state.isNotifyCommentShow = false;
  65. }
  66. catch (err) {
  67. toastError(err);
  68. }
  69. }
  70. /**
  71. * Workaround increamental search
  72. * @param {string} event
  73. */
  74. handleChangeSearchText(event) {
  75. this.props.adminUsersContainer.handleChangeSearchText(event.target.value);
  76. }
  77. render() {
  78. const { t, adminUsersContainer } = this.props;
  79. const pager = (
  80. <div className="pull-right">
  81. <PaginationWrapper
  82. activePage={adminUsersContainer.state.activePage}
  83. changePage={this.handlePage}
  84. totalItemsCount={adminUsersContainer.state.totalUsers}
  85. pagingLimit={adminUsersContainer.state.pagingLimit}
  86. />
  87. </div>
  88. );
  89. const clearButton = (
  90. adminUsersContainer.state.searchText.length > 0
  91. ? (
  92. <i
  93. className="icon-close search-clear"
  94. onClick={() => {
  95. adminUsersContainer.clearSearchText();
  96. this.searchUserElement.value = '';
  97. }}
  98. />
  99. )
  100. : ''
  101. );
  102. return (
  103. <Fragment>
  104. {adminUsersContainer.state.userForPasswordResetModal && <PasswordResetModal />}
  105. <p>
  106. <InviteUserControl />
  107. <a className="btn text-dark btn-outline-secondary ml-2" href="/admin/users/external-accounts" role="button">
  108. <i className="icon-user-follow" aria-hidden="true"></i>
  109. {t('admin:user_management.external_account')}
  110. </a>
  111. </p>
  112. <h2>{t('User_Management')}</h2>
  113. <div className="border-top border-bottom">
  114. <div className="d-flex justify-content-start align-items-center my-2">
  115. <div>
  116. <i className="icon-magnifier mr-1"></i>
  117. <span className="search-typeahead">
  118. <input
  119. type="text"
  120. ref={(searchUserElement) => { this.searchUserElement = searchUserElement }}
  121. onChange={this.handleChangeSearchText}
  122. />
  123. { clearButton }
  124. </span>
  125. </div>
  126. <div className="mx-5 form-inline">
  127. <div className="checkbox checkbox-primary pl-0">
  128. <input
  129. type="checkbox"
  130. id="c1"
  131. checked={adminUsersContainer.isSelected('all')}
  132. onClick={() => { this.handleClick('all') }}
  133. />
  134. <label htmlFor="c1">
  135. <span className="label label-primary d-inline-block vt mt-1">All</span>
  136. </label>
  137. </div>
  138. <div className="checkbox checkbox-info">
  139. <input
  140. type="checkbox"
  141. id="c2"
  142. checked={adminUsersContainer.isSelected('registered')}
  143. onClick={() => { this.handleClick('registered') }}
  144. />
  145. <label htmlFor="c2">
  146. <span className="label label-info d-inline-block vt mt-1">Approval Pending</span>
  147. </label>
  148. </div>
  149. <div className="checkbox checkbox-success">
  150. <input
  151. type="checkbox"
  152. id="c3"
  153. checked={adminUsersContainer.isSelected('active')}
  154. onClick={() => { this.handleClick('active') }}
  155. />
  156. <label htmlFor="c3">
  157. <span className="label label-success d-inline-block vt mt-1">Active</span>
  158. </label>
  159. </div>
  160. <div className="checkbox checkbox-warning">
  161. <input
  162. type="checkbox"
  163. id="c4"
  164. checked={adminUsersContainer.isSelected('suspended')}
  165. onClick={() => { this.handleClick('suspended') }}
  166. />
  167. <label htmlFor="c4">
  168. <span className="label label-warning d-inline-block vt mt-1">Suspended</span>
  169. </label>
  170. </div>
  171. <div className="checkbox checkbox-info">
  172. <input
  173. type="checkbox"
  174. id="c5"
  175. checked={adminUsersContainer.isSelected('invited')}
  176. onClick={() => { this.handleClick('invited') }}
  177. />
  178. <label htmlFor="c5">
  179. <span className="label label-info d-inline-block vt mt-1">Invited</span>
  180. </label>
  181. </div>
  182. </div>
  183. <div>
  184. <button
  185. type="button"
  186. className="btn btn-default btn-outline btn-sm"
  187. onClick={() => { this.resetButtonClickHandler() }}
  188. >
  189. <span
  190. className="icon-refresh mr-1"
  191. >
  192. </span>
  193. Reset
  194. </button>
  195. </div>
  196. <div className="ml-5">
  197. {this.state.isNotifyCommentShow && <span className="text-warning">{t('admin:user_management.click_twice_same_checkbox')}</span>}
  198. </div>
  199. </div>
  200. </div>
  201. {pager}
  202. <UserTable />
  203. {pager}
  204. </Fragment>
  205. );
  206. }
  207. }
  208. UserManagement.propTypes = {
  209. t: PropTypes.func.isRequired, // i18next
  210. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  211. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  212. };
  213. const UserManagementWrapper = (props) => {
  214. return createSubscribedElement(UserManagement, props, [AppContainer, AdminUsersContainer]);
  215. };
  216. export default withTranslation()(UserManagementWrapper);