cancelSubscriptionAction.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {Component, Fragment} from 'react';
  2. import moment from 'moment-timezone';
  3. import type {
  4. AdminConfirmParams,
  5. AdminConfirmRenderProps,
  6. } from 'admin/components/adminConfirmationModal';
  7. import type {Subscription} from 'getsentry/types';
  8. type Props = AdminConfirmRenderProps & {
  9. subscription: Subscription;
  10. };
  11. type State = {
  12. applyBalance: boolean;
  13. cancelAtPeriodEnd: boolean;
  14. };
  15. /**
  16. * Rendered as part of a openAdminConfirmModal call
  17. */
  18. class CancelSubscriptionAction extends Component<Props, State> {
  19. state: State = {
  20. cancelAtPeriodEnd: true,
  21. applyBalance: true,
  22. };
  23. componentDidMount() {
  24. this.props.setConfirmCallback(this.handleConfirm);
  25. }
  26. handleConfirm = (params: AdminConfirmParams) => {
  27. const {applyBalance, cancelAtPeriodEnd} = this.state;
  28. this.props.onConfirm?.({cancelAtPeriodEnd, applyBalance, ...params});
  29. };
  30. render() {
  31. const {subscription} = this.props;
  32. const options: Array<[boolean, string, string]> = [
  33. [false, 'Immediately', 'End the subscription immediately.'],
  34. [
  35. true,
  36. `At period end (${moment(subscription.contractPeriodEnd).format('ll')})`,
  37. 'End the subscription at the end of the current contract period.',
  38. ],
  39. ];
  40. return (
  41. <Fragment>
  42. {options.map(([enabled, label, help]) => (
  43. <div key={enabled.toString()}>
  44. <label
  45. style={{marginBottom: 10, position: 'relative'}}
  46. key="cancelAtPeriodEnd"
  47. aria-label={label}
  48. >
  49. <div style={{position: 'absolute', left: 0, width: 20}}>
  50. <input
  51. type="radio"
  52. name="cancelAtPeriodEnd"
  53. checked={this.state.cancelAtPeriodEnd === enabled}
  54. onChange={() => this.setState({cancelAtPeriodEnd: enabled})}
  55. />
  56. </div>
  57. <div style={{marginLeft: 25}}>
  58. <strong>{label}</strong>
  59. <br />
  60. <small style={{fontWeight: 'normal'}}>{help}</small>
  61. </div>
  62. </label>
  63. {enabled ? null : (
  64. <label
  65. style={{
  66. marginBottom: 10,
  67. marginLeft: 25,
  68. position: 'relative',
  69. }}
  70. key="credit"
  71. >
  72. <input
  73. type="checkbox"
  74. name="applyBalance"
  75. checked={this.state.applyBalance}
  76. style={{marginRight: 5}}
  77. onChange={e => this.setState({applyBalance: e.target.checked})}
  78. disabled={this.state.cancelAtPeriodEnd !== enabled}
  79. />
  80. Apply credit for the remaining time on their subscription.
  81. </label>
  82. )}
  83. </div>
  84. ))}
  85. </Fragment>
  86. );
  87. }
  88. }
  89. export default CancelSubscriptionAction;