changeDatesAction.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {Fragment} from 'react';
  2. import {addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import {type ModalRenderProps, openModal} from 'sentry/actionCreators/modal';
  4. import {Alert} from 'sentry/components/core/alert';
  5. import DateTimeField from 'sentry/components/deprecatedforms/dateTimeField';
  6. import Form from 'sentry/components/deprecatedforms/form';
  7. import useApi from 'sentry/utils/useApi';
  8. import type {Subscription} from 'getsentry/types';
  9. type Props = {
  10. onSuccess: () => void;
  11. orgId: string;
  12. subscription: Subscription;
  13. };
  14. type ModalProps = Props & ModalRenderProps;
  15. class DateField extends DateTimeField {
  16. getType() {
  17. return 'date';
  18. }
  19. }
  20. function ChangeDatesModal({
  21. orgId,
  22. subscription,
  23. onSuccess,
  24. closeModal,
  25. Header,
  26. Body,
  27. }: ModalProps) {
  28. const api = useApi();
  29. async function onSubmit(formData: any, _onSubmitSuccess: unknown, onSubmitError: any) {
  30. try {
  31. const postData = {
  32. onDemandPeriodStart: subscription.onDemandPeriodStart,
  33. onDemandPeriodEnd: subscription.onDemandPeriodEnd,
  34. contractPeriodStart: subscription.contractPeriodStart,
  35. contractPeriodEnd: subscription.contractPeriodEnd,
  36. };
  37. for (const k in formData) {
  38. if (formData[k] !== '' && formData[k]) {
  39. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  40. postData[k] = formData[k];
  41. }
  42. }
  43. await api.requestPromise(`/customers/${orgId}/`, {
  44. method: 'PUT',
  45. data: postData,
  46. success: () => {
  47. addSuccessMessage('Contract and on-demand period dates updated');
  48. onSuccess();
  49. },
  50. });
  51. closeModal();
  52. } catch (err) {
  53. onSubmitError({
  54. responseJSON: err.responseJSON,
  55. });
  56. }
  57. }
  58. return (
  59. <Fragment>
  60. <Header>Change Contract and Current On-Demand Period Dates</Header>
  61. <Body>
  62. <Form
  63. onSubmit={onSubmit}
  64. onCancel={closeModal}
  65. submitLabel="Submit"
  66. cancelLabel="Cancel"
  67. >
  68. <Alert.Container>
  69. <Alert type="warning">
  70. This overrides the current contract and on-demand period dates so the
  71. subscription may fall into a weird state.
  72. </Alert>
  73. </Alert.Container>
  74. <p>To end the contract period immediately, use the End Period Now action.</p>
  75. <DateField
  76. label="On-Demand Period Start Date"
  77. name="onDemandPeriodStart"
  78. help="The new start date for the on-demand period."
  79. defaultValue={subscription.onDemandPeriodStart}
  80. />
  81. <DateField
  82. label="On-Demand Period End Date"
  83. name="onDemandPeriodEnd"
  84. help="The new end date for the on-demand period."
  85. defaultValue={subscription.onDemandPeriodEnd}
  86. />
  87. <DateField
  88. label="Contract Period Start Date"
  89. name="contractPeriodStart"
  90. help="The new start date for the contract period."
  91. defaultValue={subscription.contractPeriodStart}
  92. />
  93. <DateField
  94. label="Contract Period End Date"
  95. name="contractPeriodEnd"
  96. help="The new end date for the contract period."
  97. defaultValue={subscription.contractPeriodEnd}
  98. />
  99. </Form>
  100. </Body>
  101. </Fragment>
  102. );
  103. }
  104. type Options = Pick<Props, 'orgId' | 'subscription' | 'onSuccess'>;
  105. const triggerChangeDatesModal = (opts: Options) =>
  106. openModal(deps => <ChangeDatesModal {...deps} {...opts} />);
  107. export default triggerChangeDatesModal;