organizationApiKeyDetails.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import type {RouteComponentProps} from 'react-router';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import ApiForm from 'sentry/components/forms/apiForm';
  4. import MultipleCheckbox from 'sentry/components/forms/controls/multipleCheckbox';
  5. import TextareaField from 'sentry/components/forms/fields/textareaField';
  6. import TextField from 'sentry/components/forms/fields/textField';
  7. import FormField from 'sentry/components/forms/formField';
  8. import Panel from 'sentry/components/panels/panel';
  9. import PanelBody from 'sentry/components/panels/panelBody';
  10. import PanelHeader from 'sentry/components/panels/panelHeader';
  11. import {API_ACCESS_SCOPES} from 'sentry/constants';
  12. import {t} from 'sentry/locale';
  13. import type {Organization} from 'sentry/types/organization';
  14. import {browserHistory} from 'sentry/utils/browserHistory';
  15. import recreateRoute from 'sentry/utils/recreateRoute';
  16. import routeTitleGen from 'sentry/utils/routeTitle';
  17. import withOrganization from 'sentry/utils/withOrganization';
  18. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  19. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  20. import type {DeprecatedApiKey} from './types';
  21. type RouteParams = {
  22. apiKey: string;
  23. };
  24. type Props = RouteComponentProps<RouteParams, {}> & {
  25. organization: Organization;
  26. };
  27. type State = DeprecatedAsyncView['state'] & {
  28. apiKey: DeprecatedApiKey;
  29. };
  30. class OrganizationApiKeyDetails extends DeprecatedAsyncView<Props, State> {
  31. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  32. const {organization} = this.props;
  33. return [
  34. [
  35. 'apiKey',
  36. `/organizations/${organization.slug}/api-keys/${this.props.params.apiKey}/`,
  37. ],
  38. ];
  39. }
  40. getTitle() {
  41. return routeTitleGen(t('Edit API Key'), this.props.organization.slug, false);
  42. }
  43. handleSubmitSuccess = () => {
  44. addSuccessMessage('Saved changes');
  45. // Go back to API list
  46. browserHistory.push(
  47. recreateRoute('', {
  48. stepBack: -1,
  49. routes: this.props.routes,
  50. params: this.props.params,
  51. })
  52. );
  53. };
  54. handleSubmitError = () => {
  55. addErrorMessage('Unable to save changes. Please try again.');
  56. };
  57. renderBody() {
  58. const {organization} = this.props;
  59. return (
  60. <div>
  61. <SettingsPageHeader title={t('Edit API Key')} />
  62. <Panel>
  63. <PanelHeader>{t('API Key')}</PanelHeader>
  64. <ApiForm
  65. apiMethod="PUT"
  66. apiEndpoint={`/organizations/${organization.slug}/api-keys/${this.props.params.apiKey}/`}
  67. initialData={this.state.apiKey}
  68. onSubmitSuccess={this.handleSubmitSuccess}
  69. onSubmitError={this.handleSubmitError}
  70. onCancel={() =>
  71. browserHistory.push(
  72. recreateRoute('', {
  73. stepBack: -1,
  74. routes: this.props.routes,
  75. params: this.props.params,
  76. })
  77. )
  78. }
  79. >
  80. <PanelBody>
  81. <TextField label={t('Label')} name="label" />
  82. <TextField label={t('API Key')} name="key" disabled />
  83. <FormField name="scope_list" label={t('Scopes')} inline={false} required>
  84. {({name, value, onChange}) => (
  85. <MultipleCheckbox value={value} onChange={onChange} name={name}>
  86. {API_ACCESS_SCOPES.map(scope => (
  87. <MultipleCheckbox.Item value={scope} key={scope}>
  88. {scope}
  89. </MultipleCheckbox.Item>
  90. ))}
  91. </MultipleCheckbox>
  92. )}
  93. </FormField>
  94. <TextareaField
  95. label={t('Allowed Domains')}
  96. name="allowed_origins"
  97. placeholder="e.g. example.com or https://example.com"
  98. help="Separate multiple entries with a newline"
  99. />
  100. </PanelBody>
  101. </ApiForm>
  102. </Panel>
  103. </div>
  104. );
  105. }
  106. }
  107. export default withOrganization(OrganizationApiKeyDetails);