accountSubscriptions.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import findIndex from 'lodash/findIndex';
  4. import groupBy from 'lodash/groupBy';
  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 '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 = findIndex(newSubscriptions, {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. const subGroups = Object.entries(groupBy(this.state.subscriptions, sub => sub.email));
  74. return (
  75. <div>
  76. <SettingsPageHeader title="Subscriptions" />
  77. <TextBlock>
  78. {t(`Sentry is committed to respecting your inbox. Our goal is to
  79. provide useful content and resources that make fixing errors less
  80. painful. Enjoyable even.`)}
  81. </TextBlock>
  82. <TextBlock>
  83. {t(`As part of our compliance with the EU’s General Data Protection
  84. Regulation (GDPR), starting on 25 May 2018, we’ll only email you
  85. according to the marketing categories to which you’ve explicitly
  86. opted-in.`)}
  87. </TextBlock>
  88. <Panel>
  89. {this.state.subscriptions.length ? (
  90. <div>
  91. <PanelHeader>{t('Subscription')}</PanelHeader>
  92. <PanelBody>
  93. {subGroups.map(([email, subscriptions]) => (
  94. <Fragment key={email}>
  95. {subGroups.length > 1 && (
  96. <Heading>
  97. <IconToggle /> {t('Subscriptions for %s', email)}
  98. </Heading>
  99. )}
  100. {subscriptions.map(subscription => (
  101. <PanelItem center key={subscription.listId}>
  102. <SubscriptionDetails>
  103. <SubscriptionName>{subscription.listName}</SubscriptionName>
  104. {subscription.listDescription && (
  105. <Description>{subscription.listDescription}</Description>
  106. )}
  107. {subscription.subscribed ? (
  108. <SubscribedDescription>
  109. <div>
  110. {tct('[email] on [date]', {
  111. email: subscription.email,
  112. date: (
  113. <DateTime
  114. date={moment(subscription.subscribedDate!)}
  115. />
  116. ),
  117. })}
  118. </div>
  119. </SubscribedDescription>
  120. ) : (
  121. <SubscribedDescription>
  122. {t('Not currently subscribed')}
  123. </SubscribedDescription>
  124. )}
  125. </SubscriptionDetails>
  126. <div>
  127. <Switch
  128. isActive={subscription.subscribed}
  129. size="lg"
  130. toggle={this.handleToggle.bind(
  131. this,
  132. subscription,
  133. subscription.listId
  134. )}
  135. />
  136. </div>
  137. </PanelItem>
  138. ))}
  139. </Fragment>
  140. ))}
  141. </PanelBody>
  142. </div>
  143. ) : (
  144. <EmptyMessage>{t("There's no subscription backend present.")}</EmptyMessage>
  145. )}
  146. </Panel>
  147. <TextBlock>
  148. {t(`We’re applying GDPR consent and privacy policies to all Sentry
  149. contacts, regardless of location. You’ll be able to manage your
  150. subscriptions here and from an Unsubscribe link in the footer of
  151. all marketing emails.`)}
  152. </TextBlock>
  153. <TextBlock>
  154. {tct(
  155. 'Please contact [email:learn@sentry.io] with any questions or suggestions.',
  156. {email: <a href="mailto:learn@sentry.io" />}
  157. )}
  158. </TextBlock>
  159. </div>
  160. );
  161. }
  162. }
  163. const Heading = styled(PanelItem)`
  164. display: grid;
  165. grid-template-columns: max-content 1fr;
  166. gap: ${space(1)};
  167. align-items: center;
  168. font-size: ${p => p.theme.fontSizeMedium};
  169. padding: ${space(1.5)} ${space(2)};
  170. background: ${p => p.theme.backgroundSecondary};
  171. color: ${p => p.theme.subText};
  172. `;
  173. const SubscriptionDetails = styled('div')`
  174. width: 50%;
  175. padding-right: ${space(2)};
  176. `;
  177. const SubscriptionName = styled('div')`
  178. font-size: ${p => p.theme.fontSizeMedium};
  179. `;
  180. const Description = styled('div')`
  181. font-size: ${p => p.theme.fontSizeSmall};
  182. color: ${p => p.theme.subText};
  183. margin-top: ${space(0.5)};
  184. `;
  185. const SubscribedDescription = styled(Description)`
  186. color: ${p => p.theme.subText};
  187. `;
  188. export default AccountSubscriptions;