UserRemoveButton.jsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import React 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 AdminUsersContainer from '../../../services/AdminUsersContainer';
  7. import { toastSuccess, toastError } from '../../../util/apiNotification';
  8. class UserRemoveButton extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. this.onClickDeleteBtn = this.onClickDeleteBtn.bind(this);
  12. }
  13. async onClickDeleteBtn() {
  14. const { t } = this.props;
  15. try {
  16. await this.props.adminUsersContainer.removeUser(this.props.user._id);
  17. const { username } = this.props.user;
  18. toastSuccess(t('user_management.remove_user_success', { username }));
  19. }
  20. catch (err) {
  21. toastError(err);
  22. }
  23. }
  24. render() {
  25. const { t } = this.props;
  26. return (
  27. <a className="px-4" onClick={() => { this.onClickDeleteBtn() }}>
  28. <i className="icon-fw icon-fire text-danger"></i> { t('Delete') }
  29. </a>
  30. );
  31. }
  32. }
  33. /**
  34. * Wrapper component for using unstated
  35. */
  36. const UserRemoveButtonWrapper = (props) => {
  37. return createSubscribedElement(UserRemoveButton, props, [AppContainer, AdminUsersContainer]);
  38. };
  39. UserRemoveButton.propTypes = {
  40. t: PropTypes.func.isRequired, // i18next
  41. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  42. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  43. user: PropTypes.object.isRequired,
  44. };
  45. export default withTranslation()(UserRemoveButtonWrapper);