monitorHeaderActions.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {browserHistory} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {
  4. addErrorMessage,
  5. addLoadingMessage,
  6. clearIndicators,
  7. } from 'sentry/actionCreators/indicator';
  8. import Button from 'sentry/components/button';
  9. import ButtonBar from 'sentry/components/buttonBar';
  10. import Confirm from 'sentry/components/confirm';
  11. import {IconDelete, IconEdit} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import space from 'sentry/styles/space';
  14. import {logException} from 'sentry/utils/logging';
  15. import useApi from 'sentry/utils/useApi';
  16. import {Monitor} from './types';
  17. type Props = {
  18. monitor: Monitor;
  19. onUpdate: (data: Monitor) => void;
  20. orgId: string;
  21. };
  22. const MonitorHeaderActions = ({monitor, orgId, onUpdate}: Props) => {
  23. const api = useApi();
  24. const handleDelete = () => {
  25. const redirectPath = `/organizations/${orgId}/monitors/`;
  26. addLoadingMessage(t('Deleting Monitor...'));
  27. api
  28. .requestPromise(`/monitors/${monitor.id}/`, {
  29. method: 'DELETE',
  30. })
  31. .then(() => {
  32. browserHistory.push(redirectPath);
  33. })
  34. .catch(() => {
  35. addErrorMessage(t('Unable to remove monitor.'));
  36. });
  37. };
  38. const updateMonitor = (data: Partial<Monitor>) => {
  39. addLoadingMessage();
  40. api
  41. .requestPromise(`/monitors/${monitor.id}/`, {
  42. method: 'PUT',
  43. data,
  44. })
  45. .then(resp => {
  46. clearIndicators();
  47. onUpdate?.(resp);
  48. })
  49. .catch(err => {
  50. logException(err);
  51. addErrorMessage(t('Unable to update monitor.'));
  52. });
  53. };
  54. const toggleStatus = () =>
  55. updateMonitor({
  56. status: monitor.status === 'disabled' ? 'active' : 'disabled',
  57. });
  58. return (
  59. <ButtonContainer>
  60. <ButtonBar gap={1}>
  61. <Button
  62. size="sm"
  63. icon={<IconEdit size="xs" />}
  64. to={`/organizations/${orgId}/monitors/${monitor.id}/edit/`}
  65. >
  66. &nbsp;
  67. {t('Edit')}
  68. </Button>
  69. <Button size="sm" onClick={toggleStatus}>
  70. {monitor.status !== 'disabled' ? t('Pause') : t('Enable')}
  71. </Button>
  72. <Confirm
  73. onConfirm={handleDelete}
  74. message={t(
  75. 'Deleting this monitor is permanent. Are you sure you wish to continue?'
  76. )}
  77. >
  78. <Button size="sm" icon={<IconDelete size="xs" />}>
  79. {t('Delete')}
  80. </Button>
  81. </Confirm>
  82. </ButtonBar>
  83. </ButtonContainer>
  84. );
  85. };
  86. const ButtonContainer = styled('div')`
  87. margin-bottom: ${space(3)};
  88. display: flex;
  89. flex-shrink: 1;
  90. `;
  91. export default MonitorHeaderActions;