addOrRemoveOrgModal.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {Fragment, useState} from 'react';
  2. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import {Alert} from 'sentry/components/core/alert';
  4. import TextField from 'sentry/components/forms/fields/inputField';
  5. import SentryOrganizationRoleSelectorField from 'sentry/components/forms/fields/sentryOrganizationRoleSelectorField';
  6. import Form from 'sentry/components/forms/form';
  7. import {t} from 'sentry/locale';
  8. import useApi from 'sentry/utils/useApi';
  9. interface AddOrRemoveOrgModalProps extends ModalRenderProps {
  10. userId: string;
  11. }
  12. function AddToOrgModal({Header, Body, userId, closeModal}: AddOrRemoveOrgModalProps) {
  13. const api = useApi();
  14. const [error, setError] = useState(null);
  15. const onSubmit = async (data: Record<string, any>) => {
  16. try {
  17. await api
  18. .requestPromise(`/_admin/${data.organizationSlug}/users/${userId}/members/`, {
  19. method: 'POST',
  20. data: {
  21. orgRole: data.role,
  22. },
  23. })
  24. .then(() => {
  25. closeModal();
  26. window.location.reload();
  27. });
  28. } catch (err) {
  29. setError(err.responseJSON.detail);
  30. }
  31. };
  32. return (
  33. <Fragment>
  34. <Header closeButton>
  35. <h4>{t('Add Member to an Organization')}</h4>
  36. </Header>
  37. <Body>
  38. <Form onSubmit={onSubmit} submitLabel="Submit" cancelLabel="Cancel">
  39. <TextField
  40. required
  41. label="Organization Slug"
  42. name="organizationSlug"
  43. help="A unique ID used to identify this organization"
  44. />
  45. <SentryOrganizationRoleSelectorField required name="role" label="Role" />
  46. <Fragment>
  47. <br />
  48. Note: This action will be recorded in the audit log.
  49. </Fragment>
  50. {error && (
  51. <Alert.Container>
  52. <Alert type="error">{error}</Alert>
  53. </Alert.Container>
  54. )}
  55. </Form>
  56. </Body>
  57. </Fragment>
  58. );
  59. }
  60. function RemoveFromOrgModal({
  61. Header,
  62. Body,
  63. userId,
  64. closeModal,
  65. }: AddOrRemoveOrgModalProps) {
  66. const api = useApi();
  67. const [error, setError] = useState(null);
  68. const onSubmit = async (data: Record<string, any>) => {
  69. try {
  70. await api
  71. .requestPromise(`/_admin/${data.organizationSlug}/users/${userId}/members/`, {
  72. method: 'DELETE',
  73. })
  74. .then(() => {
  75. closeModal();
  76. window.location.reload();
  77. });
  78. } catch (err) {
  79. setError(err.responseJSON.detail);
  80. }
  81. };
  82. return (
  83. <Fragment>
  84. <Header closeButton>
  85. <h4>{t('Remove Member from an Organization')}</h4>
  86. </Header>
  87. <Body>
  88. <Form onSubmit={onSubmit} submitLabel="Submit" cancelLabel="Cancel">
  89. <TextField
  90. required
  91. label="Organization Slug"
  92. name="organizationSlug"
  93. help="A unique ID used to identify this organization"
  94. />
  95. <Fragment>
  96. <br />
  97. Note: This action will be recorded in the audit log.
  98. </Fragment>
  99. {error && (
  100. <Alert.Container>
  101. <Alert type="error">{error}</Alert>
  102. </Alert.Container>
  103. )}
  104. </Form>
  105. </Body>
  106. </Fragment>
  107. );
  108. }
  109. export {AddToOrgModal, RemoveFromOrgModal};