accountClose.tsx 5.7 KB

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