RemoveAdminButton.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import React, { Fragment } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { withUnstatedContainers } from '../../UnstatedUtils';
  5. import AppContainer from '~/client/services/AppContainer';
  6. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  7. import AdminUsersContainer from '~/client/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. <button className="dropdown-item" type="button" onClick={() => { this.onClickRemoveAdminBtn() }}>
  27. <i className="icon-fw icon-user-unfollow"></i> {t('admin:user_management.user_table.remove_admin_access')}
  28. </button>
  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 = withUnstatedContainers(RemoveAdminButton, [AppContainer, AdminUsersContainer]);
  55. RemoveAdminButton.propTypes = {
  56. t: PropTypes.func.isRequired, // i18next
  57. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  58. adminUsersContainer: PropTypes.instanceOf(AdminUsersContainer).isRequired,
  59. user: PropTypes.object.isRequired,
  60. };
  61. export default withTranslation()(RemoveAdminButtonWrapper);