index.tsx 2.9 KB

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