accountSubscriptions.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import groupBy from 'lodash/groupBy';
  4. import orderBy from 'lodash/orderBy';
  5. import moment from 'moment';
  6. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  7. import DateTime from 'sentry/components/dateTime';
  8. import EmptyMessage from 'sentry/components/emptyMessage';
  9. import {Panel, PanelBody, PanelHeader, PanelItem} from 'sentry/components/panels';
  10. import Switch from 'sentry/components/switchButton';
  11. import {IconToggle} from 'sentry/icons';
  12. import {t, tct} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import AsyncView from 'sentry/views/asyncView';
  15. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  16. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  17. const ENDPOINT = '/users/me/subscriptions/';
  18. type Subscription = {
  19. email: string;
  20. listDescription: string;
  21. listId: number;
  22. listName: string;
  23. subscribed: boolean;
  24. subscribedDate: string | null;
  25. unsubscribedDate: string | null;
  26. };
  27. type State = AsyncView['state'] & {
  28. subscriptions: Subscription[];
  29. };
  30. class AccountSubscriptions extends AsyncView<AsyncView['props'], State> {
  31. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  32. return [['subscriptions', ENDPOINT]];
  33. }
  34. getTitle() {
  35. return t('Subscriptions');
  36. }
  37. handleToggle = (subscription: Subscription, listId: number, _e: React.MouseEvent) => {
  38. const subscribed = !subscription.subscribed;
  39. const oldSubscriptions = this.state.subscriptions;
  40. this.setState(state => {
  41. const newSubscriptions = state.subscriptions.slice();
  42. const index = newSubscriptions.findIndex(sub => sub.listId === listId);
  43. newSubscriptions[index] = {
  44. ...subscription,
  45. subscribed,
  46. subscribedDate: new Date().toString(),
  47. };
  48. return {
  49. ...state,
  50. subscriptions: newSubscriptions,
  51. };
  52. });
  53. this.api.request(ENDPOINT, {
  54. method: 'PUT',
  55. data: {
  56. listId: subscription.listId,
  57. subscribed,
  58. },
  59. success: () => {
  60. addSuccessMessage(
  61. `${subscribed ? 'Subscribed' : 'Unsubscribed'} to ${subscription.listName}`
  62. );
  63. },
  64. error: () => {
  65. addErrorMessage(
  66. `Unable to ${subscribed ? '' : 'un'}subscribe to ${subscription.listName}`
  67. );
  68. this.setState({subscriptions: oldSubscriptions});
  69. },
  70. });
  71. };
  72. renderBody() {
  73. // Group by does not guarantee order, so we sort by email
  74. const subGroups = orderBy(
  75. Object.entries(groupBy(this.state.subscriptions, sub => sub.email)),
  76. ([email]) => email
  77. );
  78. return (
  79. <div>
  80. <SettingsPageHeader title={this.getTitle()} />
  81. <TextBlock>
  82. {t(`Sentry is committed to respecting your inbox. Our goal is to
  83. provide useful content and resources that make fixing errors less
  84. painful. Enjoyable even.`)}
  85. </TextBlock>
  86. <TextBlock>
  87. {t(`As part of our compliance with the EU’s General Data Protection
  88. Regulation (GDPR), starting on 25 May 2018, we’ll only email you
  89. according to the marketing categories to which you’ve explicitly
  90. opted-in.`)}
  91. </TextBlock>
  92. <Panel>
  93. {this.state.subscriptions.length ? (
  94. <div>
  95. <PanelHeader>{t('Subscription')}</PanelHeader>
  96. <PanelBody>
  97. {subGroups.map(([email, subscriptions]) => (
  98. <Fragment key={email}>
  99. {subGroups.length > 1 && (
  100. <Heading>
  101. <IconToggle /> {t('Subscriptions for %s', email)}
  102. </Heading>
  103. )}
  104. {subscriptions.map(subscription => (
  105. <PanelItem center key={subscription.listId}>
  106. <SubscriptionDetails>
  107. <SubscriptionName>{subscription.listName}</SubscriptionName>
  108. {subscription.listDescription && (
  109. <Description>{subscription.listDescription}</Description>
  110. )}
  111. {subscription.subscribed ? (
  112. <SubscribedDescription>
  113. <div>
  114. {tct('[email] on [date]', {
  115. email: subscription.email,
  116. date: (
  117. <DateTime
  118. date={moment(subscription.subscribedDate!)}
  119. />
  120. ),
  121. })}
  122. </div>
  123. </SubscribedDescription>
  124. ) : (
  125. <SubscribedDescription>
  126. {t('Not currently subscribed')}
  127. </SubscribedDescription>
  128. )}
  129. </SubscriptionDetails>
  130. <div>
  131. <Switch
  132. isActive={subscription.subscribed}
  133. size="lg"
  134. toggle={this.handleToggle.bind(
  135. this,
  136. subscription,
  137. subscription.listId
  138. )}
  139. />
  140. </div>
  141. </PanelItem>
  142. ))}
  143. </Fragment>
  144. ))}
  145. </PanelBody>
  146. </div>
  147. ) : (
  148. <EmptyMessage>{t("There's no subscription backend present.")}</EmptyMessage>
  149. )}
  150. </Panel>
  151. <TextBlock>
  152. {t(`We’re applying GDPR consent and privacy policies to all Sentry
  153. contacts, regardless of location. You’ll be able to manage your
  154. subscriptions here and from an Unsubscribe link in the footer of
  155. all marketing emails.`)}
  156. </TextBlock>
  157. <TextBlock>
  158. {tct(
  159. 'Please contact [email:learn@sentry.io] with any questions or suggestions.',
  160. {email: <a href="mailto:learn@sentry.io" />}
  161. )}
  162. </TextBlock>
  163. </div>
  164. );
  165. }
  166. }
  167. const Heading = styled(PanelItem)`
  168. display: grid;
  169. grid-template-columns: max-content 1fr;
  170. gap: ${space(1)};
  171. align-items: center;
  172. font-size: ${p => p.theme.fontSizeMedium};
  173. padding: ${space(1.5)} ${space(2)};
  174. background: ${p => p.theme.backgroundSecondary};
  175. color: ${p => p.theme.subText};
  176. `;
  177. const SubscriptionDetails = styled('div')`
  178. width: 50%;
  179. padding-right: ${space(2)};
  180. `;
  181. const SubscriptionName = styled('div')`
  182. font-size: ${p => p.theme.fontSizeMedium};
  183. `;
  184. const Description = styled('div')`
  185. font-size: ${p => p.theme.fontSizeSmall};
  186. color: ${p => p.theme.subText};
  187. margin-top: ${space(0.5)};
  188. `;
  189. const SubscribedDescription = styled(Description)`
  190. color: ${p => p.theme.subText};
  191. `;
  192. export default AccountSubscriptions;