UserRemoveButton.jsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 { toastSuccess, toastError } 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. }
  19. catch (err) {
  20. toastError(err);
  21. }
  22. }
  23. render() {
  24. const { t } = this.props;
  25. return (
  26. <button className="dropdown-item" type="button" onClick={() => { this.onClickDeleteBtn() }}>
  27. <span className="material-symbols-outlined text-danger">delete_forever</span> {t('Delete')}
  28. </button>
  29. );
  30. }
  31. }
  32. UserRemoveButton.propTypes = {
  33. t: PropTypes.func.isRequired, // i18next
  34. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  35. user: PropTypes.object.isRequired,
  36. };
  37. const UserRemoveButtonWrapperFC = (props) => {
  38. const { t } = useTranslation('admin');
  39. return <UserRemoveButton t={t} {...props} />;
  40. };
  41. /**
  42. * Wrapper component for using unstated
  43. */
  44. const UserRemoveButtonWrapper = withUnstatedContainers(UserRemoveButtonWrapperFC, [AdminUsersContainer]);
  45. export default UserRemoveButtonWrapper;