organizationAccessRequests.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {Client} from 'sentry/api';
  5. import {Button} from 'sentry/components/button';
  6. import {Panel, PanelBody, PanelHeader, PanelItem} from 'sentry/components/panels';
  7. import {t, tct} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import {AccessRequest} from 'sentry/types';
  10. import withApi from 'sentry/utils/withApi';
  11. type Props = {
  12. api: Client;
  13. onRemoveAccessRequest: (id: string, isApproved: boolean) => void;
  14. orgId: string;
  15. requestList: AccessRequest[];
  16. };
  17. type State = {
  18. accessRequestBusy: Record<string, boolean>;
  19. };
  20. type HandleOpts = {
  21. errorMessage: string;
  22. id: string;
  23. isApproved: boolean;
  24. successMessage: string;
  25. };
  26. class OrganizationAccessRequests extends Component<Props, State> {
  27. state: State = {
  28. accessRequestBusy: {},
  29. };
  30. async handleAction({id, isApproved, successMessage, errorMessage}: HandleOpts) {
  31. const {api, orgId, onRemoveAccessRequest} = this.props;
  32. this.setState(state => ({
  33. accessRequestBusy: {...state.accessRequestBusy, [id]: true},
  34. }));
  35. try {
  36. await api.requestPromise(`/organizations/${orgId}/access-requests/${id}/`, {
  37. method: 'PUT',
  38. data: {isApproved},
  39. });
  40. onRemoveAccessRequest(id, isApproved);
  41. addSuccessMessage(successMessage);
  42. } catch {
  43. addErrorMessage(errorMessage);
  44. }
  45. this.setState(state => ({
  46. accessRequestBusy: {...state.accessRequestBusy, [id]: false},
  47. }));
  48. }
  49. handleApprove = (id: string, e: React.MouseEvent) => {
  50. e.stopPropagation();
  51. this.handleAction({
  52. id,
  53. isApproved: true,
  54. successMessage: t('Team request approved'),
  55. errorMessage: t('Error approving team request'),
  56. });
  57. };
  58. handleDeny = (id: string, e: React.MouseEvent) => {
  59. e.stopPropagation();
  60. this.handleAction({
  61. id,
  62. isApproved: false,
  63. successMessage: t('Team request denied'),
  64. errorMessage: t('Error denying team request'),
  65. });
  66. };
  67. render() {
  68. const {requestList} = this.props;
  69. const {accessRequestBusy} = this.state;
  70. if (!requestList || !requestList.length) {
  71. return null;
  72. }
  73. return (
  74. <Panel>
  75. <PanelHeader>{t('Pending Team Requests')}</PanelHeader>
  76. <PanelBody>
  77. {requestList.map(({id, member, team, requester}) => {
  78. const memberName =
  79. member.user &&
  80. (member.user.name || member.user.email || member.user.username);
  81. const requesterName =
  82. requester && (requester.name || requester.email || requester.username);
  83. return (
  84. <StyledPanelItem key={id}>
  85. <div data-test-id="request-message">
  86. {requesterName
  87. ? tct('[requesterName] requests to add [name] to the [team] team.', {
  88. requesterName,
  89. name: <strong>{memberName}</strong>,
  90. team: <strong>#{team.slug}</strong>,
  91. })
  92. : tct('[name] requests access to the [team] team.', {
  93. name: <strong>{memberName}</strong>,
  94. team: <strong>#{team.slug}</strong>,
  95. })}
  96. </div>
  97. <div>
  98. <StyledButton
  99. priority="primary"
  100. size="sm"
  101. onClick={e => this.handleApprove(id, e)}
  102. busy={accessRequestBusy[id]}
  103. >
  104. {t('Approve')}
  105. </StyledButton>
  106. <Button
  107. busy={accessRequestBusy[id]}
  108. onClick={e => this.handleDeny(id, e)}
  109. size="sm"
  110. >
  111. {t('Deny')}
  112. </Button>
  113. </div>
  114. </StyledPanelItem>
  115. );
  116. })}
  117. </PanelBody>
  118. </Panel>
  119. );
  120. }
  121. }
  122. const StyledPanelItem = styled(PanelItem)`
  123. display: grid;
  124. grid-template-columns: auto max-content;
  125. gap: ${space(2)};
  126. align-items: center;
  127. `;
  128. const StyledButton = styled(Button)`
  129. margin-right: ${space(1)};
  130. `;
  131. export default withApi(OrganizationAccessRequests);