accountClose.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import styled from '@emotion/styled';
  2. import {addErrorMessage, addLoadingMessage} from 'sentry/actionCreators/indicator';
  3. import {ModalRenderProps, openModal} from 'sentry/actionCreators/modal';
  4. import {Alert} from 'sentry/components/alert';
  5. import {Button} from 'sentry/components/button';
  6. import HookOrDefault from 'sentry/components/hookOrDefault';
  7. import {
  8. Panel,
  9. PanelAlert,
  10. PanelBody,
  11. PanelHeader,
  12. PanelItem,
  13. } from 'sentry/components/panels';
  14. import {t, tct} from 'sentry/locale';
  15. import {Organization} from 'sentry/types';
  16. import AsyncView from 'sentry/views/asyncView';
  17. import {ConfirmAccountClose} from 'sentry/views/settings/account/confirmAccountClose';
  18. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  19. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  20. const BYE_URL = '/';
  21. const leaveRedirect = () => (window.location.href = BYE_URL);
  22. const Important = styled('div')`
  23. font-weight: bold;
  24. font-size: 1.2em;
  25. `;
  26. const GoodbyeModalContent = ({Header, Body, Footer}: ModalRenderProps) => (
  27. <div>
  28. <Header>{t('Closing Account')}</Header>
  29. <Body>
  30. <TextBlock>
  31. {t('Your account has been deactivated and scheduled for removal.')}
  32. </TextBlock>
  33. <TextBlock>
  34. {t('Thanks for using Sentry! We hope to see you again soon!')}
  35. </TextBlock>
  36. </Body>
  37. <Footer>
  38. <Button href={BYE_URL}>{t('Goodbye')}</Button>
  39. </Footer>
  40. </div>
  41. );
  42. type OwnedOrg = {
  43. organization: Organization;
  44. singleOwner: boolean;
  45. };
  46. type Props = AsyncView['props'];
  47. type State = AsyncView['state'] & {
  48. organizations: OwnedOrg[] | null;
  49. /**
  50. * Org slugs that will be removed
  51. */
  52. orgsToRemove: Set<string> | null;
  53. };
  54. class AccountClose extends AsyncView<Props, State> {
  55. leaveRedirectTimeout: number | undefined = undefined;
  56. componentWillUnmount() {
  57. window.clearTimeout(this.leaveRedirectTimeout);
  58. }
  59. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  60. return [['organizations', '/organizations/?owner=1']];
  61. }
  62. getDefaultState() {
  63. return {
  64. ...super.getDefaultState(),
  65. orgsToRemove: null,
  66. };
  67. }
  68. get singleOwnerOrgs() {
  69. return this.state.organizations
  70. ?.filter(({singleOwner}) => singleOwner)
  71. ?.map(({organization}) => organization.slug);
  72. }
  73. handleChange = (
  74. {slug}: Organization,
  75. isSingle: boolean,
  76. event: React.ChangeEvent<HTMLInputElement>
  77. ) => {
  78. const checked = event.target.checked;
  79. // Can't unselect an org where you are the single owner
  80. if (isSingle) {
  81. return;
  82. }
  83. this.setState(state => {
  84. const set = state.orgsToRemove || new Set(this.singleOwnerOrgs);
  85. if (checked) {
  86. set.add(slug);
  87. } else {
  88. set.delete(slug);
  89. }
  90. return {orgsToRemove: set};
  91. });
  92. };
  93. get orgSlugsToRemove() {
  94. const {orgsToRemove} = this.state;
  95. return (
  96. (orgsToRemove === null ? this.singleOwnerOrgs : Array.from(orgsToRemove)) || []
  97. );
  98. }
  99. handleRemoveAccount = async () => {
  100. const orgs = this.orgSlugsToRemove;
  101. addLoadingMessage('Closing account\u2026');
  102. try {
  103. await this.api.requestPromise('/users/me/', {
  104. method: 'DELETE',
  105. data: {organizations: orgs},
  106. });
  107. openModal(GoodbyeModalContent, {
  108. onClose: leaveRedirect,
  109. });
  110. // Redirect after 10 seconds
  111. window.clearTimeout(this.leaveRedirectTimeout);
  112. this.leaveRedirectTimeout = window.setTimeout(leaveRedirect, 10000);
  113. } catch {
  114. addErrorMessage('Error closing account');
  115. }
  116. };
  117. renderBody() {
  118. const {organizations, orgsToRemove} = this.state;
  119. const HookedCustomConfirmAccountClose = HookOrDefault({
  120. hookName: 'component:confirm-account-close',
  121. defaultComponent: props => <ConfirmAccountClose {...props} />,
  122. });
  123. return (
  124. <div>
  125. <SettingsPageHeader title="Close Account" />
  126. <TextBlock>
  127. {t('This will permanently remove all associated data for your user')}.
  128. </TextBlock>
  129. <Alert type="error" showIcon>
  130. <Important>
  131. {t('Closing your account is permanent and cannot be undone')}!
  132. </Important>
  133. </Alert>
  134. <Panel>
  135. <PanelHeader>{t('Remove the following organizations')}</PanelHeader>
  136. <PanelBody>
  137. <PanelAlert type="info">
  138. {t(
  139. 'Ownership will remain with other organization owners if an organization is not deleted.'
  140. )}
  141. <br />
  142. {tct(
  143. "Boxes which can't be unchecked mean that you are the only organization owner and the organization [strong:will be deleted].",
  144. {strong: <strong />}
  145. )}
  146. </PanelAlert>
  147. {organizations?.map(({organization, singleOwner}) => (
  148. <PanelItem key={organization.slug}>
  149. <label>
  150. <input
  151. style={{marginRight: 6}}
  152. type="checkbox"
  153. value={organization.slug}
  154. onChange={this.handleChange.bind(this, organization, singleOwner)}
  155. name="organizations"
  156. checked={
  157. orgsToRemove === null
  158. ? singleOwner
  159. : orgsToRemove.has(organization.slug)
  160. }
  161. disabled={singleOwner}
  162. />
  163. {organization.slug}
  164. </label>
  165. </PanelItem>
  166. ))}
  167. </PanelBody>
  168. </Panel>
  169. <HookedCustomConfirmAccountClose
  170. handleRemoveAccount={this.handleRemoveAccount}
  171. organizationSlugs={this.orgSlugsToRemove}
  172. />
  173. </div>
  174. );
  175. }
  176. }
  177. export default AccountClose;