index.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type {RouteComponentProps} from 'react-router';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import {t} from 'sentry/locale';
  4. import type {Organization} from 'sentry/types/organization';
  5. import {browserHistory} from 'sentry/utils/browserHistory';
  6. import recreateRoute from 'sentry/utils/recreateRoute';
  7. import routeTitleGen from 'sentry/utils/routeTitle';
  8. import withOrganization from 'sentry/utils/withOrganization';
  9. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  10. import OrganizationApiKeysList from './organizationApiKeysList';
  11. import type {DeprecatedApiKey} from './types';
  12. type Props = RouteComponentProps<{}, {}> & {
  13. organization: Organization;
  14. };
  15. type State = {
  16. keys: DeprecatedApiKey[];
  17. } & DeprecatedAsyncView['state'];
  18. /**
  19. * API Keys are deprecated, but there may be some legacy customers that still use it
  20. */
  21. class OrganizationApiKeys extends DeprecatedAsyncView<Props, State> {
  22. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  23. const {organization} = this.props;
  24. return [['keys', `/organizations/${organization.slug}/api-keys/`]];
  25. }
  26. getTitle() {
  27. return routeTitleGen(t('API Keys'), this.props.organization.slug, false);
  28. }
  29. handleRemove = async (id: string) => {
  30. const {organization} = this.props;
  31. const oldKeys = [...this.state.keys];
  32. this.setState(state => ({
  33. keys: state.keys.filter(({id: existingId}) => existingId !== id),
  34. }));
  35. try {
  36. await this.api.requestPromise(
  37. `/organizations/${organization.slug}/api-keys/${id}/`,
  38. {
  39. method: 'DELETE',
  40. data: {},
  41. }
  42. );
  43. } catch {
  44. this.setState({keys: oldKeys, busy: false});
  45. addErrorMessage(t('Error removing key'));
  46. }
  47. };
  48. handleAddApiKey = async () => {
  49. this.setState({
  50. busy: true,
  51. });
  52. const {organization} = this.props;
  53. try {
  54. const data = await this.api.requestPromise(
  55. `/organizations/${organization.slug}/api-keys/`,
  56. {
  57. method: 'POST',
  58. data: {},
  59. }
  60. );
  61. if (data) {
  62. this.setState({busy: false});
  63. browserHistory.push(
  64. recreateRoute(`${data.id}/`, {
  65. params: {orgId: organization.slug},
  66. routes: this.props.routes,
  67. })
  68. );
  69. addSuccessMessage(t('Created a new API key "%s"', data.label));
  70. }
  71. } catch {
  72. this.setState({busy: false});
  73. }
  74. };
  75. renderLoading() {
  76. return this.renderBody();
  77. }
  78. renderBody() {
  79. const {organization} = this.props;
  80. const params = {orgId: organization.slug};
  81. return (
  82. <OrganizationApiKeysList
  83. {...this.props}
  84. params={params}
  85. loading={this.state.loading}
  86. busy={this.state.busy}
  87. keys={this.state.keys}
  88. onRemove={this.handleRemove}
  89. onAddApiKey={this.handleAddApiKey}
  90. />
  91. );
  92. }
  93. }
  94. export default withOrganization(OrganizationApiKeys);