toggleSpendAllocationModal.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {Fragment} from 'react';
  2. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import {openModal} from 'sentry/actionCreators/modal';
  4. import type {Client} from 'sentry/api';
  5. import Form from 'sentry/components/forms/form';
  6. import {t, tct} from 'sentry/locale';
  7. import withApi from 'sentry/utils/withApi';
  8. type Props = {
  9. api: Client;
  10. onUpdated: (data: any) => void;
  11. orgId: string;
  12. spendAllocationEnabled: boolean;
  13. };
  14. type ModalProps = Props & ModalRenderProps;
  15. function SpendAllocationModal({
  16. Body,
  17. Header,
  18. closeModal,
  19. api,
  20. onUpdated,
  21. orgId,
  22. spendAllocationEnabled: isCurrentlyEnabled,
  23. }: ModalProps) {
  24. const onSubmit = async () => {
  25. const shouldEnableAllocations = !isCurrentlyEnabled;
  26. const method = shouldEnableAllocations ? 'POST' : 'DELETE';
  27. try {
  28. await api.requestPromise(`/organizations/${orgId}/spend-allocations/toggle/`, {
  29. method,
  30. });
  31. // Create root allocations
  32. await api.requestPromise(`/organizations/${orgId}/spend-allocations/index/`, {
  33. method,
  34. });
  35. onUpdated({spendAllocationEnabled: shouldEnableAllocations});
  36. } catch (error) {
  37. onUpdated({error});
  38. }
  39. closeModal();
  40. };
  41. return (
  42. <Fragment>
  43. <Header>Toggle Spend Allocations</Header>
  44. <Body>
  45. <Form
  46. onSubmit={onSubmit}
  47. submitLabel={isCurrentlyEnabled ? t('Disable') : t('Enable')}
  48. onCancel={closeModal}
  49. >
  50. {t('Access to spend allocations is currently ')}
  51. <strong>
  52. {tct('[action]', {
  53. action: isCurrentlyEnabled ? t('enabled') : t('disabled'),
  54. })}
  55. </strong>
  56. {t(' for this organization.')}
  57. {tct('Would you like to [newAction] access to spend allocations?', {
  58. root: <p />,
  59. newAction: isCurrentlyEnabled ? t('disable') : t('enable'),
  60. })}
  61. </Form>
  62. </Body>
  63. </Fragment>
  64. );
  65. }
  66. const Modal = withApi(SpendAllocationModal);
  67. type Options = Pick<Props, 'orgId' | 'spendAllocationEnabled' | 'onUpdated'>;
  68. const toggleSpendAllocationModal = (opts: Options) =>
  69. openModal(deps => <Modal {...deps} {...opts} />);
  70. export default toggleSpendAllocationModal;