jsonFormModal.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {Fragment} from 'react';
  2. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import type {APIRequestMethod} from 'sentry/api';
  4. import ApiForm from 'sentry/components/forms/apiForm';
  5. import type {FormProps} from 'sentry/components/forms/form';
  6. import JsonForm from 'sentry/components/forms/jsonForm';
  7. import type {FieldObject} from 'sentry/components/forms/types';
  8. interface JsonFormModalProps extends ModalRenderProps, Pick<FormProps, 'onSubmitError'> {
  9. apiEndpoint: string;
  10. // XXX(dcramer): as of the time of writing, apiMethod is forced to be `APIRequestMethod` which is an an allow-list of HTTP verbs.
  11. apiMethod: APIRequestMethod;
  12. fields: FieldObject[];
  13. onSuccess: (data: any) => void;
  14. title: string;
  15. initialData?: any;
  16. }
  17. function JsonFormModal({
  18. Body,
  19. Header,
  20. closeModal,
  21. title,
  22. apiEndpoint,
  23. apiMethod,
  24. initialData = {},
  25. fields,
  26. onSuccess,
  27. onSubmitError,
  28. }: JsonFormModalProps) {
  29. return (
  30. <Fragment>
  31. <Header closeButton>{title}</Header>
  32. <Body>
  33. <ApiForm
  34. apiMethod={apiMethod}
  35. apiEndpoint={apiEndpoint}
  36. onSubmitSuccess={(data: any) => {
  37. if (onSuccess) {
  38. onSuccess(data);
  39. }
  40. closeModal();
  41. }}
  42. onSubmitError={onSubmitError}
  43. initialData={initialData}
  44. submitLabel="Save Changes"
  45. >
  46. <JsonForm fields={fields} />
  47. </ApiForm>
  48. </Body>
  49. </Fragment>
  50. );
  51. }
  52. export default JsonFormModal;