sendWeeklyEmailAction.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {Component} from 'react';
  2. import {
  3. addErrorMessage,
  4. addLoadingMessage,
  5. addSuccessMessage,
  6. } from 'sentry/actionCreators/indicator';
  7. import type {Client} from 'sentry/api';
  8. import BooleanField from 'sentry/components/forms/fields/booleanField';
  9. import TextField from 'sentry/components/forms/fields/textField';
  10. import withApi from 'sentry/utils/withApi';
  11. import type {
  12. AdminConfirmParams,
  13. AdminConfirmRenderProps,
  14. } from 'admin/components/adminConfirmationModal';
  15. type Props = {api: Client; orgId: string} & AdminConfirmRenderProps;
  16. type State = {
  17. deliveryEmail: string;
  18. dryRun: boolean;
  19. targetEmail: string;
  20. };
  21. /**
  22. * Rendered as part of a openAdminConfirmModal call
  23. */
  24. class SendWeeklyEmailAction extends Component<Props, State> {
  25. state: State = {
  26. dryRun: true,
  27. targetEmail: '',
  28. deliveryEmail: '',
  29. };
  30. componentDidMount() {
  31. this.props.setConfirmCallback(this.handleConfirm);
  32. }
  33. handleConfirm = (_params: AdminConfirmParams) => {
  34. const {targetEmail, deliveryEmail, dryRun} = this.state;
  35. addLoadingMessage('Sending Email');
  36. this.props.api
  37. .requestPromise(`/customers/${this.props.orgId}/send-weekly-email/`, {
  38. method: 'POST',
  39. data: {targetEmail, deliveryEmail, dryRun},
  40. })
  41. .then(() => {
  42. addSuccessMessage('Email queued');
  43. })
  44. .catch(res => {
  45. if (res.status === 404) {
  46. addErrorMessage('User is not a member of the organization!');
  47. } else {
  48. addErrorMessage(res.responseText || res.name);
  49. }
  50. });
  51. };
  52. render() {
  53. return (
  54. <div>
  55. <TextField
  56. autoFocus
  57. inline={false}
  58. stacked
  59. flexibleControlStateSize
  60. label="Target User Email"
  61. help="The weekly email will be generated based on this user. If left empty the email will be generated for a user with access to all projects"
  62. name="username"
  63. inputMode="text"
  64. value={this.state.targetEmail}
  65. onChange={(targetEmail: any) => this.setState({targetEmail})}
  66. />
  67. <TextField
  68. autoFocus
  69. inline={false}
  70. stacked
  71. flexibleControlStateSize
  72. label="Delivery email address"
  73. help="The weekly email will be sent to this address."
  74. required
  75. name="username"
  76. inputMode="text"
  77. value={this.state.deliveryEmail}
  78. onChange={(deliveryEmail: any) => this.setState({deliveryEmail})}
  79. />
  80. <BooleanField
  81. label="Dry Run"
  82. inline={false}
  83. name="dryrun"
  84. stacked
  85. value={this.state.dryRun}
  86. onChange={(dryRun: any) => this.setState({dryRun})}
  87. />
  88. </div>
  89. );
  90. }
  91. }
  92. export default withApi(SendWeeklyEmailAction);