organizationApiKeyDetails.tsx 4.0 KB

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