row.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {
  4. addErrorMessage,
  5. addLoadingMessage,
  6. clearIndicators,
  7. } from 'sentry/actionCreators/indicator';
  8. import {Client} from 'sentry/api';
  9. import {Button} from 'sentry/components/button';
  10. import Link from 'sentry/components/links/link';
  11. import PanelItem from 'sentry/components/panels/panelItem';
  12. import {IconDelete} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {ApiApplication} from 'sentry/types';
  16. import getDynamicText from 'sentry/utils/getDynamicText';
  17. const ROUTE_PREFIX = '/settings/account/api/';
  18. type Props = {
  19. api: Client;
  20. app: ApiApplication;
  21. onRemove: (app: ApiApplication) => void;
  22. };
  23. type State = {
  24. loading: boolean;
  25. };
  26. class Row extends Component<Props, State> {
  27. state: State = {
  28. loading: false,
  29. };
  30. handleRemove = () => {
  31. if (this.state.loading) {
  32. return;
  33. }
  34. const {api, app, onRemove} = this.props;
  35. this.setState(
  36. {
  37. loading: true,
  38. },
  39. async () => {
  40. addLoadingMessage();
  41. try {
  42. await api.requestPromise(`/api-applications/${app.id}/`, {
  43. method: 'DELETE',
  44. });
  45. clearIndicators();
  46. onRemove(app);
  47. } catch (_err) {
  48. addErrorMessage(t('Unable to remove application. Please try again.'));
  49. }
  50. }
  51. );
  52. };
  53. render() {
  54. const {app} = this.props;
  55. return (
  56. <StyledPanelItem>
  57. <ApplicationNameWrapper>
  58. <ApplicationName to={`${ROUTE_PREFIX}applications/${app.id}/`}>
  59. {getDynamicText({value: app.name, fixed: 'CI_APPLICATION_NAME'})}
  60. </ApplicationName>
  61. <ClientId>
  62. {getDynamicText({value: app.clientID, fixed: 'CI_CLIENT_ID'})}
  63. </ClientId>
  64. </ApplicationNameWrapper>
  65. <Button
  66. aria-label="Remove"
  67. onClick={this.handleRemove}
  68. disabled={this.state.loading}
  69. icon={<IconDelete />}
  70. />
  71. </StyledPanelItem>
  72. );
  73. }
  74. }
  75. const StyledPanelItem = styled(PanelItem)`
  76. padding: ${space(2)};
  77. align-items: center;
  78. `;
  79. const ApplicationNameWrapper = styled('div')`
  80. display: flex;
  81. flex-direction: column;
  82. flex: 1;
  83. margin-right: ${space(1)};
  84. `;
  85. const ApplicationName = styled(Link)`
  86. margin-bottom: ${space(1)};
  87. `;
  88. const ClientId = styled('div')`
  89. color: ${p => p.theme.subText};
  90. font-size: ${p => p.theme.fontSizeSmall};
  91. `;
  92. export default Row;