UserRemoveButton.jsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import PropTypes from 'prop-types';
  4. import AdminUsersContainer from '~/client/services/AdminUsersContainer';
  5. import { toastError, toastSuccess } from '~/client/util/toastr';
  6. import { withUnstatedContainers } from '../../UnstatedUtils';
  7. class UserRemoveButton extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.onClickDeleteBtn = this.onClickDeleteBtn.bind(this);
  11. }
  12. async onClickDeleteBtn() {
  13. const { t } = this.props;
  14. try {
  15. await this.props.adminUsersContainer.removeUser(this.props.user._id);
  16. const { username } = this.props.user;
  17. toastSuccess(t('toaster.remove_user_success', { username }));
  18. } catch (err) {
  19. toastError(err);
  20. }
  21. }
  22. render() {
  23. const { t } = this.props;
  24. return (
  25. <button
  26. className="dropdown-item"
  27. type="button"
  28. onClick={() => {
  29. this.onClickDeleteBtn();
  30. }}
  31. >
  32. <span className="material-symbols-outlined text-danger">
  33. delete_forever
  34. </span>{' '}
  35. {t('Delete')}
  36. </button>
  37. );
  38. }
  39. }
  40. UserRemoveButton.propTypes = {
  41. t: PropTypes.func.isRequired, // i18next
  42. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  43. user: PropTypes.object.isRequired,
  44. };
  45. const UserRemoveButtonWrapperFC = (props) => {
  46. const { t } = useTranslation('admin');
  47. return <UserRemoveButton t={t} {...props} />;
  48. };
  49. /**
  50. * Wrapper component for using unstated
  51. * @type {React.ComponentType<{ user: import('@growi/core').IUserHasId }>}
  52. */
  53. const UserRemoveButtonWrapper = withUnstatedContainers(
  54. UserRemoveButtonWrapperFC,
  55. [AdminUsersContainer],
  56. );
  57. export default UserRemoveButtonWrapper;