accountSubscriptions.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 = (
  38. subscription: Subscription,
  39. email: string,
  40. listId: number,
  41. _e: React.MouseEvent
  42. ) => {
  43. const subscribed = !subscription.subscribed;
  44. const oldSubscriptions = this.state.subscriptions;
  45. this.setState(state => {
  46. const newSubscriptions = [...state.subscriptions];
  47. const index = newSubscriptions.findIndex(
  48. sub => sub.listId === listId && sub.email === email
  49. );
  50. newSubscriptions[index] = {
  51. ...subscription,
  52. subscribed,
  53. subscribedDate: new Date().toString(),
  54. };
  55. return {
  56. ...state,
  57. subscriptions: newSubscriptions,
  58. };
  59. });
  60. this.api.request(ENDPOINT, {
  61. method: 'PUT',
  62. data: {
  63. listId: subscription.listId,
  64. subscribed,
  65. },
  66. success: () => {
  67. addSuccessMessage(
  68. `${subscribed ? 'Subscribed' : 'Unsubscribed'} to ${subscription.listName}`
  69. );
  70. },
  71. error: () => {
  72. addErrorMessage(
  73. `Unable to ${subscribed ? '' : 'un'}subscribe to ${subscription.listName}`
  74. );
  75. this.setState({subscriptions: oldSubscriptions});
  76. },
  77. });
  78. };
  79. renderBody() {
  80. // Group by does not guarantee order, so we sort by email
  81. const subGroups = orderBy(
  82. Object.entries(groupBy(this.state.subscriptions, sub => sub.email)),
  83. ([email]) => email
  84. );
  85. return (
  86. <div>
  87. <SettingsPageHeader title={this.getTitle()} />
  88. <TextBlock>
  89. {t(`Sentry is committed to respecting your inbox. Our goal is to
  90. provide useful content and resources that make fixing errors less
  91. painful. Enjoyable even.`)}
  92. </TextBlock>
  93. <TextBlock>
  94. {t(`As part of our compliance with the EU’s General Data Protection
  95. Regulation (GDPR), starting on 25 May 2018, we’ll only email you
  96. according to the marketing categories to which you’ve explicitly
  97. opted-in.`)}
  98. </TextBlock>
  99. <Panel>
  100. {this.state.subscriptions.length ? (
  101. <div>
  102. <PanelHeader>{t('Subscription')}</PanelHeader>
  103. <PanelBody>
  104. {subGroups.map(([email, subscriptions]) => (
  105. <Fragment key={email}>
  106. {subGroups.length > 1 && (
  107. <Heading>
  108. <IconToggle /> {t('Subscriptions for %s', email)}
  109. </Heading>
  110. )}
  111. {subscriptions
  112. .sort((a, b) => a.listId - b.listId)
  113. .map(subscription => (
  114. <PanelItem center key={subscription.listId}>
  115. <SubscriptionDetails
  116. htmlFor={`${subscription.email}-${subscription.listId}`}
  117. aria-label={subscription.listName}
  118. >
  119. <SubscriptionName>{subscription.listName}</SubscriptionName>
  120. {subscription.listDescription && (
  121. <Description>{subscription.listDescription}</Description>
  122. )}
  123. {subscription.subscribed ? (
  124. <SubscribedDescription>
  125. <div>
  126. {tct('[email] on [date]', {
  127. email: subscription.email,
  128. date: (
  129. <DateTime
  130. date={moment(subscription.subscribedDate!)}
  131. />
  132. ),
  133. })}
  134. </div>
  135. </SubscribedDescription>
  136. ) : (
  137. <SubscribedDescription>
  138. {t('Not currently subscribed')}
  139. </SubscribedDescription>
  140. )}
  141. </SubscriptionDetails>
  142. <div>
  143. <Switch
  144. id={`${subscription.email}-${subscription.listId}`}
  145. isActive={subscription.subscribed}
  146. size="lg"
  147. toggle={this.handleToggle.bind(
  148. this,
  149. subscription,
  150. email,
  151. subscription.listId
  152. )}
  153. />
  154. </div>
  155. </PanelItem>
  156. ))}
  157. </Fragment>
  158. ))}
  159. </PanelBody>
  160. </div>
  161. ) : (
  162. <EmptyMessage>{t("There's no subscription backend present.")}</EmptyMessage>
  163. )}
  164. </Panel>
  165. <TextBlock>
  166. {t(`We’re applying GDPR consent and privacy policies to all Sentry
  167. contacts, regardless of location. You’ll be able to manage your
  168. subscriptions here and from an Unsubscribe link in the footer of
  169. all marketing emails.`)}
  170. </TextBlock>
  171. <TextBlock>
  172. {tct(
  173. 'Please contact [email:learn@sentry.io] with any questions or suggestions.',
  174. {email: <a href="mailto:learn@sentry.io" />}
  175. )}
  176. </TextBlock>
  177. </div>
  178. );
  179. }
  180. }
  181. const Heading = styled(PanelItem)`
  182. display: grid;
  183. grid-template-columns: max-content 1fr;
  184. gap: ${space(1)};
  185. align-items: center;
  186. font-size: ${p => p.theme.fontSizeMedium};
  187. padding: ${space(1.5)} ${space(2)};
  188. background: ${p => p.theme.backgroundSecondary};
  189. color: ${p => p.theme.subText};
  190. `;
  191. const SubscriptionDetails = styled('label')`
  192. font-weight: initial;
  193. padding-right: ${space(2)};
  194. width: 85%;
  195. @media (min-width: ${p => p.theme.breakpoints.small}) {
  196. width: 75%;
  197. }
  198. @media (min-width: ${p => p.theme.breakpoints.large}) {
  199. width: 50%;
  200. }
  201. `;
  202. const SubscriptionName = styled('div')`
  203. font-size: ${p => p.theme.fontSizeMedium};
  204. `;
  205. const Description = styled('div')`
  206. font-size: ${p => p.theme.fontSizeSmall};
  207. color: ${p => p.theme.subText};
  208. margin-top: ${space(0.5)};
  209. `;
  210. const SubscribedDescription = styled(Description)`
  211. color: ${p => p.theme.subText};
  212. `;
  213. export default AccountSubscriptions;