RemoveAdminButton.jsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 { toastSuccess, toastError } from '../../../util/apiNotification';
  7. import AdminUsersContainer from '../../../services/AdminUsersContainer';
  8. class RemoveAdminButton extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. this.onClickRemoveAdminBtn = this.onClickRemoveAdminBtn.bind(this);
  12. }
  13. async onClickRemoveAdminBtn() {
  14. const { t } = this.props;
  15. try {
  16. const username = await this.props.adminUsersContainer.removeUserAdmin(this.props.user._id);
  17. toastSuccess(t('toaster.remove_user_admin', { username }));
  18. }
  19. catch (err) {
  20. toastError(err);
  21. }
  22. }
  23. renderRemoveAdminBtn() {
  24. const { t } = this.props;
  25. return (
  26. <a className="dropdown-item" href="" onClick={() => { this.onClickRemoveAdminBtn() }}>
  27. <i className="icon-fw icon-user-unfollow"></i> {t('admin:user_management.user_table.remove_admin_access')}
  28. </a>
  29. );
  30. }
  31. renderRemoveAdminAlert() {
  32. const { t } = this.props;
  33. return (
  34. <div className="px-4">
  35. <i className="icon-fw icon-user-unfollow mb-2"></i>{t('admin:user_management.user_table.remove_admin_access')}
  36. <p className="alert alert-danger">{t('admin:user_management.user_table.cannot_remove')}</p>
  37. </div>
  38. );
  39. }
  40. render() {
  41. const { user } = this.props;
  42. const { currentUsername } = this.props.appContainer;
  43. return (
  44. <Fragment>
  45. {user.username !== currentUsername ? this.renderRemoveAdminBtn()
  46. : this.renderRemoveAdminAlert()}
  47. </Fragment>
  48. );
  49. }
  50. }
  51. /**
  52. * Wrapper component for using unstated
  53. */
  54. const RemoveAdminButtonWrapper = (props) => {
  55. return createSubscribedElement(RemoveAdminButton, props, [AppContainer, AdminUsersContainer]);
  56. };
  57. RemoveAdminButton.propTypes = {
  58. t: PropTypes.func.isRequired, // i18next
  59. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  60. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  61. user: PropTypes.object.isRequired,
  62. };
  63. export default withTranslation()(RemoveAdminButtonWrapper);