You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserDeleteAccountModal.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import { connect } from 'react-redux';
  22. import { translate, translateWithParameters } from 'sonar-ui-common/helpers/l10n';
  23. import { Alert } from 'sonar-ui-common/components/ui/Alert';
  24. import InputValidationField from 'sonar-ui-common/components/controls/InputValidationField';
  25. import ValidationModal from 'sonar-ui-common/components/controls/ValidationModal';
  26. import UserDeleteAccountContent from './UserDeleteAccountContent';
  27. import RecentHistory from '../../../app/components/RecentHistory';
  28. import { deactivateUser } from '../../../api/users';
  29. import { Router, withRouter } from '../../../components/hoc/withRouter';
  30. import { doLogout } from '../../../store/rootActions';
  31. interface Values {
  32. login: string;
  33. }
  34. interface DeleteModalProps {
  35. doLogout: () => Promise<void>;
  36. label: string;
  37. organizationsSafeToDelete: T.Organization[];
  38. organizationsToTransferOrDelete: T.Organization[];
  39. router: Pick<Router, 'push'>;
  40. toggleModal: VoidFunction;
  41. user: T.LoggedInUser;
  42. }
  43. export class UserDeleteAccountModal extends React.PureComponent<DeleteModalProps> {
  44. handleSubmit = () => {
  45. const { user } = this.props;
  46. return deactivateUser({ login: user.login })
  47. .then(this.props.doLogout)
  48. .then(() => {
  49. RecentHistory.clear();
  50. window.location.replace('/account-deleted');
  51. });
  52. };
  53. handleValidate = ({ login }: Values) => {
  54. const { user } = this.props;
  55. const errors: { login?: string } = {};
  56. const trimmedLogin = login.trim();
  57. if (!trimmedLogin) {
  58. errors.login = translate('my_profile.delete_account.login.required');
  59. } else if (user.externalIdentity && trimmedLogin !== user.externalIdentity.trim()) {
  60. errors.login = translate('my_profile.delete_account.login.wrong_value');
  61. }
  62. return errors;
  63. };
  64. render() {
  65. const {
  66. label,
  67. organizationsSafeToDelete,
  68. organizationsToTransferOrDelete,
  69. toggleModal,
  70. user
  71. } = this.props;
  72. return (
  73. <ValidationModal
  74. confirmButtonText={translate('delete')}
  75. header={translateWithParameters(
  76. 'my_profile.delete_account.modal.header',
  77. label,
  78. user.externalIdentity || ''
  79. )}
  80. initialValues={{
  81. login: ''
  82. }}
  83. isDestructive={true}
  84. onClose={toggleModal}
  85. onSubmit={this.handleSubmit}
  86. validate={this.handleValidate}>
  87. {({ dirty, errors, handleBlur, handleChange, isSubmitting, touched, values }) => (
  88. <>
  89. <Alert className="big-spacer-bottom" variant="error">
  90. {translate('my_profile.warning_message')}
  91. </Alert>
  92. <UserDeleteAccountContent
  93. className="list-styled no-padding big-spacer-bottom"
  94. organizationsSafeToDelete={organizationsSafeToDelete}
  95. organizationsToTransferOrDelete={organizationsToTransferOrDelete}
  96. />
  97. <InputValidationField
  98. autoFocus={true}
  99. dirty={dirty}
  100. disabled={isSubmitting}
  101. error={errors.login}
  102. id="user-login"
  103. label={
  104. <label htmlFor="user-login">
  105. {translate('my_profile.delete_account.verify')}
  106. <em className="mandatory">*</em>
  107. </label>
  108. }
  109. name="login"
  110. onBlur={handleBlur}
  111. onChange={handleChange}
  112. touched={touched.login}
  113. type="text"
  114. value={values.login}
  115. />
  116. </>
  117. )}
  118. </ValidationModal>
  119. );
  120. }
  121. }
  122. const mapStateToProps = () => ({});
  123. const mapDispatchToProps = { doLogout: doLogout as any };
  124. export default connect(
  125. mapStateToProps,
  126. mapDispatchToProps
  127. )(withRouter(UserDeleteAccountModal));