index.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {RouteComponentProps} from 'react-router';
  2. import {
  3. addErrorMessage,
  4. addLoadingMessage,
  5. addSuccessMessage,
  6. } from 'sentry/actionCreators/indicator';
  7. import Button from 'sentry/components/button';
  8. import {Panel, PanelBody, PanelHeader} from 'sentry/components/panels';
  9. import {IconAdd} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import {ApiApplication} from 'sentry/types';
  12. import AsyncView from 'sentry/views/asyncView';
  13. import Row from 'sentry/views/settings/account/apiApplications/row';
  14. import EmptyMessage from 'sentry/views/settings/components/emptyMessage';
  15. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  16. const ROUTE_PREFIX = '/settings/account/api/';
  17. type Props = RouteComponentProps<{}, {}>;
  18. type State = {
  19. appList: ApiApplication[];
  20. } & AsyncView['state'];
  21. class ApiApplications extends AsyncView<Props, State> {
  22. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  23. return [['appList', '/api-applications/']];
  24. }
  25. getTitle() {
  26. return t('API Applications');
  27. }
  28. handleCreateApplication = async () => {
  29. addLoadingMessage();
  30. try {
  31. const app = await this.api.requestPromise('/api-applications/', {
  32. method: 'POST',
  33. });
  34. addSuccessMessage(t('Created a new API Application'));
  35. this.props.router.push(`${ROUTE_PREFIX}applications/${app.id}/`);
  36. } catch (_err) {
  37. addErrorMessage(t('Unable to remove application. Please try again.'));
  38. }
  39. };
  40. handleRemoveApplication = (app: ApiApplication) => {
  41. this.setState({
  42. appList: this.state.appList.filter(a => a.id !== app.id),
  43. });
  44. };
  45. renderBody() {
  46. const action = (
  47. <Button
  48. priority="primary"
  49. size="sm"
  50. onClick={this.handleCreateApplication}
  51. icon={<IconAdd size="xs" isCircled />}
  52. >
  53. {t('Create New Application')}
  54. </Button>
  55. );
  56. const isEmpty = this.state.appList.length === 0;
  57. return (
  58. <div>
  59. <SettingsPageHeader title="API Applications" action={action} />
  60. <Panel>
  61. <PanelHeader>{t('Application Name')}</PanelHeader>
  62. <PanelBody>
  63. {!isEmpty ? (
  64. this.state.appList.map(app => (
  65. <Row
  66. api={this.api}
  67. key={app.id}
  68. app={app}
  69. onRemove={this.handleRemoveApplication}
  70. />
  71. ))
  72. ) : (
  73. <EmptyMessage>
  74. {t("You haven't created any applications yet.")}
  75. </EmptyMessage>
  76. )}
  77. </PanelBody>
  78. </Panel>
  79. </div>
  80. );
  81. }
  82. }
  83. export default ApiApplications;