UserGroupUserFormByClick.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React, { Fragment } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { createSubscribedElement } from '../../UnstatedUtils';
  5. import AppContainer from '../../../services/AppContainer';
  6. import UserGroupDetailContainer from '../../../services/UserGroupDetailContainer';
  7. import { toastSuccess, toastError } from '../../../util/apiNotification';
  8. class UserGroupUserFormByClick extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. this.xss = window.xss;
  12. this.addUserByClick = this.addUserByClick.bind(this);
  13. }
  14. async addUserByClick(username) {
  15. try {
  16. await this.props.userGroupDetailContainer.addUserByUsername(username);
  17. toastSuccess(`Added "${this.xss.process(username)}" to "${this.xss.process(this.props.userGroupDetailContainer.state.userGroup.name)}"`);
  18. }
  19. catch (err) {
  20. toastError(new Error(`Unable to add "${this.xss.process(username)}" to "${this.xss.process(this.props.userGroupDetailContainer.userGroup.name)}"`));
  21. }
  22. }
  23. render() {
  24. // eslint-disable-next-line no-unused-vars
  25. const { t, userGroupDetailContainer } = this.props;
  26. return (
  27. <Fragment>
  28. <ul className="list-inline">
  29. {userGroupDetailContainer.state.unrelatedUsers.map((user) => {
  30. return (
  31. <li key={user._id}>
  32. <button type="submit" className="btn btn-xs btn-primary" onClick={() => { return this.addUserByClick(user.username) }}>
  33. {user.username}
  34. </button>
  35. </li>
  36. );
  37. })}
  38. </ul>
  39. {userGroupDetailContainer.state.unrelatedUsers.length === 0 ? 'No users available.' : null}
  40. </Fragment>
  41. );
  42. }
  43. }
  44. UserGroupUserFormByClick.propTypes = {
  45. t: PropTypes.func.isRequired, // i18next
  46. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  47. userGroupDetailContainer: PropTypes.instanceOf(UserGroupDetailContainer).isRequired,
  48. };
  49. /**
  50. * Wrapper component for using unstated
  51. */
  52. const UserGroupUserFormByClickWrapper = (props) => {
  53. return createSubscribedElement(UserGroupUserFormByClick, props, [AppContainer, UserGroupDetailContainer]);
  54. };
  55. export default withTranslation()(UserGroupUserFormByClickWrapper);