organizationApiKeyDetails.tsx 3.6 KB

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