index.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 AsyncView from 'sentry/views/asyncView';
  9. import OrganizationApiKeysList from './organizationApiKeysList';
  10. import {DeprecatedApiKey} from './types';
  11. type RouteParams = {
  12. orgId: string;
  13. };
  14. type Props = RouteComponentProps<RouteParams, {}> & {
  15. organization: Organization;
  16. };
  17. type State = {
  18. keys: DeprecatedApiKey[];
  19. } & AsyncView['state'];
  20. /**
  21. * API Keys are deprecated, but there may be some legacy customers that still use it
  22. */
  23. class OrganizationApiKeys extends AsyncView<Props, State> {
  24. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  25. return [['keys', `/organizations/${this.props.params.orgId}/api-keys/`]];
  26. }
  27. getTitle() {
  28. return routeTitleGen(t('API Keys'), this.props.organization.slug, false);
  29. }
  30. handleRemove = async (id: string) => {
  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/${this.props.params.orgId}/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. try {
  53. const data = await this.api.requestPromise(
  54. `/organizations/${this.props.params.orgId}/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: this.props.params,
  65. routes: this.props.routes,
  66. })
  67. );
  68. addSuccessMessage(t(`Created a new API key "${data.label}"`));
  69. }
  70. } catch {
  71. this.setState({busy: false});
  72. }
  73. };
  74. renderLoading() {
  75. return this.renderBody();
  76. }
  77. renderBody() {
  78. return (
  79. <OrganizationApiKeysList
  80. loading={this.state.loading}
  81. busy={this.state.busy}
  82. keys={this.state.keys}
  83. onRemove={this.handleRemove}
  84. onAddApiKey={this.handleAddApiKey}
  85. {...this.props}
  86. />
  87. );
  88. }
  89. }
  90. export default withOrganization(OrganizationApiKeys);