UserRemoveButton.jsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { useTranslation } from 'react-i18next';
  4. import AdminUsersContainer from '~/client/services/AdminUsersContainer';
  5. import AppContainer from '~/client/services/AppContainer';
  6. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  7. import { withUnstatedContainers } from '../../UnstatedUtils';
  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('toaster.remove_user_success', { username }));
  19. }
  20. catch (err) {
  21. toastError(err);
  22. }
  23. }
  24. render() {
  25. const { t } = this.props;
  26. return (
  27. <button className="dropdown-item" type="button" onClick={() => { this.onClickDeleteBtn() }}>
  28. <i className="icon-fw icon-fire text-danger"></i> {t('Delete')}
  29. </button>
  30. );
  31. }
  32. }
  33. UserRemoveButton.propTypes = {
  34. t: PropTypes.func.isRequired, // i18next
  35. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  36. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  37. user: PropTypes.object.isRequired,
  38. };
  39. const UserRemoveButtonWrapperFC = (props) => {
  40. const { t } = useTranslation();
  41. return <UserRemoveButton t={t} {...props} />;
  42. };
  43. /**
  44. * Wrapper component for using unstated
  45. */
  46. const UserRemoveButtonWrapper = withUnstatedContainers(UserRemoveButtonWrapperFC, [AppContainer, AdminUsersContainer]);
  47. export default UserRemoveButtonWrapper;