index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 EmptyMessage from 'sentry/components/emptyMessage';
  9. import {Panel, PanelBody, PanelHeader} from 'sentry/components/panels';
  10. import {IconAdd} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import {ApiApplication} from 'sentry/types';
  13. import AsyncView from 'sentry/views/asyncView';
  14. import Row from 'sentry/views/settings/account/apiApplications/row';
  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 {
  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 isEmpty = this.state.appList.length === 0;
  47. return (
  48. <div>
  49. <SettingsPageHeader
  50. title="API Applications"
  51. action={
  52. <Button
  53. priority="primary"
  54. size="sm"
  55. onClick={this.handleCreateApplication}
  56. icon={<IconAdd size="xs" isCircled />}
  57. >
  58. {t('Create New Application')}
  59. </Button>
  60. }
  61. />
  62. <Panel>
  63. <PanelHeader>{t('Application Name')}</PanelHeader>
  64. <PanelBody>
  65. {!isEmpty ? (
  66. this.state.appList.map(app => (
  67. <Row
  68. api={this.api}
  69. key={app.id}
  70. app={app}
  71. onRemove={this.handleRemoveApplication}
  72. />
  73. ))
  74. ) : (
  75. <EmptyMessage>
  76. {t("You haven't created any applications yet.")}
  77. </EmptyMessage>
  78. )}
  79. </PanelBody>
  80. </Panel>
  81. </div>
  82. );
  83. }
  84. }
  85. export default ApiApplications;