relocationCancelModal.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {Fragment} from 'react';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  4. import ApiForm from 'sentry/components/forms/apiForm';
  5. import SelectField from 'sentry/components/forms/fields/selectField';
  6. import type {Relocation} from 'admin/types';
  7. import {RelocationSteps} from 'admin/types';
  8. import titleCase from 'getsentry/utils/titleCase';
  9. type Props = ModalRenderProps & {
  10. relocation: Relocation;
  11. onSuccess?: (relocation: Relocation) => void;
  12. };
  13. function RelocationCancelModal({Body, Header, relocation, onSuccess, closeModal}: Props) {
  14. const currentStep = RelocationSteps[relocation.step];
  15. const choices = Object.keys(RelocationSteps)
  16. // @ts-expect-error TS(7015): Element implicitly has an 'any' type because index... Remove this comment to see the full error message
  17. .filter(step => RelocationSteps[step] > currentStep && step !== 'COMPLETED')
  18. .map(step => [step, titleCase(step)]);
  19. choices.unshift(['ASAP', 'As soon as possible']);
  20. return (
  21. <Fragment>
  22. <Header closeButton>Cancel Relocation</Header>
  23. <Body>
  24. <ApiForm
  25. apiMethod="PUT"
  26. apiEndpoint={`/relocations/${relocation.uuid}/cancel/`}
  27. hostOverride={relocation.region.url}
  28. onSubmit={(data: Record<string, any>) => {
  29. const payload: Record<string, any> = {};
  30. if (data.atStep !== 'ASAP') {
  31. payload.atStep = data.atStep;
  32. }
  33. return payload;
  34. }}
  35. onSubmitSuccess={(rawRelocation: Relocation) => {
  36. if (onSuccess) {
  37. onSuccess(rawRelocation);
  38. }
  39. closeModal();
  40. addSuccessMessage('This relocation has been scheduled for cancellation.');
  41. }}
  42. onSubmitError={error => {
  43. addErrorMessage(error.responseJSON?.detail);
  44. }}
  45. initialData={relocation || {atStep: 'ASAP'}}
  46. submitLabel="Schedule"
  47. >
  48. <SelectField
  49. choices={choices}
  50. flexibleControlStateSize={false}
  51. help="Select a step to cancel PRIOR to:"
  52. inline={false}
  53. label="Scheduled At"
  54. name="atStep"
  55. stacked
  56. required
  57. />
  58. </ApiForm>
  59. </Body>
  60. </Fragment>
  61. );
  62. }
  63. export default RelocationCancelModal;